Java Code Examples for org.apache.hadoop.hbase.util.Addressing#parsePort()

The following examples show how to use org.apache.hadoop.hbase.util.Addressing#parsePort() . These examples are extracted from open source projects. 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 Project: phoenix   File: QueryUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String getConnectionUrl(Properties props, Configuration conf)
        throws ClassNotFoundException, SQLException {
    // make sure we load the phoenix driver
    Class.forName(PhoenixDriver.class.getName());

    // read the hbase properties from the configuration
    String server = ZKConfig.getZKQuorumServersString(conf);
    // could be a comma-separated list
    String[] rawServers = server.split(",");
    List<String> servers = new ArrayList<String>(rawServers.length);
    boolean first = true;
    int port = -1;
    for (String serverPort : rawServers) {
        try {
            server = Addressing.parseHostname(serverPort);
            int specifiedPort = Addressing.parsePort(serverPort);
            // there was a previously specified port and it doesn't match this server
            if (port > 0 && specifiedPort != port) {
                throw new IllegalStateException("Phoenix/HBase only supports connecting to a " +
                        "single zookeeper client port. Specify servers only as host names in " +
                        "HBase configuration");
            }
            // set the port to the specified port
            port = specifiedPort;
            servers.add(server);
        } catch (IllegalArgumentException e) {
        }
    }
    // port wasn't set, shouldn't ever happen from HBase, but just in case
    if (port == -1) {
        port = conf.getInt(QueryServices.ZOOKEEPER_PORT_ATTRIB, -1);
        if (port == -1) {
            throw new RuntimeException("Client zk port was not set!");
        }
    }
    server = Joiner.on(',').join(servers);

    return getUrl(server, port);
}
 
Example 2
Source Project: hbase   File: ProtobufUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get a ServerName from the passed in data bytes.
 * @param data Data with a serialize server name in it; can handle the old style servername where
 *          servername was host and port. Works too with data that begins w/ the pb 'PBUF' magic
 *          and that is then followed by a protobuf that has a serialized {@link ServerName} in
 *          it.
 * @return Returns null if <code>data</code> is null else converts passed data to a ServerName
 *         instance.
 */
public static ServerName toServerName(final byte[] data) throws DeserializationException {
  if (data == null || data.length <= 0) {
    return null;
  }
  if (ProtobufMagic.isPBMagicPrefix(data)) {
    int prefixLen = ProtobufMagic.lengthOfPBMagic();
    try {
      ZooKeeperProtos.Master rss =
        ZooKeeperProtos.Master.parser().parseFrom(data, prefixLen, data.length - prefixLen);
      HBaseProtos.ServerName sn = rss.getMaster();
      return ServerName.valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
    } catch (/* InvalidProtocolBufferException */IOException e) {
      // A failed parse of the znode is pretty catastrophic. Rather than loop
      // retrying hoping the bad bytes will changes, and rather than change
      // the signature on this method to add an IOE which will send ripples all
      // over the code base, throw a RuntimeException. This should "never" happen.
      // Fail fast if it does.
      throw new DeserializationException(e);
    }
  }
  // The str returned could be old style -- pre hbase-1502 -- which was
  // hostname and port seperated by a colon rather than hostname, port and
  // startcode delimited by a ','.
  String str = Bytes.toString(data);
  int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
  if (index != -1) {
    // Presume its ServerName serialized with versioned bytes.
    return ServerName.parseVersionedServerName(data);
  }
  // Presume it a hostname:port format.
  String hostname = Addressing.parseHostname(str);
  int port = Addressing.parsePort(str);
  return ServerName.valueOf(hostname, port, -1L);
}
 
Example 3
Source Project: hbase   File: ProtobufUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get a ServerName from the passed in data bytes.
 * @param data Data with a serialize server name in it; can handle the old style
 * servername where servername was host and port.  Works too with data that
 * begins w/ the pb 'PBUF' magic and that is then followed by a protobuf that
 * has a serialized {@link ServerName} in it.
 * @return Returns null if <code>data</code> is null else converts passed data
 * to a ServerName instance.
 * @throws DeserializationException
 */
public static ServerName parseServerNameFrom(final byte [] data) throws DeserializationException {
  if (data == null || data.length <= 0) return null;
  if (ProtobufMagic.isPBMagicPrefix(data)) {
    int prefixLen = ProtobufMagic.lengthOfPBMagic();
    try {
      ZooKeeperProtos.Master rss =
        ZooKeeperProtos.Master.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
      org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ServerName sn =
          rss.getMaster();
      return ServerName.valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
    } catch (/*InvalidProtocolBufferException*/IOException e) {
      // A failed parse of the znode is pretty catastrophic. Rather than loop
      // retrying hoping the bad bytes will changes, and rather than change
      // the signature on this method to add an IOE which will send ripples all
      // over the code base, throw a RuntimeException.  This should "never" happen.
      // Fail fast if it does.
      throw new DeserializationException(e);
    }
  }
  // The str returned could be old style -- pre hbase-1502 -- which was
  // hostname and port seperated by a colon rather than hostname, port and
  // startcode delimited by a ','.
  String str = Bytes.toString(data);
  int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
  if (index != -1) {
    // Presume its ServerName serialized with versioned bytes.
    return ServerName.parseVersionedServerName(data);
  }
  // Presume it a hostname:port format.
  String hostname = Addressing.parseHostname(str);
  int port = Addressing.parsePort(str);
  return ServerName.valueOf(hostname, port, -1L);
}
 
Example 4
public static StartcodeAgnosticServerName valueOf(final String hostnameAndPort, long startcode) {
  return new StartcodeAgnosticServerName(Addressing.parseHostname(hostnameAndPort),
      Addressing.parsePort(hostnameAndPort), startcode);
}