全部產品
Search
文件中心

Simple Log Service:使用Java SDK管理Shard

更新時間:Jan 10, 2026

Log Service使用Shard控制LogStore或MetricStore讀寫資料的能力,資料必定儲存在某一個Shard中。本文通過程式碼範例介紹如何查詢、分裂、合并Shard。

前提條件

您已完成以下操作:

  • 已安裝Log ServiceJava SDK。具體操作,請參見安裝Java SDK

  • 已建立Project、標準型Logstore。具體操作,請參見管理Project建立基礎LogStore

  • 分裂Shard、合并Shard需要該Shard狀態為readwrite,Shard狀態為readonly分裂或者合并會報錯。

注意事項

本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為https://cn-hangzhou.log.aliyuncs.com

如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com

關於Log Service支援的地區與Endpoint的對應關係,請參見服務入口

查詢Shard範例程式碼

以下代碼用於查詢指定LogStore的Shard資訊。

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 {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 輸入LogStore名稱。
        String logstoreName = "ali-test-logstore";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

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

            // 查詢指定LogStore的所有Shard。
            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;
        }
    }
}

預期結果如下:

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

分裂Shard範例程式碼

以下代碼用於分裂指定LogStore的Shard。

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 {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 輸入LogStore名稱。
        String logstoreName = "ali-test-logstore";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

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

            // 分裂之前先查看所有Shard。
            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");
            // 分裂Shard 0。
            client.SplitShard(projectName, logstoreName, 0, "3f000000000000000000000000000000");

            // 等待60秒後再查詢Shard。
            TimeUnit.SECONDS.sleep(60);

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

            // 分裂之後再查看所有Shard。
            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();
        }
    }
}

預期結果如下:

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

合并Shard範例程式碼

以下代碼用於合并指定LogStore的Shard。

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 {
        // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 輸入Project名稱。
        String projectName = "ali-test-project";
        // 輸入LogStore名稱。
        String logstoreName = "ali-test-logstore";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        Client client = new Client(host, accessId, accessKey);

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

            // 合并之前先查看所有Shard。
            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");
            // 將Shard 2和3進行合并。服務端會自動找到相鄰的下一個Shard進行合并。
            client.MergeShards(projectName, logstoreName, 2);

            // 等待60秒後再查詢Shard。
            TimeUnit.SECONDS.sleep(60);

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

            // 合并之後再查看所有Shard。
            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();
        }
    }
}

預期結果如下:

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

相關文檔