簡體   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