Shard operations
Use the DataHub Go SDK to list, split, and merge shards.
Shard description
The number of shards in a topic determines its concurrent data transmission capability. Each shard has a unique ID and can exist in one of several states:
-
Opening: The shard is starting up.
-
Active: The shard is started and ready to provide services.
Each enabled shard consumes server resources. Request shards based on your actual needs.
Shards can be merged and split. When data volume increases, split shards to create more data channels and improve concurrent write operations. When data volume decreases, merge shards to reduce server resource consumption. For example, during the Double 11 Shopping Festival, data volume increases dramatically and the write pressure on each shard becomes too high, so you can increase the number of shards to improve write efficiency. After the festival, when data volume decreases significantly, you need to merge shards.
List shards
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The name of the project. |
|
topicName |
string |
The name of the topic |
Response example
1. type SplitShardResult struct {
2. NewShards []ShardEntry `json:"NewShards"`
3. }
Errors
|
Error class name |
Error code |
Description |
|
ResourceNotFoundError |
|
The specified resource does not exist. This exception may also be thrown if you send requests immediately after a split or merge operation. |
|
AuthorizationFailedError |
|
Authorization signature parsing failed. Verify that your AccessKey is correct. |
|
DatahubClientError |
- |
The base class for all other errors and exceptions. |
|
InvalidParameterError |
|
A parameter is set to an invalid value. |
Code example
func ExampleDataHub_ListShard() {
ls, err := dh.ListShard(projectName, topicName)
if err != nil {
fmt.Println("get shard list failed")
fmt.Println(err)
return
}
fmt.Println("get shard list successful")
for _, shard := range ls.Shards {
fmt.Println(shard)
}
}
Split shards
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The name of the project. |
|
topicName |
string |
The name of the topic. |
|
shardId |
string |
The shard to split. |
|
splitKey |
string |
The split key used to split the shard. |
Response example
type SplitShardResult struct {
NewShards []ShardEntry `json:"NewShards"`
}
Errors
|
Error class name |
Error code |
Description |
|
ResourceNotFoundError |
|
The specified resource does not exist. This exception may also be thrown if you send requests immediately after a split or merge operation. |
|
AuthorizationFailedError |
|
Authorization signature parsing failed. Verify that your AccessKey is correct. |
|
DatahubClientError |
- |
The base class for all other errors and exceptions. |
|
InvalidParameterError |
|
A parameter is set to an invalid value. |
Code example
func ExampleDataHub_SplitShard() {
// the shardId of you want to split
shardId := "0"
ss, err := dh.SplitShard(projectName, topicName, shardId)
if err != nil {
fmt.Println("split shard failed")
fmt.Println(err)
return
}
fmt.Println("split shard successful")
fmt.Println(ss)
// After splitting, you need to wait for all shard states to be ready
// before you can perform related operations.
dh.WaitAllShardsReady(projectName, topicName)
}
Merge shards
Parameters
|
Parameter name |
Parameter type |
Description |
|
projectName |
String |
The name of the project. |
|
topicName |
string |
The name of the topic. |
|
shardId |
string |
The shard to merge. |
|
adjacentShardId |
string |
The adjacent shard of the specified shard. |
Response example
type SplitShardResult struct {
NewShards []ShardEntry `json:"NewShards"`
}
Errors
|
Error class name |
Error code |
Description |
|
ResourceNotFoundError |
|
The specified resource does not exist. This exception may also be thrown if you send requests immediately after a split or merge operation. |
|
InvalidOperationError |
- |
- |
|
AuthorizationFailedError |
|
Authorization signature parsing failed. Verify that your AccessKey is correct. |
|
InvalidParameterError |
|
One or more parameters are invalid. |
|
ShardSealedError |
- |
- |
Code example
func ExampleDataHub_MergeShard() {
shardId := "3"
adjacentShardId := "4"
ms, err := dh.MergeShard(projectName, topicName, shardId, adjacentShardId)
if err != nil {
fmt.Println("merge shard failed")
fmt.Println(err)
return
}
fmt.Println("merge shard successful")
fmt.Println(ms)
// After splitting, you need to wait for all shard states to be ready
// before you can perform related operations.
dh.WaitAllShardsReady(projectName, topicName)
}