1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.zk;
2021import org.apache.commons.cli.CommandLine;
22import org.apache.commons.cli.CommandLineParser;
23import org.apache.commons.cli.HelpFormatter;
24import org.apache.commons.cli.Options;
25import org.apache.commons.cli.PosixParser;
26import org.apache.hadoop.conf.Configuration;
27import org.apache.hadoop.util.Tool;
28import org.apache.hadoop.util.ToolRunner;
29import org.apache.zookeeper.CreateMode;
30import org.apache.zookeeper.WatchedEvent;
31import org.apache.zookeeper.Watcher;
32import org.apache.zookeeper.ZooDefs;
3334importstatic java.lang.System.out;
3536/** A utility class to be used to create a ZooKeeper node */37publicclassZooKeeperNodeCreatorimplements Tool, Watcher {
38/** The configuration */39private Configuration conf;
4041 @Override
42publicvoid setConf(Configuration conf) {
43this.conf = conf;
44 }
4546 @Override
47public Configuration getConf() {
48return conf;
49 }
5051 @Override
52publicint run(String[] args) throws Exception {
53 Options options = new Options();
54 options.addOption("zk", "zkServer", true,
55"List of host:port ZooKeeper servers");
56 options.addOption("n", "zkNode", true,
57"ZooKeeper node to create");
5859 HelpFormatter formatter = new HelpFormatter();
60if (args.length == 0) {
61 formatter.printHelp(getClass().getName(), options, true);
62return 0;
63 }
6465 CommandLineParser parser = new PosixParser();
66 CommandLine cmd = parser.parse(options, args);
6768ZooKeeperExt zkExt = newZooKeeperExt(cmd.getOptionValue("zkServer"),
69 30 * 1000, 5, 1000, this);
70 zkExt.createExt(cmd.getOptionValue("zkNode"), new byte[0],
71 ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, true);
72return 0;
73 }
7475 @Override
76publicvoid process(WatchedEvent event) {
77 out.println("process: ZK event received: " + event);
78 }
7980/**81 * Entry point from shell script82 * @param args the command line arguments83 * @throws Exception84 */85publicstaticvoid main(String[] args) throws Exception {
86 System.exit(ToolRunner.run(newZooKeeperNodeCreator(), args));
87 }
88 }