Azure文档数据库-Java 1.9.5 |授权错误
我收集了一些文件。在我的应用程序中,我首先创建这个集合,然后插入文档。此外,根据需求,我还需要截断(删除所有文档)集合。为此,我使用documentdbjavaapi编写了以下代码-Azure文档数据库-Java 1.9.5 |授权错误,java,azure,azure-cosmosdb,Java,Azure,Azure Cosmosdb,我收集了一些文件。在我的应用程序中,我首先创建这个集合,然后插入文档。此外,根据需求,我还需要截断(删除所有文档)集合。为此,我使用documentdbjavaapi编写了以下代码- DocumentClient documentClient = getConnection(masterkey, server, portNo); List<Database> databaseList = documentClient.queryDatabases("SELECT * FROM roo
DocumentClient documentClient = getConnection(masterkey, server, portNo);
List<Database> databaseList = documentClient.queryDatabases("SELECT * FROM root r WHERE r.id='" + schemaName + "'", null).getQueryIterable().toList();
DocumentCollection collection = null;
Database databaseCache = (Database)databaseList.get(0);
List<DocumentCollection> collectionList = documentClient.queryCollections(databaseCache.getSelfLink(), "SELECT * FROM root r WHERE r.id='" + collectionName + "'", null).getQueryIterable().toList();
// truncate logic
if (collectionList.size() > 0) {
collection = ((DocumentCollection) collectionList.get(0));
if (truncate) {
try {
documentClient.deleteDocument(collection.getSelfLink(), null);
} catch (DocumentClientException e) {
e.printStackTrace();
}
}
} else { // create logic
RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferType("S1");
collection = new DocumentCollection();
collection.setId(collectionName);
try {
collection = documentClient.createCollection(databaseCache.getSelfLink(), collection, requestOptions).getResource();
} catch (DocumentClientException e) {
e.printStackTrace();
}
我使用的是Azure Document DB Java API版本1.9.5。
如果您能指出我的代码中的错误,或者有其他更好的方法截断集合,这将非常有帮助。非常感谢您的帮助。根据您的描述和代码,我认为问题是由以下代码引起的
try {
documentClient.deleteDocument(collection.getSelfLink(), null);
} catch (DocumentClientException e) {
e.printStackTrace();
}
似乎您希望通过上面的代码删除文档,但通过集合链接传递参数documentLink
因此,如果您的真正意图是删除集合,请使用方法DocumentClient.deleteCollection(collectionLink,options)
try {
documentClient.deleteDocument(collection.getSelfLink(), null);
} catch (DocumentClientException e) {
e.printStackTrace();
}