写在前面
本文接上篇phoenix安装部署:https://blog.csdn.net/weixin_42814075/article/details/94436278
主要介绍如何通过java代码访问phoenix,进行查询操作
版本
hbase 1.1.2
phoenix 4.7.0
POM依赖
<properties>
<phoenix.version>4.7.0-HBase-1.1</phoenix.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>${phoenix.version}</version>
</dependency>
</dependencies>
主函数
Properties props = new Properties();
props.setProperty("phoenix.query.timeoutMs", "1200000");
props.setProperty("hbase.rpc.timeout", "1200000");
props.setProperty("hbase.client.scanner.timeout.period", "1200000");
try {
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
// 这里配置zookeeper的地址,可单个,也可多个。可以是域名或者ip
String url= "jdbc:phoenix:z-001,z-002,z-003:2181:/hbase-unsecure";
Connection conn= DriverManager.getConnection(url, props);
System.out.println(conn);
Statement statement = conn.createStatement();
String sql = "select count(1) from test";
long time = System.currentTimeMillis();
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
int count = rs.getInt(1);
System.out.println("row count is " + count);
}
long timeUsed = System.currentTimeMillis() - time;
System.out.println("time " + timeUsed + "mm");
// 关闭连接
rs.close();
statement.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
这里有个点,windows上开发代码的时候一定要注意地址映射配置修改hosts文件