long long - 2 months ago 18
Java Question

How to store a list of strings in a single ZooKeeper znode using Curator

For example, there is a znode path

A/B/C/D
.
I'd like to store a list of Strings on that znode.
Obviously, I could use join a list of strings into the single string and then serialize it into byte array like this:

curator.create()
.creatingParentContainersIfNeeded()
.forPath(path, value.getBytes(StandardCharsets.UTF_8));


But this looks not very convenient.
Is there any other approach?

Answer

The easiest/best way to do it is probably to use ApacheUtils:

byte[] input = SerializationUtils.serialize(yourList);
curator.create()
        .creatingParentContainersIfNeeded()
        .forPath(path, input);

and to get it out:

byte[] output = curator.getData().forPath(path);
List<String> newList = (List<String>)SerializationUtils.deserialize(output);

This is a rather general aproach that will work with most java objects.

Comments