Topic operation
This topic describes how to perform topic-related operations using the DataHub Java SDK.
Topic overview
A topic is the smallest unit for data subscription and publishing in DataHub. You can use a topic to represent a type or category of streaming data. DataHub currently supports two types of topics: Blob and Tuple.
A Blob topic allows you to write a block of binary data as a record.
A Tuple topic supports structured records similar to database rows. Each record consists of multiple columns. A record schema must be specified because data is transmitted over the network in string format, and the schema is required to parse it into the corresponding data types.
Supported Data Types:
Type | Description | Range |
BIGINT | 8-byte signed integer |
|
DOUBLE | 8-byte double-precision float |
|
BOOLEAN | Boolean type | Can be any of the following pairs:
|
TIMESTAMP | 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 |
|
INTEGER | 4-byte signed integer |
|
FLOAT | 4-byte single-precision float |
|
TINYINT, SMALLINT, INTEGER, and FLOAT are supported starting from Java SDK version 2.16.1-public.
Create a topic
Create a Tuple topic
Create a Blob Topic
Delete a topic
Before deleting a Topic, ensure that no subscriptions or connectors are associated with it. Otherwise, a NoPermissionException will be thrown.
Parameters
Name | Type | Description |
projectName | String | The project name. |
topicName | String | The topic name. |
Exception description
Class name | Error code | Description |
DatahubClientException | - | The base class for all exceptions. |
NoPermissionException |
| No permission. This is usually caused by incorrect RAM configuration or authorization. |
Code example
public static void deleteTopic(String projectName, String topicName) {
try {
datahubClient.deleteTopic(projectName, topicName);
System.out.println("delete topic successful");
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}List topics
Retrieves all topics under the specified project.
Parameters
Name | Type | Description |
projectName | String | The project name. |
Code example
public static void listTopic(String projectName ) {
try {
ListTopicResult listTopicResult = datahubClient.listTopic(projectName);
if (listTopicResult.getTopicNames().size() > 0) {
for (String tName : listTopicResult.getTopicNames()) {
System.out.println(tName);
}
}
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}Update a topic
Updates the lifecycle or description of a Topic.
Parameters
Name | Type | Description |
projectName | String | The project name. |
topicName | String | The topic name. |
comment | String | The topic description. |
lifeCycle | int | The topic lifecycle. |
Exceptions
Class name | Error code | Description |
DatahubClientException | - | The base class for all exceptions. |
Code example
public static void updateTopic(String projectName, String topicName, int lifeCycle, String comment) {
try {
comment = "new topic comment";
lifeCycle = 1;
datahubClient.updateTopic(projectName, Constant.topicName,lifeCycle, comment);
System.out.println("update topic successful");
//View the updated result
GetTopicResult getTopicResult = datahubClient.getTopic(projectName, topicName);
System.out.println(getTopicResult.getComment());
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}Query a topic
Retrieves topic metadata by project name and topic name.
Parameters
Name | Type | Description |
projectName | String | The project name. |
topicName | String | The topic name. |
Exceptions
Class name | Error code | Description |
DatahubClientException | - | The base class for all exceptions. |
Code example
public static void getTopic(String projectName, String topicName) {
try {
GetTopicResult getTopicResult = datahubClient.getTopic(projectName, topicName);
System.out.println(getTopicResult.getShardCount() + "\t"
+ getTopicResult.getLifeCycle() + "\t"
+ getTopicResult.getRecordType() + "\t"
+ getTopicResult.getComment());
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}