有了Phoenix 就能够像mysql、oracle等RDBMS一样通过sql 操作Hbase,大大降低了开发成本。而且据官方说明,Phoenix 的性能很高,相对于 hbase 原生的scan 并不会差多少,而对于类似的组件 hive、Impala等,性能有着显著的提升
详细请阅读 https://phoenix.apache.org/performance.html。
Apache Phoenix 官方站点:https://phoenix.apache.org/
安装
下载对应HBase版本的 Phoenix
http://www.apache.org/dyn/closer.lua/phoenix/
Phoenix 2.x - HBase 0.94.x
Phoenix 3.x - HBase 0.94.x
Phoenix 4.x - HBase 0.98.1+
#拷贝phoenix相关jar包到$HBASE_HOME/lib下
phoenix-4.7.0-HBase-0.98-server.jar
phoenix-core-4.7.0-HBase-0.98.jar
集群环境需要每台regionserver拷贝,
重启hbase server
shell
cp phoenix-core-4.7.0-HBase-0.98.jar ../hbase-0.98.6-cdh5.3.6/lib/
#启动命令行
bin/sqlline.py hadoop01.ibeifeng.com
0: jdbc:phoenix:hadoop01.ibeifeng.com> !quit
Closing: org.apache.phoenix.jdbc.PhoenixConnection
!号命令 不能加;
!verbose
实际在HBase中生成的数据是:(不知道为什么数字变成了16进制)
图形界面
下载sqlline
http://www.squirrelsql.org/#installation
最新的3.7.1版本 必须安装jdk1.8
我使用的是3.7版本 在jdk1.7下可以使用
**安装sqlline
java -jar squirrel-sql-3.7-standard
Welcome to the installation of SQuirreL SQL Client snapshot-20160731_2321!
- Gerd Wagner <gerdwagner@users.sourceforge.net>
- Rob Manning <manningr@users.sourceforge.net>
- Colin Bell <colbell@users.sourceforge.net>
The homepage is at: http://www.squirrelsql.org
press 1 to continue, 2 to quit, 3 to redisplay
1
Select target path [/opt/modules/cdh-5.3.6-ps]
/opt/modules/cdh-5.3.6-ps/squirrel-sql-client/
press 1 to continue, 2 to quit, 3 to redisplay
1
Select the packs you want to install:
[<required>] Base (SQL Client)
[x] Standard (Standard Plugins: Code completion, Graph, Look & Feel, SQL Script, Syntax, DB Copy, DB Diff, Refactori
**一路选择需要安装的软件包输入 1
Install was successful
application installed on /opt/modules/cdh-5.3.6-ps/squirrel-sql-client/
[ Console installation done ]
**拷贝到hbase相关jar包到sqlline
cd /opt/modules/cdh-5.3.6-ps/hbase-0.98.6-cdh5.3.6/lib
cp hadoop-auth-2.5.0-cdh5.3.6.jar hadoop-common-2.5.0-cdh5.3.6.jar hbase-server-0.98.6-cdh5.3.6.jar /opt/modules/cdh-5.3.6-ps/squirrel-sql-client/lib/
**拷贝到phoenix 相关jar包到sqlline
cp phoenix-core-4.7.0-HBase-0.98.jar phoenix-4.7.0-HBase-0.98-client.jar ../squirrel-sql-client/lib
#启动squirrel
root
进入虚拟机
cd squirrel-sql-client/
sh squirrel-sql.sh
新建Driver
phoenix
jdbc:phoenix:<alias>
Class Name
org.apache.phoenix.jdbc.PhoenixDriver
新建Alias
#随便填
Name:hbase-client
Driver:选择 phoenix
#写如上内容,jdbc:phoenix:node1,node2,master等这里主要是zookeeper主机名。
URL:jdbc:phoenix:hadoop01.ibeifeng.com
#要连接的主机的用户名
User Name:root
#要连接的主机的密码
Password:123456
点击Test测试 如果成功,点击Ok按钮 进入下面的界面
至此GUI操作HBase,大功告成!!!
Phoenix Java api
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();
}
}
sql示例:
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
FAQ:
Hbase已存在的表phoniex不会映射 需要手动创建表 进行映射
phoniex 创建的表 HBase使用的时候 表名必须大写才能使用
phoniex 创建的表 主键 对应HBase的ROWKEY