全部产品
云市场

HBase SQL(Phoenix) 入门

更新时间:2019-08-20 10:21:55

Phoenix 入门

本篇介绍简单的Phoenix使用方法,进行初步的数据查询。

在开始之前,请先确认您已经准备好了Phoenix的运行环境,若还没有,请参考这里

快速开始

  1. 创建一个us_population表
    1. CREATE TABLE IF NOT EXISTS us_population (
    2. state CHAR(2) NOT NULL,
    3. city VARCHAR NOT NULL,
    4. population BIGINT
    5. CONSTRAINT my_pk PRIMARY KEY (state, city));
  2. 写入数据

    1. UPSERT INTO us_population VALUES('NY','New York',8143197);
    2. UPSERT INTO us_population VALUES('CA','Los Angeles',3844829);
    3. UPSERT INTO us_population VALUES('IL','Chicago',2842518);
    4. UPSERT INTO us_population VALUES('TX','Houston',2016582);
    5. UPSERT INTO us_population VALUES('PA','Philadelphia',1463281);
    6. UPSERT INTO us_population VALUES('AZ','Phoenix',1461575);
    7. UPSERT INTO us_population VALUES('TX','San Antonio',1256509);
    8. UPSERT INTO us_population VALUES('CA','San Diego',1255540);
    9. UPSERT INTO us_population VALUES('TX','Dallas',1213825);
    10. UPSERT INTO us_population VALUES('CA','San Jose',912332);
  3. 查询SQL

    1. SELECT state as "State",count(city) as "City Count",sum(population) as "Population Sum"
    2. FROM us_population
    3. GROUP BY state
    4. ORDER BY sum(population) DESC;
  4. 结果验证

    result

API访问Phoenix JDBC

Phoenix 5.x SDK maven依赖

  1. <dependency>
  2. <groupId>com.aliyun.phoenix</groupId>
  3. <artifactId>ali-phoenix-queryserver-client</artifactId>
  4. <version>5.2.1-HBase-2.x</version>
  5. </dependency>

Phoenix 4.x SDK maven依赖

  1. <dependency>
  2. <groupId>com.aliyun.phoenix</groupId>
  3. <artifactId>ali-phoenix-core</artifactId>
  4. <version>${参考FAQ中最新版本说明}</version>
  5. </dependency>

代码示例请参考:云HBase Demo