快速入门
Phoenix是一个开源的HBASE SQL层。Phoeinx可以用标准的JDBC API替代HBASE client API来创建表,插入和查询查询HBASE中的数据。
Phoenix作为应用层和HBASE之间的中间件,以下特性使它在大数据量的简单查询场景有着独有的优势
1. 二级索引支持(global index + local index)
2. 编译SQL成为原生HBASE的可并行执行的scan
3. 在数据层完成计算,server端的coprocessor执行聚合
4. 下推where过滤条件到server端的scan filter上
5. 利用统计信息优化、选择查询计划(5.x版本将支持CBO)
6. skip scan功能提高扫描速度
一般可以使用以下三种方式访问Phoenix
1. JDBC API
2. 使用Python编写的命令行工具(sqlline, sqlline-thin和psql等)
3. SQuirrel
一.命令行工具psql使用示例
1.创建一个建表的sql脚本文件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)
);
2. 创建csv格式的数据文件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
3. 创建一个查询sql脚本文件us_population_queries.sql:
SELECT state as "State",count(city) as "City Count",sum(population) as "Population Sum"FROM us_populationGROUP BY stateORDER BY sum(population) DESC;
4. 执行psql.py工具运行sql脚本:
./psql.py us_population.sql us_population.csv us_population_queries.sql
二.JDBC API使用示例
1. 使用Maven构建工程时,需要添加以下依赖
com.aliyun.phoenix
ali-phoenix-core
${version}
2. 创建名为test.java的文件
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.PreparedStatement;importjava.sql.Statement;publicclasstest{publicstaticvoidmain(String[] args)throwsSQLException{ 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(); }}
3.执行test.java
javac test.javajava -cp"../phoenix-[version]-client.jar:."test
三.SQuirrel使用示例
数据类型
目前Phoenix支持24种简单数据类型和1个一维Array的复杂类型。以下是对支持数据类型的说明:
DML语法
云HBASE上Phoenix支持的DML
1. SELECT