使用Java API编写自定义文档到Cosmos DB。

huangapple go评论107阅读模式
英文:

Write custom document to Cosmos DB with Java API

问题

以下是翻译好的内容:

我有一个 Cosmos DB,并且想要向其中写入不同类型的文档。文档的结构是动态的,可能会发生变化。

我尝试了以下方法。假设我有如下的类:

  1. class CosmosDbItem implements Serializable {
  2. private final String _id;
  3. private final String _payload;
  4. public CosmosDbItem(String id, String payload) {
  5. _id = id;
  6. _payload = payload;
  7. }
  8. public String getId() {
  9. return _id;
  10. }
  11. public String getPayload() {
  12. return _payload;
  13. }
  14. }

然后,我可以使用一些 JSON 创建文档,如下所示:

  1. CosmosContainer _container = ...
  2. CosmosDbItem dataToWrite = new CosmosDbItem("what-ever-id-18357", "{\"name\":\"Jane Doe\", \"age\":42}")
  3. item = _cosmosContainer.createItem(dataToWrite, partitionKey, cosmosItemRequestOptions);

这将生成类似以下的文档:

  1. {
  2. "id": "what-ever-id-18357",
  3. "payload": "{\"name\":\"Jane Doe\", \"age\":42}",
  4. "_rid": "aaaaaaDaaAAAAAAAAAA==",
  5. "_self": "dbs/aaaaAA==/colls/aaaaAaaaDI=/docs/aaaaapaaaaaAAAAAAAAAA==/",
  6. "_etag": "\"6e00c443-0000-0700-0000-5f8499a70000\"",
  7. "_attachments": "attachments/",
  8. "_ts": 1602525607
  9. }

有没有办法将 payload 生成为实际的 JSON 对象并嵌套在文档中?我需要如何更改我的 CosmosDbItem 类来实现这一点?就像这样:

  1. {
  2. "id": "what-ever-id-18357",
  3. "payload": {
  4. "name": "Jane Doe",
  5. "age": 42
  6. },
  7. "_rid": "aaaaaaDaaAAAAAAAAAA==",
  8. "_self": "dbs/aaaaAA==/colls/aaaaAaaaDI=/docs/aaaaapaaaaaAAAAAAAAAA==/",
  9. "_etag": "\"6e00c443-0000-0700-0000-5f8499a70000\"",
  10. "_attachments": "attachments/",
  11. "_ts": 1602525607
  12. }
英文:

I have a Cosmos DB and want to write different kind of documents to it. The structure of the documents is dynamic and can change.

I tried the following. Let's say I have the following class:

  1. class CosmosDbItem implements Serializable {
  2. private final String _id;
  3. private final String _payload;
  4. public CosmosDbItem(String id, String payload) {
  5. _id = id;
  6. _payload = payload;
  7. }
  8. public String getId() {
  9. return _id;
  10. }
  11. public String getPayload() {
  12. return _payload;
  13. }
  14. }

I can create then the document with some JSON as follows:

  1. CosmosContainer _container = ...
  2. CosmosDbItem dataToWrite = new CosmosDbItem("what-ever-id-18357", "{\"name\":\"Jane Doe\", \"age\":42}")
  3. item = _cosmosContainer.createItem(dataToWrite, partitionKey, cosmosItemRequestOptions);

This results in a document like that:

  1. {
  2. "id": "what-ever-id-18357",
  3. "payload": "{\"name\":\"Jane Doe\", \"age\":42}",
  4. "_rid": "aaaaaaDaaAAAAAAAAAA==",
  5. "_self": "dbs/aaaaAA==/colls/aaaaAaaaDI=/docs/aaaaapaaaaaAAAAAAAAAA==/",
  6. "_etag": "\"6e00c443-0000-0700-0000-5f8499a70000\"",
  7. "_attachments": "attachments/",
  8. "_ts": 1602525607
  9. }

Is there a way in generating the payload as real JSON object in that document? What do I need to change in my CosmosDbItem class? Like this:

  1. {
  2. "id": "what-ever-id-18357",
  3. "payload": {
  4. "name":"Jane Doe",
  5. "age":42
  6. },
  7. "_rid": "aaaaaaDaaAAAAAAAAAA==",
  8. "_self": "dbs/aaaaAA==/colls/aaaaAaaaDI=/docs/aaaaapaaaaaAAAAAAAAAA==/",
  9. "_etag": "\"6e00c443-0000-0700-0000-5f8499a70000\"",
  10. "_attachments": "attachments/",
  11. "_ts": 1602525607
  12. }

答案1

得分: 0

以下是您的翻译内容:

这是我的解决方案,我最终采用了它。实际上,一旦我理解了它,它就相当简单。我使用了一个简单的 HashMap<String, Object>,而不是使用 CosmosDbItem

  1. public void writeData() {
  2. ...
  3. Map<String, Object> stringObjectMap = buildDocumentMap("the-id-", "{\"key\":\"vale\"}");
  4. _cosmosContainer.createItem(stringObjectMap, partitionKey, cosmosItemRequestOptions);
  5. ...
  6. }
  7. public Map<String, Object> buildDocumentMap(String id, String jsonToUse) {
  8. JSONObject jsonObject = new JSONObject(jsonToUse);
  9. jsonObject.put("id", id);
  10. return jsonObject.toMap();
  11. }

这可以生成以下文档:

  1. {
  2. "key": "value",
  3. "id": "the-id-",
  4. "_rid": "eaaaaaaaaaaaAAAAAAAAAA==",
  5. "_self": "dbs/eaaaAA==/colls/eaaaaaaaaaM=/docs/eaaaaaaaaaaaaaAAAAAAAA==/",
  6. "_etag": "\"3b0063ea-0000-0700-0000-5f804b3d0000\"",
  7. "_attachments": "attachments/",
  8. "_ts": 1602243389
  9. }

一点说明:在 HashMap 中设置 id 键非常重要。否则,将会出现以下错误:

  1. "The input content is invalid because the required properties - 'id; ' - are missing"
英文:

Here is my solution that I ended up. Actually it is pretty simple once I got behind it. Instead of using CosmosDbItem I use a simple HashMap<String, Object>.

  1. public void writeData() {
  2. ...
  3. Map<String, Object> stringObjectMap = buildDocumentMap("the-id-", "{\"key\":\"vale\"}");
  4. _cosmosContainer.createItem(stringObjectMap, partitionKey, cosmosItemRequestOptions);
  5. ...
  6. }
  7. public Map<String, Object> buildDocumentMap(String id, String jsonToUse) {
  8. JSONObject jsonObject = new JSONObject(jsonToUse);
  9. jsonObject.put("id", id);
  10. return jsonObject.toMap();
  11. }

This can produce the following document:

  1. {
  2. "key": "value",
  3. "id": "the-id-",
  4. "_rid": "eaaaaaaaaaaaAAAAAAAAAA==",
  5. "_self": "dbs/eaaaAA==/colls/eaaaaaaaaaM=/docs/eaaaaaaaaaaaaaAAAAAAAA==/",
  6. "_etag": "\"3b0063ea-0000-0700-0000-5f804b3d0000\"",
  7. "_attachments": "attachments/",
  8. "_ts": 1602243389
  9. }

One remark: it is important to set the id key in the HashMap. Otherwise one will get the error

  1. "The input content is invalid because the required properties - 'id; ' - are missing"

huangapple
  • 本文由 发表于 2020年10月13日 16:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64331864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定