开发者社区> 问答> 正文

Phoenix 入门



Phoenix 数据分析入门


本篇介绍简单的Phoenix使用方法,进行初步的数据分析。
在开始之前,请先确认您已经准备好了Phoenix的运行环境,若还没有,请 参考这里

快速开始

  1. 创建一个 us_population.sql 文件,用来创建表结构,内容如下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));


准备分析用数据
数据文件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

分析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;

执行分析 现在我们执行如下的语句来进行分析 ./psql.py <your_zookeeper_quorum> us_population.sql us_population.csv us_population_queries.sql

将其中的ZooKeeper的地址替换成我们的ZooKeeper的地址,类似 zk1,zk2,zk3
结果验证
成功运行后会看到类似如下的结果

API访问Phoenix JDBC

  • 使用Maven构建工程时,依赖如下<dependencies>
  •     <dependency>
  •         <groupId>com.aliyun.phoenix</groupId>
  •         <artifactId>ali-phoenix-core</artifactId>
  •         <version>4.11.0-AliHBase-1.1-0.1</version>
  •     </dependency>
  • </dependencies>

  • 测试程序import java.sql.Connection;
  • import java.sql.DriverManager;
  • import java.sql.ResultSet;
  • import java.sql.SQLException;
  • import java.sql.PreparedStatement;
  • import java.sql.Statement;
  • /**
  • * create table, create index, insert data, select table.
  • */
  • public class TestPhoenixJDBC {
  •     public static void main(String[] args) {
  •         try {
  •             Connection con =
  •                     DriverManager.getConnection("jdbc:phoenix:n-proxy-pub-zk1, n-proxy-pub-zk2,n-proxy-pub-zk3");
  •             Statement stmt = con.createStatement();
  •             stmt.execute("drop table if exists test");
  •             stmt.execute("create table test (mykey integer not null primary key, mycolumn varchar)");
  •             stmt.execute("create index test_idx on test(mycolumn)");
  •             stmt.executeUpdate("upsert into test values (1,'World!')");
  •             stmt.executeUpdate("upsert into test values (2,'Hello')");
  •             stmt.executeUpdate("upsert into test values (3,'World!')");
  •             con.commit();
  •             PreparedStatement statement = con.prepareStatement("select mykey from test where mycolumn='Hello'");
  •             ResultSet rset = statement.executeQuery();
  •             while (rset.next()) {
  •                 System.out.println(rset.getInt(1));
  •             }
  •             stmt.close();
  •             rset.close();
  •             con.close();
  •         } catch (SQLException e) {
  •             e.printStackTrace();
  •         }
  •     }
  • }


  • 编译执行
    javac TestPhoenixJDBC.java
    java -cp “../phoenix-4.11.0-AliHBase-1.1-0.1-client.jar:.” TestPhoenixJDBC
    输出:2

展开
收起
云栖大讲堂 2017-11-02 16:28:44 1886 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Phoenix在客服系统的实践 立即下载
HBase应用实践专场-HBase for Solr 立即下载
HBase3.0展望以及可能的新特性 立即下载

相关实验场景

更多