How to convert N1QL query results to BinaryDocuments

Can anybody please tell me how to convert N1QL results to BinaryDocuments.
My output is like below
"results": [
{
"b": "<binary (139 b)>",
“id”: “cn_billingphonenumber123_hashed|1004901933|10.12.121.140”
},
{
"b": "<binary (139 b)>",
“id”: “cn_billingphonenumber123_hashed|1004901933|10.12.121.141”
}
]

While storing I am using BinaryDocument.
Now I am using N1QL to fetch the documents, but I am getting them in Objects.
Is there any way to get BinaryDocuments as is from N1QL.

Didn’t we address this in another thread with you?

No that was for getting keys displayed in the result.
Now I am concerned of converting the results to BinaryDocuments.

The result in N1QL is coming as String instead of Object
i.e
{
"b": "<binary (139 b)>",
“id”: “cn_billingphonenumber123_hashed|1004901933|10.12.121.141”
}
"<binary (139 b)>" -> is a plain String instead of BinaryDocument.

Ah, I see. There isn’t a N1QL reply type that the client will automatically map back to the Java BinaryDocument class. I think the best way to handle that might be for the query to be on meta().id only, and then do a .get() of the key. That’ll be the most efficient way.

The only other approach would be the N1QL raw query and then processing the string back into the Java Object yourself. This is likely to be much less efficient and harder to code.

Any other thoughts @daschl or @subhashni?

Oh OK.

In that case, is there any way where we can get documents from Couchbase using wildcard search using .get() instead of N1Ql.

variableKey = “cn_billingphonenumber123_hashed%”;
BinaryDocument oldBinDoc = dataSource.instanceBucket.get(variableKey, BinaryDocument.class);

will this work?

In effect it’ll be two steps. First step is to get the keys of interest with your wildcard range query, second step is to fetch the item as a BinaryDocument based on the .get() in the SDK. This can be parallelized if needed. I’d probably recommend just doing it in two steps first and if that meets your needs, that’ll be the simplest to manage.

I’d probably recommend:

  1. Run your query and get the meta().ids of interest
  2. Do a series of CouchbaseBucket.async().get() operations to fetch the items. Then the SDK will automatically retrieve them as BinaryDocuments.

One example of a pretty sophisticated way of doing this is in the YCSB code. That uses RxJava (which you can read up on either in the developer.couchbase.com docs or at reactivex.io).

If you’re new to RxJava though, you’ll probably still have excellent results doing it in two steps without having to learn the async method.

I agree with what @ingenthr has said, this approach will give you the best performance. That said, if you still want to do this directly you need to extract the value as a string, base64decode it (I think it comes base64 encoded) and then store it in a ByteBuf to create a BinaryDocument. It’s possible, but more wasteful on GC and the approach outlined above will give you better performance with less code to write ultimately.