• Java 使用胖客户端连接Phoenix


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

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
      <property>
        <name>hbase.rootdir</name>
        <value>hdfs://ns1/hbase</value>
      </property>
      <property>
        <name>hbase.cluster.distributed</name>
        <value>true</value>
      </property>
      <property>
        <name>hbase.zookeeper.quorum</name>
        <value>hadoop200:2181,hadoop201:2181,hadoop202:2181</value>
      </property>
      <property>
        <name>hbase.zookeeper.property.clientPort</name>
        <value>2181</value>
      </property>
      <property>
        <name>hbase.regionserver.handler.count</name>
        <value>20</value>
      </property>
      <property>
        <name>hbase.master</name>
        <value>60000</value>
      </property>
    
      <property>
        <name>hbase.tmp.dir</name>
        <value>/opt/local/hbase/tmp
        </value>
      </property>
      <property>
        <name>hbase.unsafe.stream.capability.enforce</name>
        <value>false</value>
      </property>
      <property>
        <name>hbase.wal.provider</name>
        <value>filesystem</value>
      </property>
      <property>
        <name>hbase.coprocessor.abortonerror</name>
        <value>false</value>
      </property>
    
      <property>
        <name>hbase.regionserver.wal.codec</name>
        <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
      </property>
    
      <property>
        <name>phoenix.schema.isNamespaceMappingEnabled</name>
        <value>true</value>
      </property>
      <property>
        <name>phoenix.schema.mapSystemTablesToNamespace</name>
        <value>true</value>
      </property>
    
    </configuration>
    
    

    image

    2. 配置POM中依赖包

            <dependency>
                <groupId>org.apache.phoenix</groupId>
                <artifactId>phoenix-client-hbase</artifactId>
                <version>2.4.0-5.1.1</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.73</version>
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>30.0-jre</version>
            </dependency>
    

    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<JSONObject> 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.执行测试结果

    image

  • 相关阅读:
    C# switch-case
    Python学习日记之中文支持
    C++学习笔记(一)之指针
    python CGI 编程实践
    linux 配置 python3 CGI
    PowerShell入门简介
    资源整合,总有你想要的
    python 爬虫之 urllib库
    一天学一个Linux命令:第一天 ls
    DG磁盘分区提示错误
  • 原文地址:https://www.cnblogs.com/30go/p/16343591.html
Copyright © 2020-2023  润新知