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