Connector operations

Updated at:

Use the Go SDK of DataHub to create, query, update, and delete Connectors that synchronize streaming data to other Alibaba Cloud services.

Connector overview

A DataHub Connector synchronizes streaming data from DataHub to other Alibaba Cloud services. DataHub supports real-time or near real-time synchronization to the following destinations:

  • MaxCompute (formerly ODPS)

  • OSS (Object Storage Service)

  • Elasticsearch (ES)

  • AnalyticDB for MySQL (ADS)

  • MySQL

  • Function Compute (FC)

  • Open Table Store (OTS)

  • DataHub

Write data once into a DataHub topic and configure the synchronization rules. The data will then be available in the specified destination.

All examples in this document use MaxCompute as the target system. For more information, see Create MaxCompute synchronization.

Note

Starting from version v2.14.0, most API parameters that previously used connectorType now use connectorId instead (except for CreateConnector). Backward compatibility is maintained. You only need to convert the connectorType parameter to a string.

Conversion example:

1. gcr, err := dh.GetConnector(projectName, topicName, string(datahub.SinkOdps))

Create a connector

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

cType

ConnectorType

The type of connector to create.

columnFields

string

The fields to synchronize.

sinkStartTime

int64

The start time of the connector.

config

interface

The connector configuration.

Response example

type CreateConnectorResult struct {
    ConnectorId string `json:"ConnectorId"`
 }

Error codes

Class name

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

ResourceExistError

ResourceAlreadyExist

ProjectAlreadyExist

TopicAlreadyExist

ConnectorAlreadyExist

The resource already exists.

AuthorizationFailedError

ResourceAlreadyExist

ProjectAlreadyExist

TopicAlreadyExist

ConnectorAlreadyExist

The resource already exists.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func createConnector(dh datahub.DataHub, projectName, topicName string) {
    odpsEndpoint := ""
    odpsProject := "datahub_test"
    odpsTable := "datahub_go_example"
    odpsAccessId := ""
    odpsAccessKey := "="
    odpsTimeRange := 60
    odpsPartitionMode := datahub.SystemTimeMode
    connectorType := datahub.SinkOdps
    odpsPartitionConfig := datahub.NewPartitionConfig()
    odpsPartitionConfig.AddConfig("ds", "%Y%m%d")
    odpsPartitionConfig.AddConfig("hh", "%H")
    odpsPartitionConfig.AddConfig("mm", "%M")
    sinkOdpsConfig := datahub.SinkOdpsConfig{
        Endpoint:        odpsEndpoint,
        Project:         odpsProject,
        Table:           odpsTable,
        AccessId:        odpsAccessId,
        AccessKey:       odpsAccessKey,
        TimeRange:       odpsTimeRange,
        PartitionMode:   odpsPartitionMode,
        PartitionConfig: *odpsPartitionConfig,
    }
    fileds := []string{"field1", "field2"}
    if err := dh.CreateConnector(projectName, topicName, connectorType, fileds, *sinkOdpsConfig); err != nil {
        fmt.Println("create odps connector failed")
        fmt.Println(err)
        return
    }
    fmt.Println("create odps connector successful")
}

List connectors

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

Response example

type ListConnectorResult struct {
    ConnectorIds []string `json:"Connectors"`
}

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

The error message returned because the Authorization signature parsing failed. Verify that the AccessKey is correct.

DatahubClientError

-

All other errors, and the base class for all exceptions.

InvalidParameterError

InvalidParameter

InvalidCursor

The error message returned because a parameter value is invalid.

Sample code

func listConnector(dh datahub.DataHub, projectName, topicName string) {
    lc, err := dh.ListConnector(projectName, topicName)
    if err != nil {
        fmt.Println("get connector list failed")
        fmt.Println(err)
        return
    }
    fmt.Println("get connector list successful")
    fmt.Println(lc)
}

Query a connector

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

connectorId

string

