繁体   English   中英

如何从 cosmos db 中的现有表访问分区键

[英]How to access partition key from an existing table in cosmos db

我正在尝试仅访问 cosmos db 中现有集合的分区键我已经通过官方文档和 git hub 链接但没有运气。 请帮我。 注意:我可以通过使用 Document Client(SQL api) 成功地使用 java 读写数据

请尝试以下代码:

DocumentClient dClient = new DocumentClient(endPoint,primary_key,null,null);
String collectionLink = String.format("/dbs/%s/colls/%s", databaseId, collectionId);
ResourceResponse<DocumentCollection> response = dClient.readCollection(collectionLink,new RequestOptions());
DocumentCollection documentCollection = response.getResource();
PartitionKeyDefinition partitionKeyDefinition = documentCollection.getPartitionKey();
Collection<String> paths = partitionKeyDefinition.getPaths();
if(paths.iterator().hasNext()){
    System.out.println(paths.iterator().next());
}

我发现这很有效:_container 是 cosmos 的 db 容器。

        var settings = await _container!.ReadContainerAsync(cancellationToken: cancellationToken);
        var partitionKeyPath = settings.Resource.PartitionKeyPath[1..];

        var partitionProperty = typeof(T).GetProperty(partitionKeyPath);
        var partitionKey = partitionProperty!.GetValue(message)!.ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM