如何使用 JAVA 从 Azure Cosmos DB 的 collection 之一获取所有文档?
How to fetch all documents from one of the collection of Azure's Cosmos DB using JAVA?
我查看了文档,有两种获取文档的方法
- 使用 Azure 的端点
https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs
即使我设置了所需的 headers 令牌,我也得到了一些异常
{
"code": "Forbidden",
"message": "Sql api is not supported for this database account\r\nActivityId: fc5ec296-b7a1-
44df-9c69-42e804177242, Microsoft.Azure.Documents.Common/2.7.0"
}
使用 SDK
通过 sdk 我也得到了上面相同的异常,下面是我正在使用的 java 代码
用于获取文件
public class App {
private static final String END_POINT = "https://***.documents.azure.com/";
private static final String MASTER_KEY = "***";
// Define an id for your database and collection
private static final String DATABASE_ID = "iotdata";
private static final String COLLECTION_ID = "details";
public static void main(String[] args) {
DocumentClient documentClient = new DocumentClient(END_POINT, MASTER_KEY, new ConnectionPolicy(),
ConsistencyLevel.Session);
System.out.println("Check if database " + DATABASE_ID + " exists.");
String databaseLink = String.format("/dbs/%s", DATABASE_ID);
try {
ResourceResponse<Database> readDatabase = documentClient.readDatabase(databaseLink, null);
if (readDatabase.getStatusCode() == 200) {
System.out.println("Connection Established");
}
FeedResponse<Document> queryResults = documentClient.queryDocuments("/dbs/iotdata/colls/details",
"SELECT * FROM details WHERE mac_address = '28:b2:bd:01:d0:94'", null);
System.out.println("Running SQL query...");
for (Document family : queryResults.getQueryIterable()) {
System.out.println(String.format("\tRead %s", family));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
首先,我不得不说你在第二点中的示例代码完全 right.I 测试过它并适用于 Cosmos DB Sql Api.
根据错误:
Sql api is not supported for this database account
您似乎想使用 SQL API REST API or SQL API SDK. That's forbidden and i'm afraid that's the reason for above error.If your Cosmos DB account is using the Mongo API, you should be using tools and drivers that use the Mongo API. Please refer to this case:Sql api is not supported for this database Error
访问其他类型的 cosmos db 帐户(我猜可能是 mongo api)
我查看了文档,有两种获取文档的方法
- 使用 Azure 的端点 https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs 即使我设置了所需的 headers 令牌,我也得到了一些异常
{ "code": "Forbidden", "message": "Sql api is not supported for this database account\r\nActivityId: fc5ec296-b7a1- 44df-9c69-42e804177242, Microsoft.Azure.Documents.Common/2.7.0" }
使用 SDK 通过 sdk 我也得到了上面相同的异常,下面是我正在使用的 java 代码 用于获取文件
public class App { private static final String END_POINT = "https://***.documents.azure.com/"; private static final String MASTER_KEY = "***"; // Define an id for your database and collection private static final String DATABASE_ID = "iotdata"; private static final String COLLECTION_ID = "details"; public static void main(String[] args) { DocumentClient documentClient = new DocumentClient(END_POINT, MASTER_KEY, new ConnectionPolicy(), ConsistencyLevel.Session); System.out.println("Check if database " + DATABASE_ID + " exists."); String databaseLink = String.format("/dbs/%s", DATABASE_ID); try { ResourceResponse<Database> readDatabase = documentClient.readDatabase(databaseLink, null); if (readDatabase.getStatusCode() == 200) { System.out.println("Connection Established"); } FeedResponse<Document> queryResults = documentClient.queryDocuments("/dbs/iotdata/colls/details", "SELECT * FROM details WHERE mac_address = '28:b2:bd:01:d0:94'", null); System.out.println("Running SQL query..."); for (Document family : queryResults.getQueryIterable()) { System.out.println(String.format("\tRead %s", family)); } } catch (Exception e) { e.printStackTrace(); } }
}
首先,我不得不说你在第二点中的示例代码完全 right.I 测试过它并适用于 Cosmos DB Sql Api.
根据错误:
Sql api is not supported for this database account
您似乎想使用 SQL API REST API or SQL API SDK. That's forbidden and i'm afraid that's the reason for above error.If your Cosmos DB account is using the Mongo API, you should be using tools and drivers that use the Mongo API. Please refer to this case:Sql api is not supported for this database Error
访问其他类型的 cosmos db 帐户(我猜可能是 mongo api)