2017-03-02 27 views
0

我有一個包含一些文檔的集合。在我的應用程序中,我先創建這個集合,然後插入文檔。此外,根據需要,我還需要截斷(刪除所有文檔)集合。使用文檔數據庫的Java API我寫了下面的代碼爲我的這個目的─Azure文檔數據庫 - Java 1.9.5 |授權錯誤

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

與上面的代碼我能夠成功地創建一個新的集合。另外,我還可以在此集合中插入文檔。但截斷收集我收到以下錯誤 -

com.microsoft.azure.documentdb.DocumentClientException: The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'delete 
colls 
eyckqjnw0ae= 

我正在使用Azure文檔數據庫Java API版本1.9.5。 如果您可以指出我的代碼中的錯誤或者是否有其他更好的方法來截斷收集,那將會非常有幫助。我非常感謝這裏的任何幫助。

回答

0

根據您的描述&代碼,我認爲這個問題是由下面的代碼引起的。

try { 
    documentClient.deleteDocument(collection.getSelfLink(), null); 
} catch (DocumentClientException e) { 
    e.printStackTrace(); 
} 

看來,要刪除通過上面的代碼的文件,但傳遞參數documentLink一個集合鏈接。

因此,如果您的真實意圖是刪除一個集合,請使用方法DocumentClient.deleteCollection(collectionLink, options)