调用MergeShard接口合并Shard。
前提条件
参数说明
def merge_shard(self, project_name, logstore_name, shardId):
请求参数
参数名称 | 类型 | 是否必填 | 描述 | 示例值 |
project_name | String | 是 | Project名称。 | ali-test-project |
logstore_name | String | 是 | Logstore名称。 | test-logstore |
shardId | int | 是 | Shard ID。 重要 合并Shard时,需存在两个相邻位置的且状态为readwrite的Shard,shardId参数填入这两个相邻Shard中的第一个Shard的ID。合并后,这两个相邻Shard会变为readonly,合并后的新Shard的哈希区间为原相邻Shard的并集。 | 2 |
返回参数
返回参数说明,请参见MergeShard - 合并shard。
示例代码
from aliyun.log import LogClient
import os
def main():
# 本示例从环境变量中获取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"
project_name = "ali-test-project"
logstore_name = "xmltest"
shardId = 2
client = LogClient(endpoint, accessKeyId, accessKey, "")
res = client.merge_shard(project_name, logstore_name, shardId)
res.log_print()
if __name__ == '__main__':
main()
示例返回结果
合并Shard时,示例代码中指定了Shard2,日志服务自动找到Shard2右侧相邻的Shard3,并进行合并。合并完成后,原来两个Shard的状态由readwrite变为readonly。
合并Shard之前,Shard信息如下:
ListShardResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '668', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 30 Oct 2024 07:06:50 GMT', 'x-log-time': '1730272010', 'x-log-requestid': '6721DB0AE05BB30ACE5654AE'}
res:
[{'shardID': 2, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '40000000000000000000000000000000', 'createTime': 1730271813},
{'shardID': 3, 'status': 'readwrite', 'inclusiveBeginKey': '40000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1730271813},
{'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '80000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1716966695},
{'shardID': 0, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1716966695}]
Process finished with exit code 0
合并Shard之后,示例代码返回结果如下:
ListShardResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '500', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 30 Oct 2024 07:14:23 GMT', 'x-log-time': '1730272463', 'x-log-requestid': '6721DCCF4CCA1EE42CF839A5'}
res: [{'shardID': 4, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1730272463}, {'shardID': 2, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '40000000000000000000000000000000', 'createTime': 1730271813}, {'shardID': 3, 'status': 'readonly', 'inclusiveBeginKey': '40000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1730271813}]
Process finished with exit code 0
相关文档
调用CLI命令管理Shard请参见:
管理Shard的API请参见:
更多示例代码,请参见Aliyun Log Python SDK on GitHub。
日志服务使用Shard控制Logstore读写数据的能力。详细信息,请参见分区(Shard)。
文档内容是否对您有帮助?