Manage shards using the Java SDK

更新时间:
复制 MD 格式

Simple Log Service uses shards to manage the read and write capacity of Logstores and Metricstores. All data is stored in shards. You can query, split, and merge shards by using the Java SDK.

Prerequisites

  • Simple Log Service SDK for Java is installed. For more information, see Install the Java SDK.

  • A project and a Standard logstore are created. For more information, see Manage projects and Create a basic Logstore.

  • To split or merge a shard, its status must be read-write. Splitting or merging a read-only shard results in an error.

Precautions

In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.

If you want to access Simple Log Service from other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com.

For more information about the supported regions and endpoints of Simple Log Service, see Endpoint.

Query shards

The following code queries the shards in a specified Logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Shard;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListShardResponse;

public class ListShards {
    public static void main(String[] args) throws LogException {
        // This example obtains the AccessKey ID and AccessKey secret from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name.
        String projectName = "ali-test-project";
        // Enter the Logstore name.
        String logstoreName = "ali-test-logstore";
        // Set the endpoint for Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            System.out.println("ready to list shards");

            // Query all shards in the specified Logstore.
            ListShardResponse response = client.ListShard(projectName, logstoreName);

            for(Shard shard : response.GetShards()){
                System.out.println("Shard ID is :" + shard.getShardId());
                System.out.println("Shard status is :" + shard.getStatus());
                System.out.println("Shard begin key is :" + shard.getInclusiveBeginKey());
                System.out.println("Shard end key is :" + shard.getExclusiveEndKey());
                System.out.println("Shard create time is :" + shard.getCreateTime());
            }

            System.out.println(String.format("list shards of %s success", logstoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

The following result is expected:

ready to list shards
Shard ID is :0
Shard status is :readwrite
Shard begin key is :00000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
Shard create time is :1667810747
Shard ID is :1
Shard status is :readwrite
Shard begin key is :7f000000000000000000000000000000
Shard end key is :ffffffffffffffffffffffffffffffff
Shard create time is :1667810747
list shards of ali-test-logstore success

Split a shard

The following code splits a shard in a specified Logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Shard;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListShardResponse;

import java.util.concurrent.TimeUnit;

public class SplitShard {
    public static void main(String[] args) throws LogException {
        // This example obtains the AccessKey ID and AccessKey secret from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name.
        String projectName = "ali-test-project";
        // Enter the Logstore name.
        String logstoreName = "ali-test-logstore";
        // Set the endpoint for Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            System.out.println("ready to list shard");

            // Query all shards before the split.
            ListShardResponse response = client.ListShard(projectName, logstoreName);
            for(Shard shard : response.GetShards()){
                System.out.println("Shard ID is :" + shard.getShardId());
                System.out.println("Shard status is :" + shard.getStatus());
                System.out.println("Shard begin key is :" + shard.getInclusiveBeginKey());
                System.out.println("Shard end key is :" + shard.getExclusiveEndKey());
            }
            System.out.println("ready to split shard");
            // Split shard 0.
            client.SplitShard(projectName, logstoreName, 0, "3f000000000000000000000000000000");

            // Wait for 60 seconds and then query the shards.
            TimeUnit.SECONDS.sleep(60);

            System.out.println(String.format("split shards of %s success", logstoreName));

            // Query all shards after the split.
            ListShardResponse response1 = client.ListShard(projectName, logstoreName);
            for(Shard shard1 : response1.GetShards()){
                System.out.println("Shard ID is :" + shard1.getShardId());
                System.out.println("Shard status is :" + shard1.getStatus());
                System.out.println("Shard begin key is :" + shard1.getInclusiveBeginKey());
                System.out.println("Shard end key is :" + shard1.getExclusiveEndKey());
            }
        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

The following result is expected:

ready to list shard
Shard ID is :0
Shard status is :readwrite
Shard begin key is :00000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
Shard ID is :1
Shard status is :readwrite
Shard begin key is :7f000000000000000000000000000000
Shard end key is :ffffffffffffffffffffffffffffffff
ready to split shard
split shards of ali-test-logstore success
Shard ID is :2
Shard status is :readwrite
Shard begin key is :00000000000000000000000000000000
Shard end key is :3f000000000000000000000000000000
Shard ID is :3
Shard status is :readwrite
Shard begin key is :3f000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
Shard ID is :1
Shard status is :readwrite
Shard begin key is :7f000000000000000000000000000000
Shard end key is :ffffffffffffffffffffffffffffffff
Shard ID is :0
Shard status is :readonly
Shard begin key is :00000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000

Merge shards

The following code merges shards in a specified Logstore.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Shard;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListShardResponse;

import java.util.concurrent.TimeUnit;

public class MergeShard {
    public static void main(String[] args) throws LogException {
        // This example obtains the AccessKey ID and AccessKey secret from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Enter the project name.
        String projectName = "ali-test-project";
        // Enter the Logstore name.
        String logstoreName = "ali-test-logstore";
        // Set the endpoint for Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            System.out.println("ready to list shard");

            // Query all shards before the merge.
            ListShardResponse response = client.ListShard(projectName, logstoreName);
            for(Shard shard : response.GetShards()){
                System.out.println("Shard ID is :" + shard.getShardId());
                System.out.println("Shard status is :" + shard.getStatus());
                System.out.println("Shard begin key is :" + shard.getInclusiveBeginKey());
                System.out.println("Shard end key is :" + shard.getExclusiveEndKey());
            }
            System.out.println("ready to merge shard");
            // Merge shard 2 and shard 3. The server automatically finds the contiguous shard to merge.
            client.MergeShards(projectName, logstoreName, 2);

            // Wait for 60 seconds and then query the shards.
            TimeUnit.SECONDS.sleep(60);

            System.out.println(String.format("merge shards of %s success", logstoreName));

            // Query all shards after the merge.
            ListShardResponse response1 = client.ListShard(projectName, logstoreName);
            for(Shard shard1 : response1.GetShards()){
                System.out.println("Shard ID is :" + shard1.getShardId());
                System.out.println("Shard status is :" + shard1.getStatus());
                System.out.println("Shard begin key is :" + shard1.getInclusiveBeginKey());
                System.out.println("Shard end key is :" + shard1.getExclusiveEndKey());
            }
        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

The following result is expected:

ready to list shard
Shard ID is :2
Shard status is :readwrite
Shard begin key is :00000000000000000000000000000000
Shard end key is :3f000000000000000000000000000000
Shard ID is :3
Shard status is :readwrite
Shard begin key is :3f000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
Shard ID is :1
Shard status is :readwrite
Shard begin key is :7f000000000000000000000000000000
Shard end key is :ffffffffffffffffffffffffffffffff
Shard ID is :0
Shard status is :readonly
Shard begin key is :00000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
ready to merge shard
merge shards of ali-test-logstore success
Shard ID is :4
Shard status is :readwrite
Shard begin key is :00000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
Shard ID is :1
Shard status is :readwrite
Shard begin key is :7f000000000000000000000000000000
Shard end key is :ffffffffffffffffffffffffffffffff
Shard ID is :0
Shard status is :readonly
Shard begin key is :00000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000
Shard ID is :2
Shard status is :readonly
Shard begin key is :00000000000000000000000000000000
Shard end key is :3f000000000000000000000000000000
Shard ID is :3
Shard status is :readonly
Shard begin key is :3f000000000000000000000000000000
Shard end key is :7f000000000000000000000000000000

References