Does the Couchbase REST API support NON-JSON data (binary data)

99封情书 提交于 2019-12-08 07:10:54

问题


I am storing c structures to couchbase, I am doing so so that I can read back these structures later and process directly, I am avoiding the steps of 1 )C structure - > JSON while storing and 2 )JSON -> C structure while retrieving.

This is working well when I use lcb_get() and lcb_set() But I also need have a requirement for making hits to views using the REST model and lcb_make_http_request () call.

So I was wondering how the lcb_make_http_request () will handle my non-JSON C structure , which is hex data and may have nulls in between. Will I still be able to extract and populate my C - structure with the data that I get as HTTP response after calling lcb_make_http_request () ?


回答1:


As WiredPrairie said in his comment you aren't forced to use JSON and can store C structs, but keep in mind byte order and field alignment when you are doing so.

When server detects that your data isn't in JSON format it will encode it using base64 and set meta.type to "json" when the document comes to map function.

And you will be able to emit your complete document as a value if you'd like to get the value in the HTTP stream. In case of this simple map function:

function (doc, meta) {
  if (meta.type == "base64") {
    emit(meta.id);
  }
}

You will get response like this one (I've formatted it for clarity):

{
    "total_rows": 1,
    "rows": [
        {
            "id": "foo",
            "key": "foo",
            "value": "4KwuAgAAAAA="
        }
    ]
}

It does mean that you must use some json parser to extract "value" attribute from result, decode it and you will get exactly the same bytestream, you have sent it with SET command.



来源:https://stackoverflow.com/questions/17104465/does-the-couchbase-rest-api-support-non-json-data-binary-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!