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

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

Write custom document to Cosmos DB with Java API

问题

以下是翻译好的内容:

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

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

class CosmosDbItem implements Serializable {
    private final String _id;
    private final String _payload;

    public CosmosDbItem(String id, String payload) {
      _id = id;
      _payload = payload;
    }

    public String getId() {
      return _id;
    }

    public String getPayload() {
      return _payload;
    }
}

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

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

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

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

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

{
    "id": "what-ever-id-18357",
    "payload": {
        "name": "Jane Doe",
        "age": 42
    },
    "_rid": "aaaaaaDaaAAAAAAAAAA==",
    "_self": "dbs/aaaaAA==/colls/aaaaAaaaDI=/docs/aaaaapaaaaaAAAAAAAAAA==/",
    "_etag": "\"6e00c443-0000-0700-0000-5f8499a70000\"",
    "_attachments": "attachments/",
    "_ts": 1602525607
}
英文:

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:

class CosmosDbItem implements Serializable {
    private final String _id;
    private final String _payload;

    public CosmosDbItem(String id, String payload) {
      _id = id;
      _payload = payload;
    }

    public String getId() {
      return _id;
    }

    public String getPayload() {
      return _payload;
    }
  }

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

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

This results in a document like that:

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

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:

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

答案1

得分: 0

以下是您的翻译内容:

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

public void writeData() {
    ...
    Map<String, Object> stringObjectMap = buildDocumentMap("the-id-", "{\"key\":\"vale\"}");
    _cosmosContainer.createItem(stringObjectMap, partitionKey, cosmosItemRequestOptions);
    ...
  }
  
  public Map<String, Object> buildDocumentMap(String id, String jsonToUse) {
    JSONObject jsonObject = new JSONObject(jsonToUse);
    jsonObject.put("id", id);
    return jsonObject.toMap();
  }

这可以生成以下文档:

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

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

"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>.

public void writeData() {
    ...
    Map<String, Object> stringObjectMap = buildDocumentMap("the-id-", "{\"key\":\"vale\"}");
    _cosmosContainer.createItem(stringObjectMap, partitionKey, cosmosItemRequestOptions);
    ...
  }
  
  public Map<String, Object> buildDocumentMap(String id, String jsonToUse) {
    JSONObject jsonObject = new JSONObject(jsonToUse);
    jsonObject.put("id", id);
    return jsonObject.toMap();
  }

This can produce the following document:

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

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

"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:

确定