Topic operations

Updated at:

Use the DataHub Go SDK to create, delete, list, and update topics.

Creating a topic

Creating a tuple topic

Data written to a tuple topic requires a specific format. You must specify the record schema. The following data types are supported:

Type

Description

Range

BIGINT

8-byte signed integer

-9223372036854775807 ~ 9223372036854775807

DOUBLE

8-byte double-precision floating-point number

-1.0 _10^308 ~ 1.0 _10^308

BOOLEAN

The Boolean data type

Valid values:

  • True/False.

  • true/false.

  • 0/1.

TIMESTAMP

Timestamp type

Represents a timestamp accurate to microseconds.

STRING

String, only UTF-8 encoding is supported

A single STRING column can be up to 2MB in length.

DECIMAL

s

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

string

The name of the topic.

comment

string

The description of the topic.

lifeCycle

int

The data expiration time, in days. Data written before this period is not accessible.

recordSchema

RecordSchema

The records schema of the topic.

Errors

Error class

Error code

Description

ResourceExistError

ResourceAlreadyExist

ProjectAlreadyExist

TopicAlreadyExist

ConnectorAlreadyExist

The specified resource already exists.

AuthorizationFailedError

Unauthorized

Authorization signature parsing error. Verify that the AccessKey is correct.

DatahubClientError

-

A general client error. You can usually retry, but limit the number of retries.

InvalidParameterError

InvalidParameter

InvalidCursor

A parameter is set to an invalid value.

Sample code

func Example_CreateTupleTopic(dh datahub.DataHub, projectName, topicName string) {
    recordSchema := datahub.NewRecordSchema()
    recordSchema.AddField(datahub.Field{Name: "bigint_field", Type: datahub.BIGINT, AllowNull: true}).
        AddField(datahub.Field{Name: "timestamp_field", Type: datahub.TIMESTAMP, AllowNull: false}).
        AddField(datahub.Field{Name: "string_field", Type: datahub.STRING}).
        AddField(datahub.Field{Name: "double_field", Type: datahub.DOUBLE}).
        AddField(datahub.Field{Name: "boolean_field", Type: datahub.BOOLEAN})
    if err := dh.CreateTupleTopic(projectName, topicName, "topic comment", 5, 7, recordSchema); err != nil {
        fmt.Println("create topic failed")
        fmt.Println(err)
        return
    }
    fmt.Println("create topic successful")
}

Creating a blob topic

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

string

The name of the topic.

comment

string

The description of the topic.

lifeCycle

int

The data expiration time, in days. Data written before this period is not accessible.

Errors

Error class

Error code

Description

ResourceExistError

ResourceAlreadyExist

ProjectAlreadyExist

TopicAlreadyExist

ConnectorAlreadyExist

The specified resource already exists.

AuthorizationFailedError

Unauthorized

Authorization signature parsing error. Verify that the AccessKey is correct.

DatahubClientError

-

Other errors. This is the base class for all exceptions.

InvalidParameterError

InvalidParameter

InvalidCursor

A parameter is set to an invalid value.

Sample code

func Example_CreateBlobTopic(dh datahub.DataHub, projectName, topicName string) {
    if err := dh.CreateBlobTopic(projectName, topicName, "topic comment", 5, 7); err != nil {
        fmt.Println("create topic failed")
        fmt.Println(err)
        return
    }
    fmt.Println("create topic successful")
}

Deleting a topic

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

string

The name of the topic.

Errors

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist. This error may also occur if you send requests immediately after a split or merge operation.

AuthorizationFailedError

Unauthorized

Authorization signature parsing error. Verify that the AccessKey is correct.

DatahubClientError

A general client error. You can usually retry, but limit the number of retries.

InvalidParameterError

InvalidParameter

InvalidCursor

A parameter is set to an invalid value.

Sample code

func ExampleDataHub_DeleteTopic(dh datahub.DataHub, projectName, topicName string) {
    if err := dh.DeleteTopic(projectName, topicName); err != nil {
        fmt.Println("delete failed")
        fmt.Println(err)
        return
    }
    fmt.Println("delete successful")
}

Listing topics

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

