Using the Phoenix Query Server (PQS) client driver to connect to an HBase database

  Apache Phoenix, HBase, Java

The Phoenix Thin Client's JDBC connection string

Contents

Now the time has come to write some Java code. We apply the standard DAO pattern and extract the Phoenix specific connection code into a seperate class DaoPhoenix.java. Pay particular attention to the JDBC connection string which is what this class is really all about:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class PhoenixConnector {
  private Connection connection;

  public Connection connect(String host, Integer port, String user, String password) {
    try {
       // Loading drivers for Phoenix/HBase
       Class.forName("org.apache.phoenix.queryserver.client.Driver");
       if(connection == null) {
         connection = DriverManager.getConnection("jdbc:phoenix:thin:url=http://127.0.0.1:8765;serialization=PROTOBUF");
       }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return connection; 
  }

  public void close() throws SQLException {
    connection.close();
  }

  public Connection getConnection() {
    return connection;
  }
}

 

LEAVE A COMMENT