The ID of the connector.

Response example

type GetConnectorResult struct {
    CreateTime     int64             `json:"CreateTime"`
    LastModifyTime int64             `json:"LastModifyTime"`
    ConnectorId    string            `json:"ConnectorId"`
    ClusterAddress string            `json:"ClusterAddress"`
    Type           ConnectorType     `json:"Type"`
    State          ConnectorState    `json:"State"`
    ColumnFields   []string          `json:"ColumnFields"`
    ExtraConfig    map[string]string `json:"ExtraInfo"`
    Creator        string            `json:"Creator"`
    Owner          string            `json:"Owner"`
    Config         interface{}       `json:"Config"`
}

Error codes

Class name

Error code

Description

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func getConnector(dh datahub.DataHub, projectName, topicName, connectorId string) {
    gcr, err := dh.GetConnector(projectName, topicName, connectorId)
    if err != nil {
        fmt.Println("get odps conector failed")
        fmt.Println(err)
        return
    }
    fmt.Println("get odps conector successful")
    fmt.Println(*gcr)
}

Update connector configuration

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

connectorId

string

The ID of the connector.

config

interface

The connector configuration.

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func updateConnector(dh datahub.DataHub, projectName, topicName, connectorId string) {
    gc, err := dh.GetConnector(projectName, topicName, connectorId)
    if err != nil {
        fmt.Println("get odps connector failed")
        fmt.Println(err)
        return
    }
    config, ok := gc.Config.(datahub.SinkOdpsConfig)
    if !ok {
        fmt.Println("convert config to SinkOdpsConfig failed")
        return
    }
    // modify the config
    config.TimeRange = 200
    if err := dh.UpdateConnector(projectName, topicName, connectorId, config); err != nil {
        fmt.Println("update odps config failed")
        fmt.Println(err)
        return
    }
    fmt.Println("update odps config successful")
}

Delete a connector

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

connectorId

string

The ID of the connector.

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

 func deleteConnector(dh datahub.DataHub, projectName, topicName, connectorId string) {
     if err := dh.DeleteConnector(projectName, topicName, connectorId); err != nil {
         fmt.Println("delete odps connector failed")
         fmt.Println(err)
         return
     }
    fmt.Println("delete odps connector successful")
 }

Query connector shard status

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

shardId

string

The ID of the shard.

connectorId

string

The ID of the connector.

Response example

// getConnectorShardStatus
type GetConnectorShardStatusResult struct {
    ShardStatus map[string]ConnectorShardStatusEntry `json:"ShardStatusInfos"`
}
// GetConnectorShardStatusByShard
type ConnectorShardStatusEntry struct {
    StartSequence    int64               `json:"StartSequence"`
    EndSequence      int64               `json:"EndSequence"`
    CurrentSequence  int64               `json:"CurrentSequence"`
    CurrentTimestamp int64               `json:"CurrentTimestamp"`
    UpdateTime       int64               `json:"UpdateTime"`
    State            ConnectorShardState `json:"State"`
    LastErrorMessage string              `json:"LastErrorMessage"`
    DiscardCount     int64               `json:"DiscardCount"`
    DoneTime         int64               `json:"DoneTime"`
    WorkerAddress    string              `json:"WorkerAddress"`
}

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func getConnectorShardStatus(dh datahub.DataHub, projectName, topicName, connectorId string) {
    gcs, err := dh.GetConnectorShardStatus(projectName, topicName, connectorId)
    if err != nil {
        fmt.Println("get connector shard status failed")
        fmt.Println(err)
        return
    }
    fmt.Println("get connector shard status successful")
    for shard, status := range gcs.ShardStatus {
        fmt.Println(shard, status.State)
    }
    shardId := "0"
    gc, err := dh.GetConnectorShardStatusByShard(projectName, topicName, connectorId, shardId)
    if err != nil {
        fmt.Println("get connector shard status failed")
        fmt.Println(err)
        return
    }
    fmt.Println("get connector shard status successful")
    fmt.Println(*gc)
}

