分片时间序列集合

5.1版中的新功能。

使用本教程对新的或现有的时间序列集合进行分片。

IMPORTANT

在完成本教程之前,请查看时间序列集合的分片限制

限制img

您不能重新将分片时间序列集合分片。但是,您可以优化其 shard key 。

先决条件img

要对时间序列集合进行分片,您必须部署一个分片集群来托管包含您的时间序列集合的数据库。

程序img

分片新的时间序列集合img

连接到您的分片集群。img

连接mongoshmongos您的分片集群。指定hostport在其 mongos上运行:

mongosh --host <hostname> --port <port>

确认您的数据库已启用分片。img

运行sh.status()以确认您的数据库已启用分片:

sh.status()

该命令返回分片信息:

--- Sharding Status ---
   sharding version: {
      "_id" : 1,
      "minCompatibleVersion" : 5,
      "currentVersion" : 6,
...

创建集合。img

使用shardCollection()带有timeseries选项的方法。

例如:

sh.shardCollection(
   "test.weather",
   { "metadata.sensorId": 1 },
   {
      timeseries: {
         timeField: "timestamp",
         metaField: "metadata",
         granularity: "hours"
      }
   }
)

在这个例子中,sh.shardCollection()

  • weathertest数据库中命名的新时间序列集合进行分片。
  • metadata.sensorId将字段指定为shard key 。
  • 指定granularity小时数。

以下文档包含该集合的适当元数据:

db.weather.insertOne( {
   "metadata": { "sensorId": 5578, "type": "temperature" },
   "timestamp": ISODate("2021-05-18T00:00:00.000Z"),
   "temp": 12
} )

分片现有时间序列集合img

连接到您的分片集群。img

连接mongoshmongos您的分片集群。指定hostport在其 mongos上运行:

mongosh --host <hostname> --port <port>

确认您的数据库已启用分片。img

运行sh.status()以确认您的数据库已启用分片:

sh.status()

该命令返回分片信息:

--- Sharding Status ---
   sharding version: {
      "_id" : 1,
      "minCompatibleVersion" : 5,
      "currentVersion" : 6,
...

分片集合。img

使用shardCollection()方法对集合进行分片。

考虑具有以下属性的时间序列集合:

db.createCollection(
   "deliverySensor",
   {
      timeseries: {
         timeField: "timestamp",
         metaField: "metadata",
         granularity: "minutes"
      }
   }
)

该集合中的示例文档类似于:

db.deliverySensor.insertOne( {
   "metadata": { "location": "USA", "vehicle": "truck" },
   "timestamp": ISODate("2021-08-21T00:00:10.000Z"),
   "speed": 50
} )

要对集合进行分片,请运行以下命令:

sh.shardCollection( "test.deliverySensor", { "metadata.location": 1 } )

在这个例子中,sh.shardCollection()

  • deliverySensortest数据库中命名的现有时间序列集合进行分片。
  • metadata.location将字段指定为shard keylocation是集合的子字段 metaField

当您指定的集合sh.shardCollection()是时间序列集合时,您不需要指定 timeseries 选项。

附加信息img

在时间序列数据之上构建物化视图时间序列集合的最佳实践

原文链接-https://www.mongodb.com/docs/manual/core/timeseries/timeseries-shard-collection/

译者:陆文龙

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""