简体   繁体   中英

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

I'm trying to access only partition key of an existing collection in cosmos db I have gone through official doc and git hub links but no luck. Please help me. Note: I'm able to successfully read and write data using java by using Document Client(SQL api)

Please try this code:

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());
}

I found this works: _container is the db container for cosmos.

        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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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