全部产品
阿里云办公

Phoenix 入门

更新时间:2018-08-07 16:15:35

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

  • 使用Maven构建工程时,依赖如下
  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun.phoenix</groupId>
  4. <artifactId>ali-phoenix-core</artifactId>
  5. <!-- jdk7 compiled -->
  6. <version>4.11.0-AliHBase-1.1-0.3</version>
  7. </dependency>
  8. </dependencies>
  • 测试程序
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.PreparedStatement;
  6. import java.sql.Statement;
  7. /**
  8. * create table, create index, insert data, select table.
  9. */
  10. public class TestPhoenixJDBC {
  11. public static void main(String[] args) {
  12. try {
  13. Connection con =
  14. DriverManager.getConnection("jdbc:phoenix:n-proxy-pub-zk1, n-proxy-pub-zk2,n-proxy-pub-zk3");
  15. Statement stmt = con.createStatement();
  16. stmt.execute("drop table if exists test");
  17. stmt.execute("create table test (mykey integer not null primary key, mycolumn varchar)");
  18. stmt.execute("create index test_idx on test(mycolumn)");
  19. stmt.executeUpdate("upsert into test values (1,'World!')");
  20. stmt.executeUpdate("upsert into test values (2,'Hello')");
  21. stmt.executeUpdate("upsert into test values (3,'World!')");
  22. con.commit();
  23. PreparedStatement statement = con.prepareStatement("select mykey from test where mycolumn='Hello'");
  24. ResultSet rset = statement.executeQuery();
  25. while (rset.next()) {
  26. System.out.println(rset.getInt(1));
  27. }
  28. stmt.close();
  29. rset.close();
  30. con.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  • 编译执行
  1. javac TestPhoenixJDBC.java
  2. java -cp "../ali-phoenix-<versions>-client.jar:." TestPhoenixJDBC

输出:2