本文提供 DTS Go SDK 的代码示例供您参考。
使用 Go SDK 调用 DTS 接口时,默认无超时时间设置。 您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: ${超时时间} * time.Second})
来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。 例如,您可以通过如下代码将客户端的请求超时时间设置为 10 秒。
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")). WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) times := int32(1) // 付费方式 chargeType := dts_v20221001.CreateTransmissionTaskBodyChargeConfigChargeType("PostPaid") chargeConfig := &dts_v20221001.CreateTransmissionTaskBodyChargeConfig{ ChargeType: &chargeType, Times: ×, } // 订阅中间件 endpointType := dts_v20221001.CreateTransmissionTaskBodyDestConfigEndpointType("Builtin_Kafka") // 中间件配置 builtinKafkaSettings := &dts_v20221001.CreateTransmissionTaskBodyDestConfigBuiltinKafkaSettings{} destConfig := &dts_v20221001.CreateTransmissionTaskBodyDestConfig{ EndpointType: &endpointType, BuiltinKafkaSettings: builtinKafkaSettings, } // 项目名称 projectName := "default" solutionType := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsSolutionType("MySQL2Kafka") // 最大重试时间 maxRetrySeconds := int32(7200) errorBehaviorSettings := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsErrorBehaviorSettings{ MaxRetrySeconds: &maxRetrySeconds, } // 订阅类型-全量订阅的配置 enableFull := false fullTransmissionSettings := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsFullTransmissionSettings{ EnableFull: &enableFull, } enableIncr := true //订阅类型-增量订阅的配置 statments_1 := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsIncrTransmissionSettingsStatementsItem("StmtDMLUpdate") statements := []*dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsIncrTransmissionSettingsStatementsItem{&statments_1} incrTransmissionSettings := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsIncrTransmissionSettings{ EnableIncr: &enableIncr, Statements: statements, } // 订阅类型-结构订阅的配置 enableMeta := false metaTransmissionSettings := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsMetaTransmissionSettings{ EnableMeta: &enableMeta, } // 订阅起始点 mySQLPosition := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsMySQLPosition{} destObjName_1 := "d1" mappingList_1_destObjName_1 := "*" setObjectTransType := true meta := dts_v20221001.ObjectMapping20221001ObjectMappingSettingObjectTransTypesItem("Meta") incr := dts_v20221001.ObjectMapping20221001ObjectMappingSettingObjectTransTypesItem("Incr") objectTransTypes := []*dts_v20221001.ObjectMapping20221001ObjectMappingSettingObjectTransTypesItem{&meta, &incr} mappingList_1_objectMappingSetting_1 := dts_v20221001.ObjectMapping20221001ObjectMappingSetting{ SetObjectTransType: &setObjectTransType, ObjectTransTypes: objectTransTypes, } objectType := dts_v20221001.ObjectMapping20221001ObjectType("Table") srcObjName := "*" mappingList_1 := dts_v20221001.ObjectMapping20221001{ DestObjName: &mappingList_1_destObjName_1, ObjectMappingSetting: &mappingList_1_objectMappingSetting_1, ObjectType: &objectType, SrcObjName: &srcObjName, } mappingList := []*dts_v20221001.ObjectMapping20221001{ &mappingList_1, } meta_sub := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsPropertiesItemsItem("Meta") incr_sub := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsPropertiesItemsItem("Incr") objectTransTypes_sub := []*dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsPropertiesItemsItem{&meta_sub, &incr_sub} setObjectTransType_sub := true objectMappingSetting := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsItemObjectMappingSetting{ ObjectTransTypes: objectTransTypes_sub, SetObjectTransType: &setObjectTransType_sub, } objectType_sub := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsItemObjectType("Database") srcObjName_sub := "d1" objectMappings_1 := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsItem{ DestObjName: &destObjName_1, MappingList: mappingList, ObjectMappingSetting: &objectMappingSetting, ObjectType: &objectType_sub, SrcObjName: &srcObjName_sub, } // 订阅对象 objectMappings := []*dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsObjectMappingsItem{ &objectMappings_1, } // 订阅格式 protocol := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsSubscriptionSettingsProtocol("Volc") // 私有网络 vpcId := "vpcId" // 子网 vpcSubnetId := "vpcSubnetId" // 订阅配置 subscriptionSettings := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettingsSubscriptionSettings{ Protocol: &protocol, VPCID: &vpcId, VPCSubnetID: &vpcSubnetId, } // mysql2kafka 配置 mySQL2KafkaSettings := dts_v20221001.CreateTransmissionTaskBodySolutionSettingsMySQL2KafkaSettings{ ErrorBehaviorSettings: &errorBehaviorSettings, FullTransmissionSettings: &fullTransmissionSettings, IncrTransmissionSettings: &incrTransmissionSettings, MetaTransmissionSettings: &metaTransmissionSettings, MySQLPosition: &mySQLPosition, ObjectMappings: objectMappings, SubscriptionSettings: &subscriptionSettings, } // 订阅对象配置 solutionSettings := &dts_v20221001.CreateTransmissionTaskBodySolutionSettings{ SolutionType: &solutionType, MySQL2KafkaSettings: &mySQL2KafkaSettings, } srcEndpointType := dts_v20221001.CreateTransmissionTaskBodySrcConfigEndpointType("Volc_MySQL") // 源端实例 id dbInstanceId := "dbInstanceId" // 数据库账号 password := "password" // 账号密码 userName := "userName" region := "cn-beijing" // 源端 region 配置 regionSettings := dts_v20221001.CreateTransmissionTaskBodySrcConfigVolcMySQLSettingsRegionSettings{ Region: ®ion, } // 源端 mysql 配置 volcMySQLSettings := dts_v20221001.CreateTransmissionTaskBodySrcConfigVolcMySQLSettings{ DBInstanceID: &dbInstanceId, Password: &password, Username: &userName, RegionSettings: ®ionSettings, } // 源端配置 srcConfig := dts_v20221001.CreateTransmissionTaskBodySrcConfig{ EndpointType: &srcEndpointType, VolcMySQLSettings: &volcMySQLSettings, } // 标签 tags := []*dts_v20221001.CreateTransmissionTaskBodyTagsItem{} // 任务名称 taskName := "taskName" // 任务类型 taskType := dts_v20221001.CreateTransmissionTaskBodyTaskType("DataSubscription") // 规格 trafficSpec := dts_v20221001.CreateTransmissionTaskBodyTrafficSpec("Standard") param := &dts_v20221001.CreateTransmissionTaskBody{ ChargeConfig: chargeConfig, DestConfig: destConfig, ProjectName: &projectName, SolutionSettings: solutionSettings, SrcConfig: &srcConfig, Tags: tags, TaskName: &taskName, TaskType: &taskType, TrafficSpec: &trafficSpec, } resp, err := instance.CreateTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", resp) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.DescribeTransmissionTaskInfoBody{TaskID: &taskID} resp, err := instance.DescribeTransmissionTaskInfo(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) taskType := "DataSubscription" param := &dts_v20221001.DescribeTransmissionTasksBody{TaskType:(*dts_v20221001.DescribeTransmissionTasksBodyTaskType)(&taskType)} resp, err := instance.DescribeTransmissionTasks(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.SuspendTransmissionTaskBody{TaskID: &taskID} resp, err := instance.SuspendTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID ID1 := "" ID2 := "" taskIDs := []*string{&ID1, &ID2} param := &dts_v20221001.SuspendTransmissionTasksBody{TaskIDs: taskIDs} resp, err := instance.SuspendTransmissionTasks(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.StartTransmissionTaskBody{TaskID: &taskID} resp, err := instance.StartTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID ID1 := "ID1" ID2 := "ID2" taskIDs := []*string{&ID_1, &ID_2} param := &dts_v20221001.StartTransmissionTasksBody{TaskIDs: taskIDs} resp, err := instance.StartTransmissionTasks(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.RetryTransmissionTaskBody{TaskID: &taskID} resp, err := instance.RetryTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) ID1 := "" ID2 := "" taskIDs := []*string{&ID1, &ID2} param := &dts_v20221001.RetryTransmissionTasksBody{TaskIDs: taskIDs} resp, err := instance.RetryTransmissionTasks(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.ResumeTransmissionTaskBody{TaskID: &taskID} resp, err := instance.ResumeTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.StopTransmissionTaskBody{TaskID: &taskID} resp, err := instance.StopTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20221001" ) func main() { instance := dts_v20221001.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) // 任务 ID taskID := "taskID" param := &dts_v20221001.DeleteTransmissionTaskBody{TaskID: &taskID} resp, err := instance.DeleteTransmissionTask(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20180101" ) func main() { instance := dts_v20180101.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) param := &dts_v20180101.PreCheckAsyncBody{ TaskID: "TaskID", } resp, err := instance.PreCheckAsync(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }
package main import ( "context" "fmt" "os" "github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/service/dts/v20180101" ) func main() { instance := dts_v20180101.NewInstance() // 强烈建议不要把 AccessKey ID 和 AccessKey Secret 保存到工程代码里,否则可能导致 AccessKey 泄露,威胁您账号下所有资源的安全。 // 本示例通过从环境变量中读取 AccessKey ID 和 AccessKey Secret,来实现 API 访问的身份验证。运行代码示例前,请配置环境变量 ACCESS_KEY_ID 和 ACCESS KEY_SECRET instance.SetCredential(base.Credentials{ AccessKeyID: os.Getenv("AK"), SecretAccessKey: os.Getenv("SK"), }) param := &dts_v20180101.GetAsyncPreCheckResultBody{ // 预检查的 ID ID: "ID", } resp, err := instance.GetAsyncPreCheckResult(context.Background(), param) if err != nil { fmt.Printf("error %v", err) } else { fmt.Printf("success %v", string(resp)) } }