Restart a connector shard

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

shardId

string

The ID of the shard.

connectorId

string

The ID of the connector.

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func reloadConnector(dh datahub.DataHub, projectName, topicName, connectorId string) {
    if err := dh.ReloadConnector(projectName, topicName, connectorId); err != nil {
        fmt.Println("reload connector shard failed")
        fmt.Println(err)
        return
    }
    fmt.Println("reload connector shard successful")
    shardId := "2"
    if err := dh.ReloadConnectorByShard(projectName, topicName, connectorId, shardId); err != nil {
        fmt.Println("reload connector shard failed")
        fmt.Println(err)
        return
    }
    fmt.Println("reload connector shard successful")
}

Add a new field

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

connectorId

string

The ID of the shard.

fieldName

string

The name of the field.

Error codes

Class name

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

Code example

func appendConnectorField(dh datahub.DataHub, projectName, topicName, connectorId string) {
    if err := dh.AppendConnectorField(projectName, topicName, connectorId, "field2"); err != nil {
        fmt.Println("append filed failed")
        fmt.Println(err)
        return
    }
    fmt.Println("append filed successful")
}

Update connector status

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

connectorId

string

The ID of the connector.

state

ConnectorState

The target state of the connector.

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func updateConnectorState(dh datahub.DataHub, projectName, topicName, connectorId string) {
    if err := dh.UpdateConnectorState(projectName, topicName, connectorId, datahub.ConnectorStopped); err != nil {
        fmt.Println("update connector state failed")
        fmt.Println(err)
        return
    }
    fmt.Println("update connector state successful")
    if err := dh.UpdateConnectorState(projectName, topicName, connectorId, datahub.ConnectorRunning); err != nil {
        fmt.Println("update connector state failed")
        fmt.Println(err)
        return
    }
    fmt.Println("update connector state successful")
}

Update connector offset

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

shardId

string

The ID of the shard.

connectorId

string

The ID of the connector.

offset

ConnectorOffset

The connector offset.

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func updateConnectorOffset(dh datahub.DataHub, projectName, topicName, connectorId string) {
    shardId := "10"
    offset := datahub.ConnectorOffset{
        Timestamp: 1565864139000,
        Sequence:  104,
    }
    dh.UpdateConnectorState(projectName, topicName, connectorId, datahub.ConnectorStopped)
    defer dh.UpdateConnectorState(projectName, topicName, connectorId, datahub.ConnectorRunning)
    if err := dh.UpdateConnectorOffset(projectName, topicName, connectorId, shardId, offset); err != nil {
        fmt.Println("update connector offset failed")
        fmt.Println(err)
        return
    }
    fmt.Println("update connector offset successful")
}

Query connector completion time

Parameters

Name

Type

Description

projectName

string

The project name.

topicName

string

The topic name.

connectorId

string

The ID of the connector.

Response example

1. type GetConnectorDoneTimeResult struct {
2.     DoneTime int64 `json:"DoneTime"`
3. }

Error codes

Error class

Error code

Description

ResourceNotFoundError

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The requested resource does not exist. This error may occur if you send a request immediately after a Split/Merge operation.

AuthorizationFailedError

Unauthorized

Failed to parse the authorization signature. Check whether the AccessKey is valid.

InvalidParameterError

InvalidParameter

InvalidCursor

Invalid parameters.

DatahubClientError

-

The base class for all other exceptions.

Code example

func doneTime(dh datahub.DataHub, projectName, topicName, connectorId string) {
    gcd, err := dh.GetConnectorDoneTime(projectName, topicName, connectorId)
    if err != nil {
        fmt.Println("get connector done time failed")
        fmt.Println(err)
        return
    }
    fmt.Println("get connector done time successful")
    fmt.Println(gcd.DoneTime)
}