long long - 1 year ago 92
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download