Use the DataHub Java SDK to list, split, merge, and extend shards for a topic.
Shard description
Shards determine a topic's concurrent data transmission capability. Each shard has a unique ID and can be in one of the following states:
-
Opening: The shard is starting up.
-
Active: The shard is ready to provide services.
Each enabled shard consumes server resources. Request only the number of shards you need.
List shards
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The project name. |
|
topicName |
String |
The topic name. |
Exceptions
|
Exception class name |
Error code |
Description |
|
DatahubClientException |
- |
Base class for all exceptions. |
Sample code
public static void listShard(String projectName, String topicName) {
try {
ListShardResult listShardResult = datahubClient.listShard(projectName, topicName);
if (listShardResult.getShards().size() > 0) {
for (ShardEntry entry : listShardResult.getShards()) {
System.out.println(entry.getShardId() + "\t"
+ entry.getState() + "\t"
+ entry.getLeftShardId() + "\t"
+ entry.getRightShardId());
}
}
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}
Split shard
Splits an ACTIVE shard into two new ACTIVE shards. The original shard changes to CLOSED state and becomes read-only. You can use the default splitKey or specify a custom one.
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The project name. |
|
topicName |
String |
The topic name. |
|
shardId |
String |
The ID of the shard to split. |
|
splitKey |
String |
The split key used to split the shard. |
Exceptions
|
Exception class name |
Error code |
Description |
|
DatahubClientException |
- |
Base class for all exceptions. |
Sample code
public static void splitShard(String projectName, String topicName, String shardId) {
try {
shardId = "0";
SplitShardResult splitShardResult = datahubClient.splitShard(projectName, topicName, shardId);
for (ShardEntry entry : splitShardResult.getNewShards()) {
System.out.println(entry.getShardId());
}
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}
Merge shards
Merges two adjacent ACTIVE shards into one. Call listShard to identify which shards are adjacent and available for merging.
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The project name. |
|
topicName |
String |
The topic name. |
|
shardId |
String |
The ID of the shard to merge. |
|
adjacentShardId |
String |
The adjacent shard of the specified shard. |
Exceptions
|
Exception class name |
Error code |
Description |
|
DatahubClientException |
- |
Base class for all exceptions. |
Sample code
public static void mergeShard() {
try {
String shardId = "7";
//adjacentShardId must be contiguous with shardId. You can check the contiguous shard information in the listShard result
String adjacentShardId = "8";
MergeShardResult mergeShardResult = datahubClient.mergeShard(Constant.projectName, Constant.topicName, shardId, adjacentShardId);
System.out.println("merge successful");
System.out.println(mergeShardResult.getShardId());
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}
Extend shards
Increases the shard count for a topic. The new count must be greater than or equal to the current count.
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The project name. |
|
topicName |
String |
The topic name. |
|
shardCount |
int |
The target number of shards. |
|
adjacentShardId |
String |
The adjacent shard of the specified shard. |
Exceptions
|
Exception class name |
Error code |
Description |
|
DatahubClientException |
- |
Base class for all exceptions. |
Sample code
public static void extendTopic(String projectName, String topicName, int shardCount) {
try {
ExtendShardResult extendShardResult = datahubClient.extendShard(projectName, topicName, shardCount);
} catch (DatahubClientException e) {
System.out.println(e.getErrorMessage());
}
}