Response example

type ListTopicResult struct {
    TopicNames [] string `json:"TopicNames"`
}

Errors

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist. This error may also occur if you send requests immediately after a split or merge operation.

AuthorizationFailedError

Unauthorized

Authorization signature parsing error. Verify that the AccessKey is correct.

DatahubClientError

A general client error. You can usually retry, but limit the number of retries.

InvalidParameterError

InvalidParameter

InvalidCursor

A parameter is set to an invalid value.

Sample code

func ExampleDataHub_ListTopic(dh datahub.DataHub, projectName, topicName string) {
    lt, err := dh.ListTopic(projectName)
    if err != nil {
        fmt.Println("get topic list failed")
        fmt.Println(err)
        return
    }
    fmt.Println("get topic list successful")
    fmt.Println(lt)
}

Updating a topic

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

string

The name of the topic.

comment

string

The description of the topic.

Errors

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist. This error may also occur if you send requests immediately after a split or merge operation.

AuthorizationFailedError

Unauthorized

Authorization signature parsing error. Verify that the AccessKey is correct.

DatahubClientError

A general client error. You can usually retry, but limit the number of retries.

InvalidParameterError

InvalidParameter

InvalidCursor

A parameter is set to an invalid value.

Sample code

func ExampleDataHub_UpdateTopic(dh datahub.DataHub, projectName, topicName string) {
    if err := dh.UpdateTopic(projectName, topicName, "new topic comment"); err != nil {
        fmt.Println("update topic comment failed")
        fmt.Println(err)
        return
    }
    fmt.Println("update topic comment successful")
}

More operations

Creating a tuple topic and reading or writing records requires a schema that specifies each field's name and data type.

Note

During network transmission, data is sent as strings and converted to the corresponding types by using the schema. A schema is a slice of Field objects. Each Field has three parameters: field name, field type, and a Boolean value that indicates whether the field allows null values (True) or not (False).

Getting a schema

For an existing tuple topic, you can obtain the schema by calling the get_topic method:

func getSchema(dh datahub.DataHub, projectName, topicName string) {
    gt, err := dh.GetTopic(projectName, "topic_test")
    if err != nil {
        fmt.Println("get topic failed")
        fmt.Println(err)
        return
    } else {
        schema := gt.RecordSchema
        fmt.Println(schema)
    }
}

Defining a schema

To create a tuple topic, you must define a schema. You can initialize a schema in the following ways:

Creating a schema directly

func createSchema1() {
    fields := []datahub.Field{
        {"bigint_field", datahub.BIGINT, true},
        {"timestamp_field", datahub.TIMESTAMP, false},
        {"string_field", datahub.STRING, false},
        {"double_field", datahub.DOUBLE, false},
        {"boolean_field", datahub.BOOLEAN, true},
        {"decimal_field", datahub.DECIMAL, false},
    }
    schema := datahub.RecordSchema{
        fields,
    }
    fmt.Println(schema)
}

Initializing a schema step by step

func createSchema2() {
    recordSchema := datahub.NewRecordSchema()
    recordSchema.AddField(datahub.Field{Name: "bigint_field", Type: datahub.BIGINT, AllowNull: true}).
        AddField(datahub.Field{Name: "timestamp_field", Type: datahub.TIMESTAMP, AllowNull: false}).
        AddField(datahub.Field{Name: "string_field", Type: datahub.STRING}).
        AddField(datahub.Field{Name: "double_field", Type: datahub.DOUBLE}).
        AddField(datahub.Field{Name: "boolean_field", Type: datahub.BOOLEAN}).
        AddField(datahub.Field{Name: "decimal_field", Type: datahub.DECIMAL})
}

Defining a schema using a JSON string

func createSchema3() {
    str := ""
    schema, err := datahub.NewRecordSchemaFromJson(str)
    if err != nil {
        fmt.Println("create recordSchema failed")
        fmt.Println(err)
        return
    }
    fmt.Println("create recordSchema successful")
    fmt.Println(schema)
}
Note

The format of the JSON string is: {"fields":[{"type":"BIGINT","name":"a"},{"type":"STRING","name":"b"}]}