Java 使用胖客户端连接Phoenix


1. 拷贝hbase的配置文件到resources目录

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

  
    hbase.rootdir
    hdfs://ns1/hbase
  
  
    hbase.cluster.distributed
    true
  
  
    hbase.zookeeper.quorum
    hadoop200:2181,hadoop201:2181,hadoop202:2181
  
  
    hbase.zookeeper.property.clientPort
    2181
  
  
    hbase.regionserver.handler.count
    20
  
  
    hbase.master
    60000
  

  
    hbase.tmp.dir
    /opt/local/hbase/tmp
    
  
  
    hbase.unsafe.stream.capability.enforce
    false
  
  
    hbase.wal.provider
    filesystem
  
  
    hbase.coprocessor.abortonerror
    false
  

  
    hbase.regionserver.wal.codec
    org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec
  

  
    phoenix.schema.isNamespaceMappingEnabled
    true
  
  
    phoenix.schema.mapSystemTablesToNamespace
    true
  



2. 配置POM中依赖包

        
            org.apache.phoenix
            phoenix-client-hbase
            2.4.0-5.1.1
        
        
            com.alibaba
            fastjson
            1.2.73
        
        
            com.google.guava
            guava
            30.0-jre
        

3. jdbc方式连接代码

public class PhoenixClientTest {

   public static void main(String[] args) {
       ResultSet rs = null;
       Statement stmt = null;
       PreparedStatement ps = null;
       Connection conn = null;

       try{

           Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
           Properties prop = getProp();
           conn = DriverManager.getConnection("jdbc:phoenix:hadoop200:2181:/hbase",prop);
           stmt = conn.createStatement();
           long      time = System.currentTimeMillis();
           rs = stmt.executeQuery("select * from SYSTEM.CATALOG");
           List list = new ArrayList<>();
           while(rs.next()){
               JSONObject obj = new JSONObject();
               String tableScheme = rs.getString("TABLE_SCHEM");
               String tableName = rs.getString("TABLE_NAME");
               String columeName = rs.getString("COLUMN_NAME");
               obj.put("tableScheme",tableScheme);
               obj.put("tableName",tableName);
               obj.put("columeName",columeName);
               list.add(obj);
           }
           for(JSONObject obj:list){
               System.out.println(obj.toJSONString());
           }


           long timeUsed = System.currentTimeMillis() - time;
           System.out.println("time " + timeUsed + "mm");
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           try{
               if(null != stmt){stmt.close();}
               if(null != rs){rs.close();}
               if(null != ps){ps.close();}
               if(null != conn){conn.close();}
           }catch (Exception e){
               e.printStackTrace();
           }
           
       }
   }

   private static Properties getProp(){
       Properties prop = new Properties();
       prop.put("username","");
       prop.put("password","");
       prop.put("initialSize",20);
       prop.put("maxActive",0);
       prop.put("defaultAutoCommit",true);
       return prop;
   }
}

4.执行测试结果