Store to Couchbase Server as JSON Text instead of Binary


#1

I’m new to Couchbase.
I’ve managed to do writing and reading from Couchbase now using the C SDK.
However, I realised that all the values I’ve written to the server are in binary.
Reading them back from the application is fine as they are retrieved as strings again.
but when I go to the Couchbase Console to view the documents or edit them there, it will say that it is binary and cannot be edited.

I would like to have my data written into Couchbase and still be able to view and edit them from the console.

Here’s a short snippet of my code. Not sure if I’m just simply missing something. Please do ignore if there are some syntax errors. I just pulled snippets out and put them together to show roughly how I’m doing it. Thanks a lot!

The JSON looks something like this
{“playerid”: “123456”, “money” : “99999”}

lcb_store_cmd_t cmd;
lcb_store_cmd_t* p_cmd = &cmd;
cmd.v.v0.key = m_Key.c_str();
cmd.v.v0.nkey = m_Key.length();
cmd.v.v0.bytes = m_Data.c_str();
cmd.v.v0.nbytes = m_Data.length();
cmd.v.v0.operation = m_OpType; 
err = lcb_store(m_Instance, NULL, 1, p_cmd);
lcb_wait(m_Instance);

Odd CB webpage behavior & C SDK binary / blob document
#2

The C library is unaware of any JSON-specific formatting; however if you set your item flags to zero and ensure that the value is a sequence of bytes which parses as JSON, you should be able to see it within the Couchbase Web UI.

Please ensure you initialize your command structure to 0 using memset first, and explicitly set the flags field to 0.

Finally, ensure you are not including the terminating NUL byte in your value; from the code snippets, it seems like you are not doing this anyway.

Finally, use the cbc utility (found in the -bin package) to see what your value actually is using hd, hexdump or similar. If you are accidentally including the NUL byte it won’t be printed to your screen, but a hex dump utility should display it.

thus e.g.

cbc cat foo -U couchbase://yourhost/yourbucket | hd

#3

Thanks for your help! It works now.


#4

I‘m developing in C sdk ,too.
May I ask your some questions?

I’m going to connect the web couchbase,how I sign the connstr field?
This is my code:
“couchbase://10.xx.xx.191/default”;
default is bucket’s name
Thanks a lot!


#5

What do you mean by web couchbase? The C client cannot directly connect to a given HTTP endpoint. You can perform some basic HTTP requests against the cluster, but you will need to connect to the cluster normally at first, and then use the lcb_make_http_request() function