Phoenix 数据分析入门
本篇介绍简单的Phoenix使用方法,进行初步的数据分析。
在开始之前,请先确认您已经准备好了Phoenix的运行环境,若还没有,请
参考这里
快速开始
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
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
<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();
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
在开始使用Phoenix进行数据分析之前,请确保您已准备好以下环境: - Phoenix运行环境:包括HBase、Zookeeper等必要的服务。如果尚未配置,请参考相关文档进行设置。
创建表结构
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)
);
准备数据
us_population.csv
,包含各城市的州名、城市名和人口数量。编写分析查询
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;
执行分析
sqlline.py
)执行上述脚本和查询。确保替换ZooKeeper地址为您的实际地址,例如:
./sqlline.py zk1,zk2,zk3 < us_population.sql
然后导入数据并执行查询脚本。
结果验证
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访问。