赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 日志服务
  4. 开发参考
  5. 日志服务SDK
  6. Python SDK
  7. 资源管理
  8. 使用Python SDK管理Shard

使用Python SDK管理Shard

  • 资源管理
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

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

前提条件

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

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

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

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

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

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

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

  • 分裂Shard、合并Shard需要该Shard状态为readwrite。

注意事项

本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口。

计费说明

Shard计费说明请参见Shard租用费用。

查询Shard示例代码

以下代码用于查询指定Logstore的Shard信息。

from aliyun.log import LogClient
import os

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"

# Logstore名称。
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # 查询指定Logstore的所有Shard。
    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

分裂Shard示例代码

控制台界面支持界面化分裂合并Shard,操作更便捷。具体操作,请参见分裂Shard。

以下代码用于分裂指定Logstore的Shard。

import time
import os
from aliyun.log import LogClient

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"

# Logstore名称。
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # 分裂之前查询Shard。
    res1 = client.list_shards(project_name, logstore_name)
    print("Before splite shard, Shards info is :%s" % res1.get_shards_info())

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

    # 等待60秒后再查询Shard分裂结果。
    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

合并Shard示例代码

控制台界面支持界面化分裂合并Shard,操作更便捷。具体操作,请参见合并Shard。

以下代码用于合并指定Logstore的Shard。

import time
import os
from aliyun.log import LogClient

# 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
# 创建日志服务Client。
client = LogClient(endpoint, accessKeyId, accessKey)

# Project名称。
project_name = "ali-test-project"

# Logstore名称。
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # 合并之前查询Shard。
    res1 = client.list_shards(project_name, logstore_name)
    print("Before merge shard, Shards info is :%s" % res1.get_shards_info())

    # 将Shard 2和3进行合并。服务端会自动找到相邻的下一个Shard进行合并。
    print("ready to merge shards")
    client.merge_shard(project_name, logstore_name, shardId=2)

    # 等待60秒后再查询Shard合并结果。
    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

控制台查看Shard

  1. 登录日志服务控制台。

  2. 在Project列表区域,单击目标Project。

    image

  3. 在日志存储 > 日志库页签中,将鼠标悬浮在目标Logstore上,然后选择修改日志库 > 修改

    mergeshard

相关文档

  • 阿里云OpenAPI开发者门户提供调试、SDK、示例和配套文档。通过OpenAPI,您无需手动封装请求和签名操作,就可以快速对日志服务API进行调试。更多信息,请参见OpenAPI开发者门户。

  • 为满足越来越多的自动化日志服务配置需求,日志服务提供命令行工具CLI(Command Line Interface)。更多信息,请参见日志服务命令行工具CLI。

  • 关于Shard API接口说明,请参见如下:

    • ListShards - 查询shard列表

    • SplitShard - 分裂Shard

    • MergeShard - 合并shard

  • 更多示例代码,请参见Aliyun Log Python SDK on GitHub。

相关文章

使用Python SDK管理项目Project 2025-04-22 10:34

项目(Project)是日志服务的资源管理单元,包含Logstore、MetricStore和机器组等资源,同时也是您访问日志服务资源的入口。本文通过代码示例介绍如何创建、修改、查询、删除Project等。 前提条件

使用Python SDK管理日志库Logstore 2025-04-22 10:34

日志库(Logstore)是日志服务中数据的采集、存储和查询单元。每个Logstore隶属于一个Project,每个Project中可创建多个Logstore。本文通过代码示例介绍如何创建、修改、查询、删除Logstore等。 前提条件

使用Python SDK创建分层存储Logstore 2025-04-22 10:34

本文介绍通过Python SDK创建分层存储Logstore的代码示例。 前提条件

使用Python SDK管理Shard 2025-04-22 10:34

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

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号