首页 > 技术文章 > [Phoenix] 一、快速入门

hbase-community 2018-03-19 22:31 原文

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

  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_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使用示例

参考这里

转载: https://yq.aliyun.com/articles/253038


资料

HBase:https://pan.baidu.com/s/1jILzgns

知乎HBase讨论:https://www.zhihu.com/topic/19600820/hot

hbase-help:http://hbase-help.com/

CSDN HBase资料库:http://lib.csdn.net/hbase/node/734

这些资料是笔者整理,以供有大规模结构化需求的用户及HBase爱好者学习交流,以使用HBase更好的解决实际的问题。

交流

如果大家对HBase有兴趣,致力于使用HBase解决实际的问题,欢迎加入Hbase技术社区群交流:

 

 

​  钉钉HBase技术社区群

 

推荐阅读