简体   繁体   English

如何使用 JAVA 从 Azure 的 Cosmos DB 集合之一中获取所有文档?

[英]How to fetch all documents from one of the collection of Azure's Cosmos DB using JAVA?

I gone through the documentation , there are two ways to fetch the documents我浏览了文档,有两种获取文档的方法

  1. using Azure's Endpoint https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs even i set the required headers Token and all I am getting some exception使用 Azure 的端点 https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs 即使我设置了所需的标头令牌,但我得到了一些例外

{ "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" } { "code": "Forbidden", "message": "此数据库帐户不支持 Sql api\\r\\nActivityId: fc5ec296-b7a1-44df-9c69-42e804177242, Microsoft.Azure.Documents.Common/2.7.0" }

  1. using SDK through the sdk also im getting the same above exception , below is the java code which im using for fetching the documents通过 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(); } }

    } }

Firstly,i have to say that your sample code in second point is totally right.I tested it and works with Cosmos DB Sql Api.首先,我不得不说你在第二点中的示例代码是完全正确的。我测试了它并与 Cosmos DB Sql Api 一起使用。

在此处输入图片说明

According to the error:根据错误:

Sql api is not supported for this database account此数据库帐户不支持 Sql api

It seems that you want to access cosmos db account of other type(maybe mongo api i guess) with SQL API REST API or SQL API SDK.您似乎想使用SQL API REST API或 SQL API SDK 访问其他类型的 cosmos db 帐户(我猜可能是 mongo api)。 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.这是被禁止的,恐怕这就是上述错误的原因。如果您的 Cosmos DB 帐户使用的是 Mongo API,则您应该使用使用 Mongo API 的工具和驱动程序。 Please refer to this case: Sql api is not supported for this database Error请参考这个案例: Sql api is not supported for this database Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于 Azure Cosmos Documents DB 的 Java Api (SQL Api) - Java Api for Azure Cosmos Documents DB (SQL Api) 从 Java 代码连接到 Azure Cosmos DB 时出现 UnknownHostException - UnknownHostException while connecting to Azure Cosmos DB from Java code 如何使用 Azure Cosmos DB 中的拉 model 和 Java? - How to use the pull model in Azure Cosmos DB with Java? 如何使用 Java SQL ZDB97423871083819Z Azure Cosmos DB 重命名容器 - How to rename a container in Azure Cosmos DB with Java SQL API? Azure DocumentDB-如何从特定日期的集合中获取文档? - Azure DocumentDB - How can I fetch documents from the collection of particular date? 如何在java中删除mongodb集合中的所有文档 - How to delete all documents in mongodb collection in java 使用 Java 中的 Azure Cosmos DB 发布/订阅的示例 - Sample for publish/subscribe with Azure Cosmos DB in Java 如何实例化 Cosmos DB 的 ResourceResponse<document> 用于在 JAVA 中进行测试</document> - How to instantiate Cosmos DB's ResourceResponse<Document> for testing in JAVA 如何获取 Firestore 集合中所有文档的名称(一个字段)? - How to fetch the names (a field) of all the documents in a Firestore collection? 如何从Cloud Firestore for Android一次提取所有集合和子集合的文档 - How to retrieve all the collection's and sub collection's document in a single fetch from cloud firestore for android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM