How do I get _ts of cosmos saved document in Cosmos v4 API's (CosmosItemResponse)?

Viewed 231

In Cosmos v2 API, I am able to get Resource from azure.docuemntdb.docuemnt.getResource() and from there _ts of the docuemnt, and also _ts is part of response headers. But in v4 api with readItem or replaceItem APIs, I am not seeing any option to get _ts value from CosmosItemResponse Object. Can anyone help me resolve this.

Note: I am facing this problem for java sdk v4 Async API. I tried using "x-ms-last-state-change-utc" but it's not the _ts timesamp of the updated document.

SAMPLE CODE:

public double updatePerson(Person p) {
        Mono<CosmosItemResponse<Person>> responseFlux = container.readItem(p.getId(), new PartitionKey(p.getPersonId()), Person.class);
        double responsePersonReplaced = 0;
        Person responsePerson = responseFlux.log().flatMap(x-> {
            Person iP = x.getItem();
            iP.seteTag(x.getResponseHeaders().get("etag"));
            return Mono.just(iP);
        }).block();

        p.seteTag(responsePerson.geteTag());

        CosmosItemRequestOptions cosmosItemRequestOptions = new CosmosItemRequestOptions();
        cosmosItemRequestOptions.setIfMatchETag(responsePerson.geteTag());
        try {
        Mono<CosmosItemResponse<Person>> repalcedFlux = container.replaceItem(p, p.getId(),new PartitionKey(p.getPersonId()), cosmosItemRequestOptions);
        responsePersonReplaced = repalcedFlux.log().flatMap(x-> {
            try {
                return Mono.just(DateUtils.parseDate(x.getResponseHeaders().get("x-ms-last-state-change-utc"), "EEE, d MMM yyyy HH:mm:ss.SSS z").getTime());
            } catch (ParseException e) {
                e.printStackTrace();
                return Mono.empty();
            }


        }).block();

        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());

        }
        return responsePersonReplaced;
    }

1 Answers

There is no _ts in the response header. This is the time stamp for each document.

If you want this value in your POCO then add "_ts" as a property in your POCO and if you're using queries, add it to your SELECT statement or use SELECT *. For all other CRUD operations deserialize the response into your new POCO object it will be returned as a property.

Related