前提:已经部署好的单机hbase2.0
1.下载解压phoenix压缩包(注意对应hbase版本)
$ wget http://mirrors.tuna.tsinghua.edu.cn/apache/phoenix/apache-phoenix-5.0.0-HBase-2.0/bin/apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz $ tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz |
2.拷贝phoenix jar包到Hbase中
apache-phoenix-5.0.0-HBase-2.0-bin目录下,将phoenix-5.0.0-HBase-1.2-server.jar拷贝到集群中每个节点( 主节点也要拷贝 )的 hbase 的 lib 目录下
3.配置环境变量
$vi /etc/profile
export PHOENIX_HOME=/home/dev/hbase/apache-phoenix-5.0.0-HBase-2.0-bin export PHOENIX_CLASSPATH=$PHOENIX_HOME export PATH=$PATH:$PHOENIX_HOME/bin |
$source /etc/profile
4.重启hbase
5.进入phoenix命令控制台
sqlline.py localhost:2181
查询表,下面的系统表为首次进入phoenix初始化时所建立的phoenix系统表。
6.基础操作
入门教程:https://www.cloud.alipay.com/docs/2/53716
快速开始
- 创建一个 us_population.sql 文件,用来创建表结构,内容如下
CREATE TABLE IF NOT EXISTS us_population (
state CHAR(2) NOT NULL,
city VARCHAR NOT NULL,
population BIGINT
CONSTRAINT my_pk PRIMARY KEY (state, city));
-
准备分析用数据
数据文件us_population.csv,内容如下
NY,New York,8143197
CA,Los Angeles,3844829
IL,Chicago,2842518
TX,Houston,2016582
PA,Philadelphia,1463281
AZ,Phoenix,1461575
TX,San Antonio,1256509
CA,San Diego,1255540
TX,Dallas,1213825
CA,San Jose,912332
- 分析SQL文件,us_population_queries.sql,内容如下
SELECT state as "State",count(city) as "City Count",sum(population) as "Population Sum"
FROM us_population
GROUP BY state
ORDER BY sum(population) DESC;
- 执行分析 现在我们执行如下的语句来进行分析
./psql.py <your_zookeeper_quorum> us_population.sql us_population.csv us_population_queries.sql
zk1,zk2,zk3
-
结果验证
成功运行后会看到类似如下的结果
API访问Phoenix JDBC
- 使用Maven构建工程时,依赖如下
<dependencies>
<dependency>
<groupId>com.aliyun.phoenix</groupId>
<artifactId>ali-phoenix-core</artifactId>
<!-- jdk7 compiled -->
<version>4.11.0-AliHBase-1.1-0.3</version>
</dependency>
</dependencies>
- 测试程序
-
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; import java.sql.Statement; /** * create table, create index, insert data, select table. */ public class TestPhoenixJDBC { public static void main(String[] args) { try { Connection con = DriverManager.getConnection("jdbc:phoenix:n-proxy-pub-zk1, n-proxy-pub-zk2,n-proxy-pub-zk3"); Statement stmt = con.createStatement(); stmt.execute("drop table if exists test"); stmt.execute("create table test (mykey integer not null primary key, mycolumn varchar)"); stmt.execute("create index test_idx on test(mycolumn)"); stmt.executeUpdate("upsert into test values (1,'World!')"); stmt.executeUpdate("upsert into test values (2,'Hello')"); stmt.executeUpdate("upsert into test values (3,'World!')"); con.commit(); PreparedStatement statement = con.prepareStatement("select mykey from test where mycolumn='Hello'"); ResultSet rset = statement.executeQuery(); while (rset.next()) { System.out.println(rset.getInt(1)); } stmt.close(); rset.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
- 编译执行
javac TestPhoenixJDBC.java
java -cp "../ali-phoenix-<versions>-client.jar:." TestPhoenixJDBC