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

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

最終更新日:Jun 24, 2025

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

前提条件

  • 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ペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。

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

  • 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のシャードをクエリする方法の例を示しています。

aliyun.log import LogClientからの

from aliyun.log import LogClient
import os

# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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. 
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client. 
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project. 
project_name = "ali-test-project"

# The name of the Logstore. 
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # Query all shards of the specified Logstore. 
    print("ready to list shards")

    res = client.list_shards(project_name, logstore_name)

    print("Shards info is :%s" % res.get_shards_info())

    print("list shards success")

期待される結果

ready to list shards
Shards info is :[{'shardID': 0, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1670999677}, {'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '7f000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1670999677}]
list shards success

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

Simple Log Serviceコンソールでシャードを簡単に分割およびマージできます。 詳細については、「シャードの分割」をご参照ください。

次のサンプルコードは、指定されたLogstoreのシャードを分割する方法の例を示しています。

import time
import os
from aliyun.log import LogClient

# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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. 
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client. 
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project. 
project_name = "ali-test-project"

# The name of the Logstore. 
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # Query shards before you split a shard. 
    res1 = client.list_shards(project_name, logstore_name)
    print("Before splite shard, Shards info is :%s" % res1.get_shards_info())

    # Split Shard 0. 
    print("ready to split shards")
    client.split_shard(project_name, logstore_name, shardId=0, split_hash="3f000000000000000000000000000000")

    # Query the split results after 60 seconds. 
    time.sleep(60)
    res2 = client.list_shards(project_name, logstore_name)
    print("After split Shards, Shards info is :%s" % res2.get_shards_info())

    print("list shards success")

期待される結果

Before splite shard, Shards info is :[{'shardID': 0, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1670999677}, {'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '7f000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1670999677}]
ready to split shards
After split Shards, Shards info is :[{'shardID': 2, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '3f000000000000000000000000000000', 'createTime': 1672042813}, {'shardID': 3, 'status': 'readwrite', 'inclusiveBeginKey': '3f000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1672042813}, {'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '7f000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1670999677}, {'shardID': 0, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1670999677}]
list shards success

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

Simple Log Serviceコンソールでシャードを簡単に分割およびマージできます。 詳細については、「シャードのマージ」をご参照ください。

次のサンプルコードは、指定したLogstoreのシャードをマージする方法の例を示しています。

import time
import os
from aliyun.log import LogClient

# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 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. 
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client. 
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project. 
project_name = "ali-test-project"

# The name of the Logstore. 
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # Query shards before you merge shards. 
    res1 = client.list_shards(project_name, logstore_name)
    print("Before merge shard, Shards info is :%s" % res1.get_shards_info())

    # Merge Shard 2 and Shard 3. Simple Log Service automatically identifies the neighbor shard for merging. 
    print("ready to merge shards")
    client.merge_shard(project_name, logstore_name, shardId=2)

    # Query the merge results after 60 seconds. 
    time.sleep(60)
    res2 = client.list_shards(project_name, logstore_name)
    print("After merge Shards, Shards info is :%s" % res2.get_shards_info())

    print("merge shards success")

期待される結果

Before merge shard, Shards info is :[{'shardID': 2, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '3f000000000000000000000000000000', 'createTime': 1672042813}, {'shardID': 3, 'status': 'readwrite', 'inclusiveBeginKey': '3f000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1672042813}, {'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '7f000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1670999677}, {'shardID': 0, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1670999677}]
ready to merge shards
After merge Shards, Shards info is :[{'shardID': 4, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1672043611}, {'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '7f000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1670999677}, {'shardID': 0, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1670999677}, {'shardID': 2, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '3f000000000000000000000000000000', 'createTime': 1672042813}, {'shardID': 3, 'status': 'readonly', 'inclusiveBeginKey': '3f000000000000000000000000000000', 'exclusiveEndKey': '7f000000000000000000000000000000', 'createTime': 1672042813}]
merge shards success

コンソールでシャードを表示する

  1. Simple Log Serviceコンソール.

  2. [プロジェクト] セクションで、管理するプロジェクトをクリックします。

    image

  3. [ログストレージ] > [ログストア] タブで、管理するログストアをクリックします。 次に、修改日志库 > 変更を選択します。

  4. ページの右側にあるシャード情報を表示します。

    image

関連ドキュメント

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

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

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

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