Java sdk upgrade from version 1 to 3

Hi, as I need to use new Ubuntu OS, I need to update my Couchbase server version to >7. Hence some older projects which were using Java SDK version 1.x will no longer be working.

Now I’m trying to update the projects to use Java SDK version 3.x. However, I found some data having “flags=2” and “type=base64”, these data were strings with large size.

I’m trying to call “get()” on these data, but I got byte array which cannot be converted to original data (tried “new String(b)”).

May I know how can I parse these kind of data back to string?

Thanks a lot!

Hi @littlewitchanita ,

The “flags=2” and the fact that you’re upgrading from Couchbase SDK 1.x makes me suspect the document was created using a legacy transcoder that used GZIP compression.

If you know in advance the document was written using the legacy transcoder, you can read it like this:

GetResult result = collection.get(
    documentId,
    GetOptions.getOptions()
        .transcoder(Sdk2CompatibleLegacyTranscoder.INSTANCE)
);

String content = result.contentAs(String.class);

(More info about how to use transcoders in SDK 3: Transcoders & Non-JSON Documents | Couchbase Docs)

Alternatively, you could read the document as a byte array (without Sdk2CompatibleLegacyTranscoder), then un-gzip the byte array yourself and pass the result to the String constructor.

Thanks,
David

Hi David,

It works, thanks very much.
Now I can continue on updating the SDK.