开发者社区 问答 正文

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 1944 分享 版权
阿里云 AI 助理回答

Phoenix 数据分析入门指南

准备环境

在开始使用Phoenix进行数据分析之前,请确保您已准备好以下环境: - Phoenix运行环境:包括HBase、Zookeeper等必要的服务。如果尚未配置,请参考相关文档进行设置。

快速开始步骤

  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,包含各城市的州名、城市名和人口数量。
  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. 执行分析

    • 使用Phoenix的命令行工具(如sqlline.py)执行上述脚本和查询。确保替换ZooKeeper地址为您的实际地址,例如:
      ./sqlline.py zk1,zk2,zk3 < us_population.sql
      

      然后导入数据并执行查询脚本。

  5. 结果验证

    • 成功执行后,您将看到按人口总数降序排列的各州城市数量和人口总和。

API访问与JDBC示例

  • Maven依赖配置 对于Phoenix 4.x版本,添加以下依赖到您的Maven项目中:

    <dependency>
    <groupId>com.aliyun.phoenix</groupId>
    <artifactId>ali-phoenix-core</artifactId>
    <version>4.11.0-AliHBase-1.1-0.1</version>
    </dependency>
    
  • 测试程序代码 编写Java程序以通过JDBC连接Phoenix并执行基本操作,如创建表、插入数据和查询数据。示例代码如下:

    import java.sql.*;
    
    public class TestPhoenixJDBC {
      public static void main(String[] args) {
          try {
              Connection con = DriverManager.getConnection("jdbc:phoenix:zk1,zk2,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();
          }
      }
    }
    
  • 编译与执行 编译并运行上述Java程序:

    javac TestPhoenixJDBC.java
    java -cp "path/to/your/phoenix-client.jar:." TestPhoenixJDBC
    

    其中,path/to/your/phoenix-client.jar应替换为您的Phoenix客户端JAR包的实际路径。

完成以上步骤后,您将成功地使用Phoenix进行了数据表的创建、数据插入以及基本的数据分析查询,并通过API实现了对Phoenix的JDBC访问。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答