phoenix提供了hbase查询的jdbc接口,使用起来非常简单。

package cn.outofmemory.demo.phoenix;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Throwable
    {
        try {
            Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");
        } catch(ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        String url = "jdbc:phoenix:zkserver1,zkserver2:2181:/hbasePhoenix";
        Connection conn = DriverManager.getConnection(url);
        Statement statement = conn.createStatement();

        String sql = "select count(*) as cn from afs_afs_part";

        long time = System.currentTimeMillis();
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next()) {
            int count = rs.getInt("cn");
            System.out.println("row count is " + count);
        }

        long timeUsed = System.currentTimeMillis() - time;
        System.out.println("time " + timeUsed + "mm");
        rs.close();
        statement.close();
        conn.close();
    }
}

phoenix连接的url格式说明如下:

jdbc:phoenix:zkserver1,zkserver2,zkserver3:2181:/parentnode
  • 其中jdbc:phoenix是固定的
  • zkserver1,zkserver2,zkserver3是hbase的zookeeper服务器主机名
  • :2181是zookeeper的端口
  • /parentNode是hbase在zookeeper中注册的根目录