Create, delete, query, and list connectors by using the DataHub C++ SDK.
Create a connector
Create a connector for a specified topic.
Parameters
| Name |
Type |
Description |
projectName |
string |
The name of the project. |
topicName |
string |
The name of the topic. |
Return value
| Type |
Description |
CreateConnectorResult |
Call GetConnectorId() on the result to retrieve the connector ID. |
Code example (MaxCompute)
MaxCompute was previously known as ODPS. The SDK retains the legacy name ODPS in class and constant names such as SinkOdpsConfig and SINK_ODPS.
#include <iostream>
#include <string>
#include <vector>
#include <utility>
void CreateConnector()
{
try {
// Configure the MaxCompute (ODPS) sink
sdk::SinkOdpsConfig config;
config.SetEndpoint("<your-maxcompute-endpoint>");
config.SetProject("<your-maxcompute-project>");
config.SetTable("<your-maxcompute-table>");
config.SetAccessId("<your-access-id>");
config.SetAccessKey("<your-access-key>");
config.SetPartitionMode(sdk::PartitionMode::SYSTEM_TIME);
config.SetTimeRange(15);
const std::string projectName = "<your-project-name>";
const std::string topicName = "<your-topic-name>";
// Define time-based partition columns
std::vector<std::pair<std::string, std::string> > partitionConfig;
partitionConfig.push_back(std::pair<std::string, std::string>("ds", "%Y%m%d"));
partitionConfig.push_back(std::pair<std::string, std::string>("hh", "%H"));
partitionConfig.push_back(std::pair<std::string, std::string>("mm", "%M"));
config.SetPartitionConfig(partitionConfig);
// Specify the columns to synchronize
std::vector<std::string> columnFields;
std::string fieldName1 = "<your-field-name>";
columnFields.push_back(fieldName1);
// Create the connector
const CreateConnectorResult& connectorResult = client.CreateConnector(
projectName, topicName, sdk::ConnectorType::SINK_ODPS, columnFields, config);
std::cout << "Connector ID: " << connectorResult.GetConnectorId() << std::endl;
}
catch (const DatahubException& e)
{
std::cerr << "Failed to create connector: " << e.GetRequestId()
<< ", ErrorCode: " << e.GetErrorCode()
<< ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}
Delete a connector
Delete a connector from a specified topic.
Parameters
| Name |
Type |
Description |
projectName |
string |
The name of the project. |
topicName |
string |
The name of the topic. |
connectorId |
string |
The ID of the connector to delete. |
Code example
#include <iostream>
#include <string>
void DeleteConnector()
{
const std::string projectName = "<your-project-name>";
const std::string topicName = "<your-topic-name>";
const std::string connectorId = "<your-connector-id>";
try {
// Delete the connector
client.DeleteConnector(projectName, topicName, connectorId);
}
catch (const DatahubException& e)
{
std::cerr << "Failed to delete connector: " << e.GetRequestId()
<< ", ErrorCode: " << e.GetErrorCode()
<< ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}
Query a connector
Retrieve the configuration and details of a specified connector.
Parameters
| Name |
Type |
Description |
projectName |
string |
The name of the project. |
topicName |
string |
The name of the topic. |
connectorId |
string |
The ID of the connector to query. |
Return value
| Type |
Description |
GetConnectorResult |
Call GetConfig() on the result to retrieve the connector configuration. |
Code example
#include <iostream>
#include <string>
void GetConnector()
{
const std::string projectName = "<your-project-name>";
const std::string topicName = "<your-topic-name>";
const std::string connectorId = "<your-connector-id>";
try {
// Query the connector and retrieve its configuration
GetConnectorResult getConnectorResult = client.GetConnector(
projectName, topicName, connectorId);
const sdk::SinkOdpsConfig* odpsConfig =
dynamic_cast<const sdk::SinkOdpsConfig*>(getConnectorResult.GetConfig());
std::cout << odpsConfig->GetPartitionConfig().size() << std::endl;
}
catch (const DatahubException& e)
{
std::cerr << "Failed to get connector: " << e.GetRequestId()
<< ", ErrorCode: " << e.GetErrorCode()
<< ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}
List connectors
List all connectors for a specified topic.
Parameters
| Name |
Type |
Description |
projectName |
string |
The name of the project. |
topicName |
string |
The name of the topic. |
Return value
| Type |
Description |
ListConnectorResult |
Call GetConnectorIds() on the result to retrieve the list of connector IDs. |
Code example
#include <iostream>
#include <string>
void ListConnector()
{
const std::string projectName = "<your-project-name>";
const std::string topicName = "<your-topic-name>";
try {
// List all connectors for the specified topic
const ListConnectorResult& listConnectorResult = client.ListConnector(
projectName, topicName);
std::cout << listConnectorResult.GetConnectorIds().size() << std::endl;
}
catch (const DatahubException& e)
{
std::cerr << "Failed to list connectors: " << e.GetRequestId()
<< ", ErrorCode: " << e.GetErrorCode()
<< ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}