本文介绍了如何使用Phoenix来连接Hbase并完成增删改查
1.概述
Phoenix作为应用层和HBASE之间的中间件,以下特性使它在大数据量的简单查询场景有着独有的优势
二级索引支持(global index + local index)
编译SQL成为原生HBASE的可并行执行的scan
在数据层完成计算,server端的coprocessor执行聚合
下推where过滤条件到server端的scan filter上
利用统计信息优化、选择查询计划(5.x版本将支持CBO)
skip scan功能提高扫描速度
2.下载
https://phoenix.apache.org/download.html
选择对应的Hbase版本下载即可
3.安装
3.1.上传解压
tar -zxvf apache-phoenix-4.14.0-HBase-1.2-bin.tar.gz -C /opt/soft
3.2.拷贝jar到Hbase的lib下
scp phoenix-4.14.0-HBase-1.2-server.jar hadoop@hadoop1:/opt/soft/hbase-1.2.6/lib/
scp phoenix-4.14.0-HBase-1.2-server.jar hadoop@hadoop2:/opt/soft/hbase-1.2.6/lib/
scp phoenix-4.14.0-HBase-1.2-server.jar hadoop@hadoop3:/opt/soft/hbase-1.2.6/lib/
4.启动
4.1.启动Hbase
4.2.进入Phoenix控制台
./bin/sqlline.py
通过 help 可以查看命令
0: jdbc:phoenix:> !tables
+------------+--------------+----------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | IMMUTABLE |
+------------+--------------+----------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | false |
| | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | false |
| | SYSTEM | LOG | SYSTEM TABLE | | | | | | true |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | false |
| | SYSTEM | STATS | SYSTEM TABLE | | | | | | false |
| | | TEST | TABLE | | | | | | false |
| | | US_POPULATION | TABLE | | | | | | false |
+------------+--------------+----------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------+
CRUD
0: jdbc:phoenix:> !sql
. . . . . . . . > create table test1 (mykey integer not null primary key, mycolumn varchar);
No rows affected (1.538 seconds)
0: jdbc:phoenix:> !sql
. . . . . . . . > upsert into test1 values (1,'Hello');
1 row affected (0.064 seconds)
0: jdbc:phoenix:> !sql
. . . . . . . . > select * from test1;
+--------+-----------+
| MYKEY | MYCOLUMN |
+--------+-----------+
| 1 | Hello |
+--------+-----------+
1 row selected (0.09 seconds)
0: jdbc:phoenix:> !sql
. . . . . . . . > delete from test1 where mykey = 1;
1 row affected (0.074 seconds)
0: jdbc:phoenix:> select * from test1;
+--------+-----------+
| MYKEY | MYCOLUMN |
+--------+-----------+
+--------+-----------+
No rows selected (0.153 seconds)
键入 !quit 退出shell
5.JavaAPI
5.1.引入依赖
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.14.0-HBase-1.2</version>
</dependency>
5.2.CRUD
public class PhoenixTest {
public static void main(String[] args) throws Exception {
Statement stmt = null;
ResultSet rset = null;
Connection con = DriverManager.getConnection("jdbc:phoenix:hadoop1,hadoop2,hadoop3");
stmt = con.createStatement();
stmt.executeUpdate("create table test (mykey integer not null primary key, mycolumn varchar)");
stmt.executeUpdate("upsert into test values (1,'Hello')");
stmt.executeUpdate("upsert into test values (2,'World!')");
con.commit();
PreparedStatement statement = con.prepareStatement("SELECT * from test");
rset = statement.executeQuery();
while (rset.next()) {
System.out.println(rset.getString("mycolumn"));
}
statement.close();
con.close();
}
}
参考:
[Phoenix] 一、快速入门