View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.zk;
20  
21  import org.apache.commons.cli.CommandLine;
22  import org.apache.commons.cli.CommandLineParser;
23  import org.apache.commons.cli.HelpFormatter;
24  import org.apache.commons.cli.Options;
25  import org.apache.commons.cli.PosixParser;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.util.Tool;
28  import org.apache.hadoop.util.ToolRunner;
29  import org.apache.zookeeper.CreateMode;
30  import org.apache.zookeeper.WatchedEvent;
31  import org.apache.zookeeper.Watcher;
32  import org.apache.zookeeper.ZooDefs;
33  
34  import static java.lang.System.out;
35  
36  /** A utility class to be used to create a ZooKeeper node */
37  public class ZooKeeperNodeCreator implements Tool, Watcher {
38    /** The configuration */
39    private Configuration conf;
40  
41    @Override
42    public void setConf(Configuration conf) {
43      this.conf = conf;
44    }
45  
46    @Override
47    public Configuration getConf() {
48      return conf;
49    }
50  
51    @Override
52    public int 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");
58  
59      HelpFormatter formatter = new HelpFormatter();
60      if (args.length == 0) {
61        formatter.printHelp(getClass().getName(), options, true);
62        return 0;
63      }
64  
65      CommandLineParser parser = new PosixParser();
66      CommandLine cmd = parser.parse(options, args);
67  
68      ZooKeeperExt zkExt = new ZooKeeperExt(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);
72      return 0;
73    }
74  
75    @Override
76    public void process(WatchedEvent event) {
77      out.println("process: ZK event received: " + event);
78    }
79  
80    /**
81     * Entry point from shell script
82     * @param args the command line arguments
83     */
84    public static void main(String[] args) throws Exception {
85      System.exit(ToolRunner.run(new ZooKeeperNodeCreator(), args));
86    }
87  }