001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.zookeeper; 021 022import java.util.LinkedList; 023import java.util.List; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseConfiguration; 027import org.apache.hadoop.hbase.HBaseInterfaceAudience; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.ServerName; 030import org.apache.yetus.audience.InterfaceAudience; 031 032/** 033 * Tool for reading ZooKeeper servers from HBase XML configuration and producing 034 * a line-by-line list for use by bash scripts. 035 */ 036@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) 037public final class ZKServerTool { 038 private ZKServerTool() { 039 } 040 041 public static ServerName[] readZKNodes(Configuration conf) { 042 List<ServerName> hosts = new LinkedList<>(); 043 String quorum = conf.get(HConstants.ZOOKEEPER_QUORUM, HConstants.LOCALHOST); 044 045 String[] values = quorum.split(","); 046 for (String value : values) { 047 String[] parts = value.split(":"); 048 String host = parts[0]; 049 int port = HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT; 050 if (parts.length > 1) { 051 port = Integer.parseInt(parts[1]); 052 } 053 hosts.add(ServerName.valueOf(host, port, -1)); 054 } 055 return hosts.toArray(new ServerName[hosts.size()]); 056 } 057 058 /** 059 * Run the tool. 060 * @param args Command line arguments. 061 */ 062 public static void main(String[] args) { 063 for(ServerName server: readZKNodes(HBaseConfiguration.create())) { 064 // bin/zookeeper.sh relies on the "ZK host" string for grepping which is case sensitive. 065 System.out.println("ZK host: " + server.getHostname()); 066 } 067 } 068}