首页 > 解决方案 > 如何使用 azure-cosmos sdk 在 JAVA java 中将 cosmos 响应作为 JSON 数组获取

问题描述

我可以在 azure cosmos-db explore 中运行查询,如下图所示,并将响应视为 json 数组 在此处输入图像描述

我想使用带有 azure-cosmos SDK 的 Java 做同样的事情

下面是我的功能

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

标签: javaazureazure-cosmosdbazure-cosmosdb-sqlapi

解决方案


推荐阅读