【问题标题】:MongoDB hosted in Azure Cosmos DB: Sharding vs partitioningAzure Cosmos DB 中托管的 MongoDB:分片与分区
【发布时间】:2018-11-30 17:44:16
【问题描述】:

我们希望将 MongoDB 用于我们的数据库,并且我们希望使用 MongoDB API 来避免“锁定”到 Azure Cosmos DB 托管。

我们使用 .Net Core 和 MongoDB.Driver 包(以便能够在本地、Atlas、Azure Cosmos hsoting 等之间轻松切换)与 MongoDB 实例通信,到目前为止一切顺利。

为了能够处理数据量(大小和性能)的未来增长,我希望对我的集合进行分片。据我了解,Cosmos DB 使用的策略是使用分区键进行分区,但由于我们使用 MongoDB.Driver,我无论如何都找不到在查询中指定分区键。

“普通”MongoDB 使用分片,您可以设置一个文档属性,该属性应用作数据分片方式的分隔符。

所以,我的猜测是分片是要走的路(因为 partionkeys 是 Cosmos 的一项功能),但我无法让它工作。

Azure 门户中的“MongoDB shell”不理解 sh.shardCollection 命令,如果我从客户端连接 MongoDB shell,我会收到以下错误:

globaldb:PRIMARY> use sampledatabase
switched to db sampledatabase
globaldb:PRIMARY> sh.shardCollection("sampledatabase.Event", { TenantId: 1 } )
2018-06-21T12:03:06.522+0200 E QUERY    [thread1] Error: not connected to a mongos :

如何继续在 Azure Cosmos 中托管的 MongoDB 实例中启动和运行分片?

【问题讨论】:

    标签: mongodb azure-cosmosdb partitioning sharding mongodb.driver


    【解决方案1】:

    后来我发现你可以使用 Microsoft.Azure.Documents.Client 创建分片集合。

    您必须使用时髦的语法 @"/'$v'/ShardingKey/'$v'" 才能使其工作。 然后,您可以在文档中使用名为 ShardingKey 的属性,该属性将与 MongoDB.Driver 库。

                            _client.CreateDocumentCollectionAsync(databaseUri,
                            new DocumentCollection
                            {
                                Id = documentCollection.Id,
                                PartitionKey =
                                    new PartitionKeyDefinition
                                    {
                                        Paths = new Collection<string> {@"/'$v'/ShardingKey/'$v'"}
                                    }
                            }, new RequestOptions {OfferThroughput = 1100}).Wait();
    

    参考https://blog.olandese.nl/2017/12/13/create-a-sharded-mongodb-in-azure-cosmos-db/

    【讨论】:

      【解决方案2】:

      CosmosDB Mongo api 端点公开了启用副本集的 MongoD 接口,而不是 MongoS 接口。因此,您需要使用 db.runCommand 而不是“sh”分片命令来创建分片集合。

      您可以在https://docs.microsoft.com/en-us/azure/cosmos-db/partition-data#mongodb-api找到更多详细信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 2019-08-05
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多