0

I can run query in azure cosmos-db explore like in below image and see the response as a json array enter image description here

I want to do the same using Java with azure-cosmos SDK

Below is my function

public JSONArray getCosmosResponseFromSyncClient(String databaseName, String 
containerName, String sqlQuery) {
try {
cosmosClient = new 
CosmosClientBuilder().endpoint(cosmosURI).key(cosmosPrimaryKey).buildClient();
CosmosDatabase database = cosmosClient.getDatabase(databaseName);
CosmosContainer container = database.getContainer(containerName);

int preferredPageSize = 10;
CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
queryOptions.setQueryMetricsEnabled(true);
CosmosPagedIterable < JSONArray > responsePagedIterable = container.queryItems(sqlQuery, 
queryOptions, JSONArray.class);

return cosmosQueryResponseObjectAsAJSONArray;
}finally {
cosmosClient.close();
}
}
3
  • Can you create a POJO and use that instead of a JSONArray? Commented Jul 12, 2021 at 15:26
  • @aksappy I want to write a generic function where I can pass different queries and get a json array response Commented Jul 12, 2021 at 16:15
  • I don't think it is possible. When using SQL API, the container read will try to map the document to the type that is mentioned. You could try mapping to an object and try converting them, but don't know whether it is worth it. Commented Jul 12, 2021 at 17:07

1 Answer 1

2

Assuming that org.json.JSONArray is used for JSONArray, you can use Async API in Cosmos DB V4 SDK. The cosmosAsyncClient really should be built outside your method and re-used by all threads calling the method. See sample here for creating async client properly and consuming from multiple methods.

cosmosAsyncClient = new CosmosClientBuilder().endpoint(cosmosURI).key(cosmosPrimaryKey).buildAsyncClient();
CosmosAsyncDatabase database = cosmosAsyncClient.getDatabase(databaseName);
CosmosAsyncContainer container = database.getContainer(containerName);

Your method should look like this:

public JSONArray getCosmosResponseFromAsyncClient(String sqlQuery) {
    int preferredPageSize = 10;
    CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
    queryOptions.setQueryMetricsEnabled(true);
    CosmosPagedFlux<JsonNode> pagedFlux = container.queryItems(sqlQuery, queryOptions,
            JsonNode.class);
    List<JsonNode> cosmosQueryResponseObjectAsAJSONArray = pagedFlux.byPage(preferredPageSize)
            .flatMap(pagedFluxResponse -> {
                return Flux.just(pagedFluxResponse
                        .getResults()
                        .stream()
                        .collect(Collectors.toList()));
            }).onErrorResume((exception) -> {
                logger.error(
                        "Exception. e: {}",
                        exception.getLocalizedMessage(),
                        exception);
                return Mono.empty();
            }).blockLast();
    return new JSONArray(cosmosQueryResponseObjectAsAJSONArray.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.