long long - 3 months ago 21
Java Question

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

For example, there is a znode path

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:

.forPath(path, value.getBytes(StandardCharsets.UTF_8));

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


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

byte[] input = SerializationUtils.serialize(yourList);
        .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.