Phoenix简介

HBase 提供很方便的shell脚本,可以对数据表进行 CURD 操作,但是毕竟是有一定的学习成本的,基本上对于开发来讲,sql 语句都是看家本领,那么,有没有一种方法可以把 sql 语句转换成 hbase的原生API呢? 这样就可以通过普通平常的 sql 来对hbase 进行数据的管理,使用成本大大降低。

Apache Phoenix 组件就完成了这种需求,官方注解为 “Phoenix – we put the SQL back in NoSql”,通过官方说明,Phoenix 的性能很高,相对于 hbase 原生的scan 并不会差多少,而对于类似的组件 hive、Impala等,性能有着显著的提升,详细请阅读 https://phoenix.apache.org/performance.html。

Apache Phoenix 官方站点:https://phoenix.apache.org/
Phoenix支持的sql语句: https://phoenix.apache.org/language/index.html
Phoenix 支持的DataTypes:https://phoenix.apache.org/language/datatypes.html
Phoenix 支持的函数:https://phoenix.apache.org/language/functions.html

一、安装使用

Phoenix 安装很简单,下载对应hbase版本的Phoenix,以phoenix-4.4.0-HBase-0.98-bin.tar.gz为例,解压文件,将phoenix-4.4.0-server.jar 拷贝到hbase安装目录的lib下,注意:每台regionserver均需要拷贝,在测试环境下,master和slave01均作为regionserver。重启hbase server即可,官方如下:

download and expand the latest phoenix-[version]-bin.tar.

Add the phoenix-[version]-server.jar to the classpath of all HBase region server and master and remove any previous version. An easy way to do this is to copy it into the HBase lib directory (use phoenix-core-[version].jar for Phoenix 3.x)

restart the region servers

Add the phoenix-[version]-client.jar to the classpath of any Phoenix client.

download and setup SQuirrel as your SQL client so you can issue adhoc SQL against your HBase cluster

二、shell 命令

通过案例,create 表,插入语句,更新语句,删除语句案例,详细可参考:https://phoenix.apache.org/faq.html

Phoenix 连接hbase的命令如下:
$cd /home/hadoop/phoenix/phoenix/bin .  进入phoenix的bin目录
sqlline.py [master]   其中的master为Zookeeper所在节点的主机名

从上面能够看到,已经连接到了hbase集群上面,Phoenix version 4.2,sqlline version 4.2,输入Phoenix支持的命令!tables可以查看当前集群中存在的数据表,能够看到有些是SYSTEM TABLE,其它的都是自己建立的;
phoenix在shell中的基本命令都是以感叹号为前缀,!tables ; !quit 等

下面通过脚本来模拟下使用Phoenix建立数据表、修改表、添加数据、修改数据、删除数据、删除表等操作:

1、新建一张Person表,含有IDCardNum,Name,Age 三个字段 ,test 为table_schem ,标准sql如下:
create table midas_parent_personas (
userid integer not null primary key, 
parent_id integer,
user_freshness varchar,
pay_test varchar) salt_buckets=6;
create local index idx_parent_personas_parentid on parent_personas(parent_id);

可以看到,hbase中已经存在数据表 Person了,包含了三列。

2、对表进行插入操作,sql如下:

upsert into Person (IDCardNum,Name,Age) values (100,'小明',12);

在 Phoenix 中插入的语句为 upsert ,具体如下:


从上面可以看到,三条数据已经进入hbase里面了;好了,现在要对表添加一列 sex 性别操作,怎么办?

3、alter 修改表数据,sql如下:

ALTER TABLE test.Persion ADD sex varchar(10);

Phoenix 中操作如下:

上图看到已经新增了列sex,每行的默认值为 null ,那么怎么样修改这些值呢?

4、 更新表数据 ,sql 如下:

upsert into Person (IDCardNum,Name,Age) values (100,'小明',12);

Phoenix中不存在update的语法关键字,而是upsert ,功能上替代了Insert+update,官方说明为:

UPSERT VALUES

Inserts if not present and updates otherwise the value in the table. The list of columns is optional and if not present, the values will map to the column in the order they are declared in the schema. The values must evaluate to constants.

根据介绍,只需要在upsert语句中制定存在的idcardnum即可实现更新,在 Phoenix 客户端中操作如下:


5、复杂查询,通过Phoenix可以支持 where、group by、case when 等复杂的查询条件,案例如下:


where + group by 语句例子:

jdbc:phoenix:10.35.66.72> select sex ,count(sex) as num from test.person where age >20 group by sex;
+------------+------------------------------------------+
|    SEX     |                   NUM                    |
+------------+------------------------------------------+
| 男          | 4                                        |
+------------+------------------------------------------+

case when 的例子:

0: jdbc:phoenix:10.35.66.72> select (case name when '小明' then '明明啊' when '小红' then '红红啊' else name end) asshowname from test.person;
+------------------------------------------+
|                 SHOWNAME                 |
+------------------------------------------+
| 明明啊                                      |
| 红红啊                                      |
| 小王                                       |
| 小张                                       |
| 小李                                       |
| 小李                                       |
+------------------------------------------+

更多支持语法参考:https://phoenix.apache.org/language/index.html

6、删除数据及删除表,标准sql如下:

delete from test.Person where idcardnum=100;
drop table test.person;

Phoenix中同标准sql一样,案例如下:

7、若要在命令行执行SQL脚本

$ sqlline.py localhost ../examples/stock_symbol.sql

三、 java client api 使用

java api 完全可以采用传统的 jdbc 连接的形式,案例如官方提供:

创建test.java 类,内容如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
 
public class test {
 
public static void main(String[] args) throws SQLException {
Statement stmt = null;
ResultSet rset = null;
Connection con = DriverManager.getConnection("jdbc:phoenix:[zookeeper]");
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();
}
}
在终端使用javac编译,通过Phoenix客户端执行,就能看到结果:

$ javac test.java
$ java -cp "../phoenix-[version]-client.jar:." test
 
# You should get the following output
Hello World!

当然,在生产使用中,往往采用的是 spring mvc + mybaits 的框架来进行访问的,Phoenix 完全支持这种形式,就像平常写mysql、SqlServer一样,对应的jdbc.properties中的驱动修改为 org.apache.phoenix.jdbc.PhoenixDriver 即可,其它的写法通普通的一样。


五、图形化客户端SQuirrel使用

如果你不喜欢 终端下的脚本命令,青睐于GUI化的客户端,那么 SQuirrel是个好的选择,就跟平日里使用 MsSqlServer client、Navicat client 一样,效果如下图:

使用方法:

下载SQuirrel 客户端 ,地址 http://squirrel-sql.sourceforge.net/

解压缩,删除 lib/ 下老版本的 phoenix-[oldversion]-client.jar文件,将你刚刚下载的Phoenix文件夹下最新的文件 拷贝过去;

启动SQuirrel客户端,选择 Drivers-new driver ,名称随便,url格式: jdbc:phoenix:(zk地址) ,class name textbox 填写org.apache.phoenix.jdbc.PhoenixDriver

ok, 点击 connect即可完成连接

squirrel 客户端的用法 和 Phoenix 自带终端一样,都是常见的sql语句,大家可以自己搭建练习。
--------------------- 
作者:盛装吾步 
来源:CSDN 
原文:https://blog.csdn.net/kangkangwanwan/article/details/88422461 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/lvtula/article/details/89840843