【发布时间】:2017-03-26 08:45:39
【问题描述】:
我正在尝试编写一个功能来将对象存储在 Azure 的 DocumentDB 中。
我有以下一段代码:
public void saveEvent(Event event) throws DocumentClientException {
Document document = new Document(JsonCreator.createJson(event));
//check if document already exists
FeedOptions feedOptions = new FeedOptions();
feedOptions.setEnableCrossPartitionQuery(true);
FeedResponse<Document> eventDocument = documentClient.queryDocuments(COLLECTION_LINK, String.format(SELECT_DOCUMENT, event.getId()), feedOptions);
// if there is a document with the ID then replace
if (eventDocument.getQueryIterator().hasNext()) {
//documentClient.replaceDocument(COLLECTION_LINK, document, null);
documentClient.replaceDocument(COLLECTION_LINK, document, null);
}
else {
documentClient.createDocument(COLLECTION_LINK, document, null, false);
}
}
如果event 不存在(意味着数据库中没有id 的event 的记录),则调用createDocument。如果该记录已存在于数据库中,则调用replaceDocument。
-
调用
createDocument没有问题,并在数据库中创建文档 -
replaceDocument抛出StatusCode: Unauthorized exception
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: 'put colls dbs/sporteventsdb/colls/sportevents sun, 26 mar 2017 08:32:41 gmt
全栈:http://pastebin.com/YVGwqLkH
代码中使用的常量:
COLLECTION_LINK = "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID";SELECT_DOCUMENT = "SELECT * FROM " + DATABASE_ID + " WHERE " + DATABASE_ID + ".id = \"%d\"";
我正在使用Spring 框架在IntelliJ IDEA 在Ubuntu 和Java 8 上进行开发。
【问题讨论】:
标签: java azure azure-cosmosdb