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>
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.执行测试结果
本文来自博客园,作者:硅谷工具人,转载请注明原文链接:https://www.cnblogs.com/30go/p/16343591.html