使用Java SDK管理Shard

日志服务使用Shard控制Logstore或MetricStore读写数据的能力,数据必定保存在某一个Shard中。本文通过代码示例介绍如何查询、分裂、合并Shard。

前提条件

  • 已开通日志服务。更多信息,请参见开通日志服务

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见在Linux、macOS和Windows系统配置环境变量

    重要
    • 阿里云账号的AccessKey拥有所有API的访问权限,建议您使用RAM用户的AccessKey进行API访问或日常运维。

    • 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • 已安装日志服务Java SDK。具体操作,请参见安装Java SDK

  • 已创建Project、标准型Logstore。具体操作,请参见创建项目Project创建Logstore

  • 分裂Shard、合并Shard需要该Shard状态为readwrite,Shard状态为readonly分裂或者合并会报错。

注意事项

本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。关于日志服务支持的地域与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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 创建日志服务Client。
        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";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 创建日志服务Client。
        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

相关文档