简体   繁体   English

如何将bytearray写入couchdb?

[英]How to write bytearray to couchdb?

I want to send my object to couchdb using the ektorp java client. 我想使用ektorp Java客户端将对象发送到benchdb。 But I couldn't write my bytearray value to couchdb properly. 但是我无法将我的字节数组值正确地写入beddb。 My java object as follow: 我的java对象如下:

在此处输入图片说明

If I convert bytearray to String: 如果我将bytearray转换为String:

在此处输入图片说明

The metadata value is saved on couchdb as "AgIGZm9vBmJhegA=" (base64), This means that "foobaz". 元数据值以“ AgIGZm9vBmJhegA =“(base64)的形式保存在beddb中,这意味着“ foobaz”。 Why is my bytearray value changed? 为什么我的字节数组值更改了?

在此处输入图片说明

My example code : 我的示例代码:

private CouchDbInstance dbInstance;
private CouchDbConnector db;    

    ... 

    Map<String, Object> doc = new HashMap<>();
    doc.put("_id", "foo.com:http/");

    byte[] serilazeData = IOUtils.serialize(writer, fieldValue);
    doc.put("metadata", serilazeData);

    ...
    db.update(doc);

My main code block 我的主要代码块

    public void put(K key, T obj) {

        final Map<String, Object> doc = new HashMap<>();
        doc.put("_id", key.toString());

        Schema schema = obj.getSchema();

        List<Field> fields = schema.getFields();
        for (int i = 0; i < fields.size(); i++) {
          if (!obj.isDirty(i)) {
            continue;
          }
          Field field = fields.get(i);
          Schema.Type type = field.schema().getType();
          Object fieldValue = obj.get(field.pos());
          Schema fieldSchema = field.schema();

          fieldValue = serializeFieldValue(fieldSchema, fieldValue);
          doc.put(field.name(), fieldValue);
        }
        db.update(doc);

      }

      private Object serializeFieldValue(Schema fieldSchema, Object fieldValue ){
        ...
            byte[] data = null;
            try {
              SpecificDatumWriter writer = getDatumWriter(fieldSchema);
              data = IOUtils.serialize(writer, fieldValue);
            } catch (IOException e) {
              LOG.error(e.getMessage(), e);
            }
            fieldValue = data;
         ...
        return fieldValue;
      }

The value is the base64 encoded string "foobaz". 该值是base64编码的字符串“ foobaz”。 You should probably post your code as well to get any meaningful feedback regarding this issue. 您可能还应该发布代码,以获得有关此问题的任何有意义的反馈。

edit: Now that you provided code, is it possible, that the object you are trying to update already exists in the database? 编辑:现在您提供了代码,是否有可能,您要更新的对象已经存在于数据库中? If yes you need to get it first or provide the proper existing revision id for the update. 如果是,则需要先获取它,或者提供适当的现有版本ID进行更新。 Otherwise the update will be rejected. 否则,更新将被拒绝。

CouchDB stores JSON documents, and JSON does not support byte arrays, so I guess Ektorp is applying its own Base64 conversion during conversion of your object to JSON, before sending to CouchDB, and maybe that is skipping some characters in the byte array. CouchDB存储JSON文档,并且JSON不支持字节数组,因此我想Ektorp在将对象转换为JSON的过程中将自己的Base64转换应用于发送到CouchDB之前,并且可能跳过了字节数组中的某些字符。

You might prefer to sidestep the Ektorp behaviour by applying your own Base64 serialisation before calling Ektorp, and then deserialise yourself after fetching the document from CouchDB. 您可能希望通过在调用Ektorp之前应用自己的Base64序列化来回避Ektorp行为,然后在从CouchDB中获取文档之后对自己进行反序列化。 Or you could use something like Jackson, which will handle the object/JSON conversion behind the scenes, including byte arrays. 或者,您可以使用类似Jackson的东西,它将在后台处理对象/ JSON转换,包括字节数组。

Ektorp uses Jackson for json serialization, I think jackson defaults to base64 for byte arrays. Ektorp使用Jackson进行json序列化,我认为jackson对于字节数组默认为base64。 As long as you read/write with Ektorp you should not have any problems. 只要您使用Ektorp进行读/写,就不会有任何问题。

But I see in your code that you have some kind of type system of your own so that complicates things. 但是我在您的代码中看到您拥有自己的某种类型系统,这使事情变得复杂。 I suggest you use POJOS instead of rolling your own since you won't get much help from ektorp and jackson if you are doing it yourself. 我建议您使用POJOS而不是自己动手,因为如果您自己动手,则不会从ektorp和jackson那里获得太多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM