1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.zookeeper;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.Properties;
27
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30
31
32
33
34
35
36
37
38 public class ZooKeeperMainServerArg {
39 public String parse(final Configuration c) {
40
41
42
43 Properties zkProps = ZKConfig.makeZKProps(c);
44 String host = null;
45 String clientPort = null;
46 List<String> hosts = new ArrayList<String>();
47 for (Entry<Object, Object> entry: zkProps.entrySet()) {
48 String key = entry.getKey().toString().trim();
49 String value = entry.getValue().toString().trim();
50 if (key.startsWith("server.")) {
51 String[] parts = value.split(":");
52 hosts.add(parts[0]);
53 } else if (key.endsWith("clientPort")) {
54 clientPort = value;
55 }
56 }
57 if (hosts.isEmpty() || clientPort == null)
58 return null;
59 for (int i = 0; i < hosts.size(); i++) {
60 if (i > 0)
61 host += "," + hosts.get(i);
62 else
63 host = hosts.get(i);
64 }
65 return host != null ? host + ":" + clientPort : null;
66 }
67
68
69
70
71
72 public static void main(String args[]) {
73 Configuration conf = HBaseConfiguration.create();
74 String hostport = new ZooKeeperMainServerArg().parse(conf);
75 System.out.println((hostport == null || hostport.length() == 0)? "":
76 "-server " + hostport);
77 }
78 }