Kaile Smith
Kaile Smith

Reputation: 13

Access to ZooKeeper from within Storm Bolt

Context: I would like to share configuration information within bolts and rather than passing via configuration files, would like to load that into ZooKeeper. When a bolt comes up it can read it from there.

My preference is to use the same ZooKeeper instance as Storm, so the question is how does one access the Storm ZooKeeper from within a bolt?

I have looked at the Java docs and afraid the path does not seem obvious.

Upvotes: 0

Views: 535

Answers (1)

user2133814
user2133814

Reputation: 2651

Here is how I am using zookeeper in storm through the curator API:

List<String> servers = (List<String>) conf.get(Config.TRANSACTIONAL_ZOOKEEPER_SERVERS);
Long port = (Long) conf.get(Config.TRANSACTIONAL_ZOOKEEPER_PORT);
if (servers == null || port == null) {
    servers = (List<String>) conf.get(Config.STORM_ZOOKEEPER_SERVERS);
    port = (Long) conf.get(Config.STORM_ZOOKEEPER_PORT);
}
String connectionString = servers.get(0) + ":" + port.toString();
curatorFramework = CuratorFrameworkFactory.builder()
    .connectString(connectionString)
    .namespace(config.getNamespace())
    .retryPolicy(new ExponentialBackoffRetry(1000, 3))
    .build();

conf is the configuration object/map passed to each spout and bolt in the open or prepare method. namespace is a string to identify the path you will read and write to and is an attempt to keep all interactions with zookeeper separate from what storm is doing.

Upvotes: 1

Related Questions