[Phoenix使用文檔係列] 一、快速入門
Phoenix是一個開源的HBASE SQL層。Phoeinx可以用標準的JDBC API替代HBASE client API來創建表,插入和查詢查詢HBASE中的數據。
Phoenix作為應用層和HBASE之間的中間件,以下特性使它在大數據量的簡單查詢場景有著獨有的優勢
- 二級索引支持(global index + local index)
- 編譯SQL成為原生HBASE的可並行執行的scan
- 在數據層完成計算,server端的coprocessor執行聚合
- 下推where過濾條件到server端的scan filter上
- 利用統計信息優化、選擇查詢計劃(5.x版本將支持CBO)
- skip scan功能提高掃描速度
一般可以使用以下三種方式訪問Phoenix
- JDBC API
- 使用Python編寫的命令行工具(sqlline, sqlline-thin和psql等)
- 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_population
GROUP BY state
ORDER BY sum(population) DESC;
4. 執行psql.py工具運行sql腳本
./psql.py <your_zookeeper_quorum> us_population.sql us_population.csv us_population_queries.sql
二、JDBC API使用示例
1. 使用Maven構建工程時,需要添加以下依賴
<dependencies>
<dependency>
<groupId>com.aliyun.phoenix</groupId>
<artifactId>ali-phoenix-core</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
2. 創建名為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();
}
}
3.執行test.java
javac test.java
java -cp "../phoenix-[version]-client.jar:." test
三、SQuirrel使用示例
參考這裏
最後更新:2017-11-16 14:04:13