Topic operations
Perform topic operations by using the DataHub C++ SDK, including creating, deleting, listing, updating, and querying topics.
Topic description
Topic is the smallest unit for subscription and publishing in DataHub. A topic represents a class or type of stream data. DataHub supports two types of topics: Tuple and Blob:
Blob type topics support writing a block of binary data as a record.
Tuple type topics support data similar to database records, where each record contains multiple columns. You must specify a Record Schema because data is transmitted in string format and requires a schema for type conversion. The following data types are supported:
Type
Description
Range
BIGINT
8-byte signed integer
-9223372036854775808 – 9223372036854775807DOUBLE
8-byte double-precision float
-1.0 × 10^308 – 1.0 × 10^308BOOLEAN
Boolean type
Can be any of the following pairs:
True/False
true/false
0/1
TIMESTAMP
Timestamp type
A timestamp with microsecond precision.
STRING
UTF-8 encoded string
Max 2 MB per column
TINYINT
1-byte signed integer
-128 – 127
SMALLINT
2-byte signed integer
-32768 – 32767INTEGER
4-byte signed integer
-2147483648 – 2147483647FLOAT
4-byte single-precision float
-3.40282347 × 10^38 – 3.40282347 × 10^38
Create a topic
Create a tuple topic
Parameters
Parameter | Type | Description |
fieldName | string | The field name. |
fieldComment | string | The field description. |
Sample code
void CreateTupleTopic(){
RecordSchema schema;
std::string fieldName1 = "a";
std::string fieldName2 = "b";
std::string fieldName3 = "c";
std::string fieldComment1 = "field1 comment";
std::string fieldComment2 = "field2 comment";
std::string fieldComment3 = "field3 comment";
schema.AddField(Field(fieldName1/*Fieldname*/, BIGINT, true, fieldComment1));
schema.AddField(Field(fieldName2/*Fieldname*/, DOUBLE, true, fieldComment2));
schema.AddField(Field(fieldName3/*Fieldname*/, STRING, true, fieldComment3));
/* Create Topic */
int shardCount = 3;
int lifeCycle = 7;
RecordType type = TUPLE;
std::string projectName = "";
std::string topicName = "";
try
{
client.CreateTopic(projectName, topicName, shardCount, lifeCycle, type, schema, comment);
}
catch (const DatahubException& e)
{
std::cerr << "Create topic fail: " << e.GetRequestId() << ", ErrorCode: " << e.GetErrorCode() << ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}Create a blob topic
Parameters
Parameter | Type | Description |
projectName | string | The project name. |
topicName | string | The topic name. |
Sample code
void CreateBlobTopic()
{
/* Create Topic */
int shardCount = 3;
int lifeCycle = 7;
RecordType type = BLOB;
std::string projectName = "";
std::string topicName = "";
try
{
client.CreateTopic(projectName, topicName, shardCount, lifeCycle, type, comment);
}
catch (const DatahubException& e)
{
std::cerr << "Create topic fail: " << e.GetRequestId() << ", ErrorCode: " << e.GetErrorCode() << ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}
}Delete a topic
Parameters
Parameter | Type | Description |
projectName | string | The project name. |
topicName | string | The topic name. |
Sample code
void DeleteTopic()
{
std::string projectName = "";
std::string topicName = "";
try
{
client.DeleteTopic(projectName, topicName);
}
catch(const DatahubException& e)
{
std::cerr << "Delete topic fail: " << e.GetRequestId() << ", ErrorCode: " << e.GetErrorCode() << ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}List topics
Parameters
Parameter | Type | Description |
projectName | string | The project name. |
Sample code
void ListTopic(){
std::string projectName = "";
try
{
const ListTopicResult& listTopicResult = client.ListTopic(projectName);
std::cout<<listTopicResult.GetTopicNames().size()<<std::endl;
}
catch(const DatahubException& e)
{
std::cerr << "Get topic fail: " << e.GetRequestId() << ", ErrorCode: " << e.GetErrorCode() << ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}Update a topic
Parameters
Parameter | Type | Description |
updateComment | string | The updated description. |
updateLifecycle | int | The updated lifecycle. |
projectName | string | The project name. |
topicName | string | The topic name. |
Sample code
void UpdateTopic(){
const std::string updateComment = "test1";
int updateLifecycle = 7;
std::string projectName = "";
std::string topicName = "";
try
{
client.UpdateTopic(projectName, topicName, updateLifecycle, updateProjectComment);
}
catch(const DatahubException& e)
{
std::cerr << "Update topic fail: " << e.GetRequestId() << ", ErrorCode: " << e.GetErrorCode() << ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}Query a topic
Parameters
Parameter | Type | Description |
projectName | string | The project name. |
topicName | string | The topic name. |
Sample code
void GetTopic(){
std::string projectName = "";
std::string topicName = "";
try
{
const GetTopicResult& getTopicResult = client.GetTopic(projectName, topicName);
cout<<getTopicResult.GetComment()<<endl;
}
catch(const DatahubException& e)
{
std::cerr << "Get topic fail: " << e.GetRequestId() << ", ErrorCode: " << e.GetErrorCode() << ", ErrorMessage: " << e.GetErrorMessage() << std::endl;
}
}