You need to enable JavaScript to run this app.
Torch Log Service

Torch Log Service

Copy page
Download PDF
Go SDK
Import Kafka data using the Go SDK
Copy page
Download PDF
Import Kafka data using the Go SDK
TLS supports importing Kafka data via the SDK. This document demonstrates how to import data from Kafka into TLS using sample code with the Go SDK.​
Prerequisites​
  • TLS Go SDK has been installed. For more information, see Install Go SDK.​
  • The following commands have been executed to install the proto dependency package.​
  • ​
    go get -u github.com/gogo/protobuf/proto​
    ​
  • The VOLCENGINE_ACCESS_KEY_ID and other environment variables have been added. For details on the environment variable configuration method, please refer to Configure authentication information.​
warning​
It is recommended to use environment variables to dynamically obtain Volcengine keys and other authentication information in order to avoid data security risks caused by hardcoding AccessKey.​
Scenario description​
This document uses sample code to demonstrate how to use the GO SDK to create a Kafka data import task in a specified project. For related feature overview, important notes, and cost details, please refer to Import data from Kafka.​
Sample code​
​
package demo​
​
import (​
"github.com/volcengine/volc-sdk-golang/service/tls"​
"os"​
)​
​
func CreateImportKafkaTaskDemo() (*tls.CreateImportTaskResponse, error) {​
// When initializing the client, it's recommended to dynamically obtain the Volcano Engine key and other authentication information through environment variables to avoid data security risks caused by hard-coding the AccessKey. For detailed instructions, see https://www.volcengine.com/docs/6470/1166455.​
// When using STS, both ak and sk use temporary keys and set VOLCENGINE_TOKEN; when not using STS, the VOLCENGINE_TOKEN part is passed empty.​
client := tls.NewClient(os.Getenv("VOLCENGINE_ENDPOINT"), os.Getenv("VOLCENGINE_ACCESS_KEY_ID"), os.Getenv("VOLCENGINE_ACCESS_KEY_SECRET"), os.Getenv("VOLCENGINE_TOKEN"), os.Getenv("VOLCENGINE_REGION"))​
//Create a data import task.​
//For a description of the CreateImportTask API request parameters, see https://www.volcengine.com/docs/6470/1333757.​
createImportTaskResp, err := client.CreateImportTask(&tls.CreateImportTaskRequest{​
TopicID: "fa**",​
TaskName: "task-name-1",​
SourceType: "kafka",​
Description: "this is a task",​
ImportSourceInfo: &tls.ImportSourceInfo{​
KafkaSourceInfo: &tls.KafkaSourceInfo{​
Host: "kafka-cnngc7an0qpv****.kafka.ivolces.com:9092",​
Group: "mygroup",​
Topic: "test",​
Encode: "UTF-8",​
Password: "",​
Protocol: "",​
Username: "",​
Mechanism: "",​
InstanceID: "",​
IsNeedAuth: "false",​
InitialOffset: 0,​
TimeSourceDefault: 0,​
},​
},​
TargetInfo: &tls.TargetInfo{​
Region: "cn-shanghai",​
LogType: "json_log",​
ExtractRule: &tls.ImportExtractRule{​
TimeZone: "GMT",​
SkipLineCount: 0,​
TimeExtractRegex: "",​
ExtractRule: &tls.ExtractRule{​
Quote: "\"",​
},​
},​
},​
})​
​
if err != nil {​
return nil, err​
}​
​
return createImportTaskResp, nil​
}​
​
Related documentation​
  • After you send an API request through the SDK, you will receive a response from the server. If the response contains a status code other than 200, it indicates that the API call has failed. You can refer to the documentation for each API to view the corresponding error code information.​
  • The APIs related to data import tasks are as follows. You can refer to the API documentation to view detailed information about the corresponding interface.​
  • ​
    API​
    Description​
    Call the CreateImportTask API to create a data import task.​
    Call the DeleteImportTask API to delete the specified data import task.​
    Call the ModifyImportTask API to modify the specified data import task.​
    Call the DescribeImportTask API to obtain information about a specific data import task.​
    Call the DescribeImportTasks API to obtain the list of data import tasks.​
    ​
​
Last updated: 2025.10.11 16:44:08