如何使用JAVA从Azure的Cosmos DB集合中获取所有文档?
我浏览了文档,有两种方法获取文档 使用Azure的端点 https://{databaseaccount}.documents.azure.com/dbs/{db id}/colls/{coll id}/docs 即使我设置了所需的headers令牌,我也会遇到一些异常 { 代码:禁止, 消息:此数据库帐户不支持Sql api\r\n活动ID:fc5ec296-b7a1- 44df-9c69-42e804177242,Microsoft.Azure.Documents.Common/2.7.0 } 使用SDK 通过sdk,我也得到了与上面相同的异常,下面是我正在使用的java代码 用于获取文档如何使用JAVA从Azure的Cosmos DB集合中获取所有文档?,java,spring,azure,azure-cosmosdb,endpoint,Java,Spring,Azure,Azure Cosmosdb,Endpoint,我浏览了文档,有两种方法获取文档 使用Azure的端点 https://{databaseaccount}.documents.azure.com/dbs/{db id}/colls/{coll id}/docs 即使我设置了所需的headers令牌,我也会遇到一些异常 { 代码:禁止, 消息:此数据库帐户不支持Sql api\r\n活动ID:fc5ec296-b7a1- 44df-9c69-42e804177242,Microsoft.Azure.Documents.Common/2.7.0
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();
}
}
}
首先,我必须说,第二点中的示例代码是完全正确的。我测试了它,并使用COSMOSDBSQLAPI 根据错误: 此数据库帐户不支持Sql api
似乎您想访问其他类型的cosmos db帐户,我猜可能是mongo api或SQL api SDK。这是禁止的,恐怕这就是上述错误的原因。如果您的Cosmos DB帐户使用Mongo API,您应该使用使用Mongo API的工具和驱动程序。请参考本例:首先,我必须说第二点中的示例代码是完全正确的。我对其进行了测试,并使用了Cosmos DB Sql Api 根据错误: 此数据库帐户不支持Sql api
似乎您想访问其他类型的cosmos db帐户,我猜可能是mongo api或SQL api SDK。这是禁止的,恐怕这就是上述错误的原因。如果您的Cosmos DB帐户使用Mongo API,您应该使用使用Mongo API的工具和驱动程序。请参考此案例:请不要在问题中提及您的私钥或真实信息,这可能会很危险。已为您编辑,仍建议您重新生成它们!那不是我的主键,那键已经被编辑过了。Thankxok,很好。那么,您的cosmos db帐户是sql api还是mongo api?似乎您尝试使用sql api方式访问mongo db帐户。但是通过端点,我也得到了相同的异常,不需要为api编写查询。请不要在问题中提及您的私钥或真实信息,这可能会很危险。已为您编辑,仍建议您重新生成它们!那不是我的主键,那键已经被编辑过了。Thankxok,很好。那么,您的cosmos db帐户是sql api还是mongo api?似乎您尝试使用sql api方式访问mongo db帐户。但是通过端点,我也得到了相同的异常,不需要为api编写查询。