Java Code Examples for org.apache.hadoop.hbase.HConstants#ZOOKEEPER_CLIENT_PORT

The following examples show how to use org.apache.hadoop.hbase.HConstants#ZOOKEEPER_CLIENT_PORT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZKConfig.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Separate the given key into the three configurations it should contain:
 * hbase.zookeeper.quorum, hbase.zookeeper.client.port
 * and zookeeper.znode.parent
 * @param key
 * @return the three configuration in the described order
 * @throws IOException
 */
public static ZKClusterKey transformClusterKey(String key) throws IOException {
  String[] parts = key.split(":");

  if (parts.length == 3) {
    if (!parts[2].matches("/.*[^/]")) {
      throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +
          HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"
          + HConstants.ZOOKEEPER_ZNODE_PARENT);
    }
    return new ZKClusterKey(parts [0], Integer.parseInt(parts [1]), parts [2]);
  }

  if (parts.length > 3) {
    // The quorum could contain client port in server:clientport format, try to transform more.
    String zNodeParent = parts [parts.length - 1];
    if (!zNodeParent.matches("/.*[^/]")) {
      throw new IOException("Cluster key passed " + key + " is invalid, the format should be:"
          + HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"
          + HConstants.ZOOKEEPER_ZNODE_PARENT);
    }

    String clientPort = parts [parts.length - 2];

    // The first part length is the total length minus the lengths of other parts and minus 2 ":"
    int endQuorumIndex = key.length() - zNodeParent.length() - clientPort.length() - 2;
    String quorumStringInput = key.substring(0, endQuorumIndex);
    String[] serverHosts = quorumStringInput.split(",");

    // The common case is that every server has its own client port specified - this means
    // that (total parts - the ZNodeParent part - the ClientPort part) is equal to
    // (the number of "," + 1) - "+ 1" because the last server has no ",".
    if ((parts.length - 2) == (serverHosts.length + 1)) {
      return new ZKClusterKey(quorumStringInput, Integer.parseInt(clientPort), zNodeParent);
    }

    // For the uncommon case that some servers has no port specified, we need to build the
    // server:clientport list using default client port for servers without specified port.
    return new ZKClusterKey(
        buildZKQuorumServerString(serverHosts, clientPort),
        Integer.parseInt(clientPort),
        zNodeParent);
  }

  throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +
      HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"
      + HConstants.ZOOKEEPER_ZNODE_PARENT);
}