1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
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 | |
|
37 | 0 | public class ZooKeeperNodeCreator implements Tool, Watcher { |
38 | |
|
39 | |
private Configuration conf; |
40 | |
|
41 | |
@Override |
42 | |
public void setConf(Configuration conf) { |
43 | 0 | this.conf = conf; |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
public Configuration getConf() { |
48 | 0 | return conf; |
49 | |
} |
50 | |
|
51 | |
@Override |
52 | |
public int run(String[] args) throws Exception { |
53 | 0 | Options options = new Options(); |
54 | 0 | options.addOption("zk", "zkServer", true, |
55 | |
"List of host:port ZooKeeper servers"); |
56 | 0 | options.addOption("n", "zkNode", true, |
57 | |
"ZooKeeper node to create"); |
58 | |
|
59 | 0 | HelpFormatter formatter = new HelpFormatter(); |
60 | 0 | if (args.length == 0) { |
61 | 0 | formatter.printHelp(getClass().getName(), options, true); |
62 | 0 | return 0; |
63 | |
} |
64 | |
|
65 | 0 | CommandLineParser parser = new PosixParser(); |
66 | 0 | CommandLine cmd = parser.parse(options, args); |
67 | |
|
68 | 0 | ZooKeeperExt zkExt = new ZooKeeperExt(cmd.getOptionValue("zkServer"), |
69 | |
30 * 1000, 5, 1000, this); |
70 | 0 | zkExt.createExt(cmd.getOptionValue("zkNode"), new byte[0], |
71 | |
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, true); |
72 | 0 | return 0; |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
public void process(WatchedEvent event) { |
77 | 0 | out.println("process: ZK event received: " + event); |
78 | 0 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public static void main(String[] args) throws Exception { |
86 | 0 | System.exit(ToolRunner.run(new ZooKeeperNodeCreator(), args)); |
87 | 0 | } |
88 | |
} |