HBase之phoenix部署及使用
1. 背景
经常会使用HBase进行查询等操作,hbase shell
不太方便,装一个phoenix试试。
phoenix语法
2. 部署
# 下载地址,找到对应HBase版本的 http://archive.apache.org/dist/phoenix/
# 将phoenix目录下的phoenix-4.8.2-HBase-1.2-server.jar、phoenix-core-4.8.2-HBase-1.2.jar拷贝到各个 hbase的lib目录下。
# 将hbase的配置文件hbase-site.xml、 hadoop下的配置文件core-site.xml 、hdfs-site.xml放到phoenix/bin/下。
# 重启hbase集群,使Phoenix的jar包生效
3. shell使用
连接
# 连接到HBase集群配置zookeeper集群的ip地址和端口
bin/sqlline.py node1:2181
3.1 建表
# 注意数据类型不支持int ; primary key对应为hbase表的rowkey
create table test_phoenix(id varchar primary key, name varchar, age integer);
在hbase shell
查看表结构,有一些协处理器;Phoenix 默认会把sql语句中的小写转换成大写,如果不希望转换,需要将表名,字段名等使用引号。
desc 'TEST_PHOENIX'
phoenix表的列族名默认为0,也可以在建表的时候指定列族
create table test_phoenix2(id varchar primary key, info.name varchar, info.age integer);
3.2 插入数据
# phoenix中插入数据用upsert
upsert into test_phoenix2 values('t001', 'jacob', 14);
在hbase shell
中查看
scan 'TEST_PHOENIX2'
3.3 查询数据
select * from test_phoenix2
3.4 退出phoenix窗口
!quit
4. JDBC使用
maven依赖
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-client</artifactId>
<version>4.15.0-HBase-1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
代码
public class PhoenixDemo {
Connection conn = null;
@Before
public void init() throws IOException {
Properties prop = new Properties();
prop.put("hbase.zookeeper.quorum", "node1,node2,node3");
prop.put("hbase.zookeeper.property.clientPort", "2181");
try {
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
conn = DriverManager.getConnection("jdbc:phoenix");
} catch (Exception e) {
e.printStackTrace();
}
}
// 创建hbase表
@Test
public void createTable() throws SQLException {
String sql = "create table test_phoenix3(id varchar primary key, info.name varchar, info.age integer)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.execute();
System.out.println("创建成功");
}
@Test
public void upsertTable() throws SQLException {
String sql = "upsert into test_phoenix3 values('t001', 'jacob', 14)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.executeUpdate();
// 必须commit
conn.commit();
System.out.println("插入成功");
}
@Test
public void selectTable() throws SQLException {
String sql = "select * from test_phoenix3";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("id")
+ " " + rs.getString("name")
+ " " + rs.getInt("age")
);
}
}
@After
public void release() {
if (null != conn) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
5. 问题及解决办法
5.1 环境变量缺失
Failed to locate the winutils binary in the hadoop binary path
win10添加环境变量HADOOP_HOME
5.2
Can't find method newStub in org.apache.phoenix
将HBase client依赖注掉
<!-- <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<!-- <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>-->
分类:
大数据 / hbase
标签:
大数据/hbase
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~