使用Phoenix连接hbase数据库
来源:互联网 发布:适合冥想的音乐 知乎 编辑:程序博客网 时间:2024/05/16 17:31
本文在Phoenix及hbase均已安装成功的前提下。在eclipse中使用jdbc连接hbase数据库。
导入所需jar包
<dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core</artifactId> <version>4.7.0-HBase-1.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.1.2</version> </dependency>
编写连接hbase的工具类
package com.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class PhoenixUtil { public static Connection getConnection(){ try { Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); Connection connection = null; connection=DriverManager.getConnection("此处为zookeeper地址,可以为一个或多个:zookeeper的端口号:zookeeper.znode.parent(此处默认为 /hbase默认可不填写)"); //connection=DriverManager.getConnection("jdbc:phoenix:192.168.23.44,192.168.23.43,192.168.23.45:2181:/hbase-unsecure"); return connection; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void closeConnection(Connection conn){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
编写增删改查的测试类
package com.test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;import com.util.PhoenixUtil;public class HbaseTest { /** * 创建表 */ @Test public void createTable() { Connection conn = PhoenixUtil.getConnection(); String sql = "CREATE TABLE user (id varchar PRIMARY KEY,name varchar ,password varchar)"; try { PreparedStatement sta = conn.prepareStatement(sql); sta.execute(); System.out.println("create success...."); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { PhoenixUtil.closeConnection(conn); } } } /** * 插入数据 */ @Test public void insertData() { Connection conn = PhoenixUtil.getConnection(); String sql = "upsert into user(id, name, password) values('001', 'admin', 'admin')"; try { PreparedStatement ps = conn.prepareStatement(sql); String msg = ps.executeUpdate() > 0 ? "insert success..." : "insert fail..."; conn.commit(); System.out.println(msg); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { PhoenixUtil.closeConnection(conn); } } } /** * 查询表 */ @Test public void findData() { Connection conn = PhoenixUtil.getConnection(); String sql = "select * from user"; try { PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); System.out.println("id" + "\t" + "name" + "\t" + "password"); System.out.println("=========================="); if (rs != null) { while (rs.next()) { System.out.print(rs.getString("id") + "\t"); System.out.print(rs.getString("name") + "\t"); System.out.println(rs.getString("password")); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { PhoenixUtil.closeConnection(conn); } } } /** * 删除表 */ @Test public void dropTable() { Connection conn = PhoenixUtil.getConnection(); String sql = "drop table user"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.execute(); System.out.println("drop success..."); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { PhoenixUtil.closeConnection(conn); } } } /** * 删除数据 */ @Test public void delData() { Connection conn = PhoenixUtil.getConnection(); String sql = "delete from user where id='001'"; try { PreparedStatement ps = conn.prepareStatement(sql); String msg = ps.executeUpdate() > 0 ? "delete success..." : "delete fail..."; conn.commit(); System.out.println(msg); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { PhoenixUtil.closeConnection(conn); } } }}
完整资源连接:phoenix连接hbase完整代码
阅读全文
0 0
- 使用Phoenix连接hbase数据库
- 使用Phoenix连接Hbase
- DbVisualizer使用Phoenix连接Hbase
- 使用Phoenix连接HBase-0.96.0
- Phoenix Jdbc 连接HBase
- Phoenix安装使用及使用 SQuirrel客户端连接操作Hbase
- Phoenix+HBase使用心得
- Phoenix+HBase使用心得
- hbase使用Phoenix
- Phoenix使用JAVA编程连接HBase-0.96.0
- Phoenix安装、连接Hbase、配置
- Phoenix 使用sql查询hbase
- Phoenix基本使用(HBase)
- hbase SQL 框架phoenix使用
- 数据库操作:Jdbc,mysql,oracle,hbase,phoenix
- python使用jdbc连接phoenix
- 使用Apache Phoenix 实现 SQL 操作HBase
- Phoenix 使用脚本方式导入 hbase
- 运维 10/05 日志
- Java I/O
- Visitor in Java
- node-pre-gyp ERR! node-pre-gyp -xxxxxxxxxxxx
- word2vec 在 非 自然语言处理 (NLP) 领域的应用
- 使用Phoenix连接hbase数据库
- 任学堂:人工智能会写作、会玩游戏、会作曲,留给人类的工作还有什么?
- 最小生成树 普利姆算法代码C#实现
- Ubuntu 16.04 安装 IDEA
- Mybatis中的update动态SQL语句
- 友盟iOS推送为什么有时会出现生产环境下收不到的情况?
- Spring Validator解读
- 尼姆博奕5754Life Winner Bo
- Spring Boot创建一个RESTful Web Service