问题描述
我正在尝试编写一个功能来存储来自Azure的 DocumentDB 中的对象。
I am trying to write a functionality to store an object in DocumentDB from Azure.
我有以下代码:
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);
}
}
如果事件
不存在(表示数据库中没有记录 id
事件
)然后调用 createDocument
。如果记录已存在于数据库中,则调用 replaceDocument
。
If the event
does not exist (means that there is no record in database with the id
of event
) then createDocument
is called. If the record already exists in the database then replaceDocument
is called.
- <$调用c $ c> createDocument 没有问题,并在数据库中创建文档
-
replaceDocument
throwsStatusCode:未经授权的异常
createDocument
is called without problem and document is created in the databasereplaceDocument
throwsStatusCode: Unauthorized exception
com.microsoft.azure .documentdb.DocumentClientException:输入授权令牌无法提供请求。请检查是否按照协议构建了预期的有效负载,并检查正在使用的密钥。服务器使用以下有效负载进行签名:'put colls dbs / sporteventsdb / colls / sportevents sun,26 mar 2017 08:32:41 gmt
完整堆栈:
代码中使用的常量:
-
COLLECTION_LINK =dbs /+ DATABASE_ID +/ colls /+ COLLECTION_ID;
-
SELECT_DOCUMENT =SELECT * FROM+ DATABASE_ID +WHERE+ DATABASE_ID +。id = \%d \;
COLLECTION_LINK = "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID";
SELECT_DOCUMENT = "SELECT * FROM " + DATABASE_ID + " WHERE " + DATABASE_ID + ".id = \"%d\"";
我正在开发 Spring
框架 IntelliJ IDEA
on Ubuntu
with Java 8
。
I'm developing with Spring
framework on IntelliJ IDEA
on Ubuntu
with Java 8
.
推荐答案
您收到错误是因为DocumentDB的将 documentLink
作为参数,没有collectionLink。 Javadoc在这里:
You're getting the error because DocumentDB's replaceDocument
takes the documentLink
as an argument, no the collectionLink. Javadoc is here: http://azure.github.io/azure-documentdb-java/
这篇关于Java - DocumentDB未经授权的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!