すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:Simple Log Service SDK for Javaを使用したシャードの管理

最終更新日:Jun 24, 2025

シャードは、LogstoreまたはMetricstoreの読み取りおよび書き込み容量を管理するために使用されます。 Simple Log Serviceでは、データはシャードに保存されます。 このトピックでは、Simple Log Service SDK for Javaを使用してシャードをクエリ、分割、およびマージする方法とサンプルコードについて説明します。

前提条件

  • RAM (Resource Access Management) ユーザーが作成され、必要な権限がRAMユーザーに付与されます。 詳細については、「RAMユーザーの作成とRAMユーザーへの権限付与」をご参照ください。

  • ALIBABA_CLOUD_ACCESS_KEY_IDおよびALIBABA_CLOUD_ACCESS_KEY_SECRET環境変数が設定されています。 詳細については、「環境変数の設定」をご参照ください。

    重要
    • Alibaba CloudアカウントのAccessKeyペアには、すべてのAPI操作に対する権限があります。 RAMユーザーのAccessKeyペアを使用して、API操作を呼び出したり、ルーチンのO&Mを実行したりすることを推奨します。

    • プロジェクトコードにAccessKey IDまたはAccessKey secretを保存しないことを推奨します。 そうしないと、AccessKeyペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。

  • Simple Log Service SDK for Javaがインストールされています。 詳細については、「Simple Log Service SDK For Javaのインストール」をご参照ください。

  • Logstoreが作成されます。 詳細については、「Logstoreの作成に使用されるサンプルコード」をご参照ください。

  • 分割操作とマージ操作の場合、シャードステータスはreadwriteである必要があります。

使用上の注意

この例では、中国 (杭州) リージョンのパブリックSimple Log Serviceエンドポイントが使用されています。これは https://cn-hangzhou.log.aliyuncs.com です。 プロジェクトと同じリージョンにある他のAlibaba Cloudサービスを使用してSimple Log Serviceにアクセスする場合は、内部のSimple Log Serviceエンドポイント ( https://cn-hangzhou-intranet.log.aliyuncs.com ) を使用できます。 Simple Log Serviceのサポートされているリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。

シャードのクエリに使用されるサンプルコード

次のサンプルコードは、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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The name of the Logstore. 
        String logstoreName = "ali-test-logstore";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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 of the 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;
        }
    }
}

期待される結果:

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

シャードを分割するために使用されるサンプルコード

次のサンプルコードは、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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The name of the Logstore. 
        String logstoreName = "ali-test-logstore";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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 splitting. 
            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 before you query shards again. 
            TimeUnit.SECONDS.sleep(60);

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

            // Query all shards after splitting. 
            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

シャードのマージに使用されるサンプルコード

次のサンプルコードは、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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The name of the Logstore. 
        String logstoreName = "ali-test-logstore";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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 merging. 
            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. Simple Log Service automatically identifies the neighbor shard for merging. 
            client.MergeShards(projectName, logstoreName, 2);

            // Wait for 60 seconds before you query shards again. 
            TimeUnit.SECONDS.sleep(60);

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

            // Query all shards after merging. 
            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

関連ドキュメント

  • APIを呼び出した後、Log Serviceによって返された応答にエラー情報が含まれている場合、呼び出しは失敗します。 API呼び出しが失敗したときに返されるエラーコードに基づいてエラーを処理できます。 詳細については、エラーコードをご参照ください。

  • Alibaba Cloud OpenAPI Explorerは、デバッグ機能、SDK、サンプル、および関連ドキュメントを提供します。 OpenAPI Explorerを使用して、リクエストを手動でカプセル化したり署名したりすることなく、Log Service API操作をデバッグできます。 詳細については、をご覧ください。 OpenAPIポータル

  • Log Serviceは、Log Serviceの自動設定の要件を満たすコマンドラインインターフェイス (CLI) を提供します。 詳細については、「Log Service CLI」をご参照ください。

  • シャード関連の操作の詳細については、以下のトピックを参照してください。

  • サンプルコードの詳細については、GitHubの「Alibaba Cloud Log Service SDK For Java」をご参照ください。