Phoenix是构建在HBase上的一个SQL层,能让我们用标准的JDBC APIs而不是HBase客户端APIs来创建表,插入数据和对HBase数据进行查询。Phoenix完全使用Java编写,作为HBase内嵌的JDBC驱动。Phoenix查询引擎会将SQL查询转换为一个或多个HBase扫描,并编排执行以生成标准的JDBC结果集。
Download:http://phoenix.apache.org/download.html,下载hbase对应版本的phoenix;解压bin.tar.gz包,拷贝phoenix server jar包到hbase集群的每个region server 的lib目录下,然后重启hbase 集群。
1. 连接hbase: bin/sqlline.py 192.168.31.10:2181
HBase集群配置zookeeper集群的ip地址和端口。
利用!help 查看所有command。
2. 创建表: create table test (id varchar primary key,name varchar,age integer );
phoenix:
hbase:
Hbase是区分大小写的,Phoenix 默认会把sql语句中的小写转换成大写,再建表,如果不希望转换,需要将表名,字段名等使用引号。Hbase默认phoenix表的主键对应到ROW,column family 名为0,也可以在建表的时候指定column family
phoenix:
hbase:
3. 插入数据
upsert into test(id,name,age) values(‘000001’,’liubei’,43);
phoenix:
hbase:
4. 其他语法
见官网 http://phoenix.apache.org/language/index.html
5. 使用JDBC的方式来对HBase中的数据进行CRUD操作。
将phoenix-4.2.2-client.jar(可以在下载的phoenix-4.2.2-bin.tar.gz中找到该jar包)添加到项目的classpath中,将HBase集群的hbase-site.xml配置文件添加到项目的resources目录下。为了查看日志输出配置了一个简单的log4j.properties,也一并放到resources目录下。
log4j.properties 内容如下:
log4j.rootLogger=WARN, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5.1 获得连接
/** * project:hadoop-phoenix * file:BaseDB.java * time:2015年5月4日 下午2:19:57 * description: */ package cn.com.dimensoft.hadoop.phoenix.jdbc; import java.sql.Connection; import java.sql.DriverManager; /** * class: BaseDB * package: cn.com.dimensoft.hadoop.phoenix.jdbc * time: 2015年5月4日 下午2:19:57 * description: */ public class BaseDB { /** * * name:getConnection * time:2015年5月6日 下午2:07:06 * description: get JDBC connection * @return connection */ public static Connection getConnection() { try { // load driver Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); // get connection // jdbc 的 url 类似为 jdbc:phoenix [ :<zookeeper quorum> [ :<port number> ] [ :<root node> ] ], // 需要引用三个参数:hbase.zookeeper.quorum、hbase.zookeeper.property.clientPort、and zookeeper.znode.parent, // 这些参数可以缺省不填而在 hbase-site.xml 中定义。 return DriverManager.getConnection("jdbc:phoenix"); } catch (Exception e) { e.printStackTrace(); return null; } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
5.2 创建表
/** * * name:create * time:2015年5月4日 下午2:58:31 * description:create table */ public static void create() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // check if the table exist ResultSet rs = conn.getMetaData().getTables(null, null, "USER", null); if (rs.next()) { System.out.println("table user is exist..."); return; } // create sql String sql = "CREATE TABLE user (id varchar PRIMARY KEY,INFO.account varchar ,INFO.passwd varchar)"; PreparedStatement ps = conn.prepareStatement(sql); // execute ps.execute(); System.out.println("create success..."); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
5.3 插入数据
/** * * name:insert * time:2015年5月4日 下午2:59:11 * description: */ public static void upsert() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "upsert into user(id, INFO.account, INFO.passwd) values('001', 'admin', 'admin')"; PreparedStatement ps = conn.prepareStatement(sql); // execute upsert String msg = ps.executeUpdate() > 0 ? "insert success..." : "insert fail..."; // you must commit conn.commit(); System.out.println(msg); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
5.4 查询数据
/** * * name:query * time:2015年5月4日 下午3:58:12 * description:query data */ public static void query() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "select * from user"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); System.out.println("id" + "\t" + "account" + "\t" + "passwd"); System.out.println("======================"); if (rs != null) { while (rs.next()) { System.out.print(rs.getString("id") + "\t"); System.out.print(rs.getString("account") + "\t"); System.out.println(rs.getString("passwd")); } } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
5.5 更新数据
更新数据的操作与插入数据相同
- 1
5.6 删除数据
/** * * name:delete * time:2015年5月4日 下午4:03:11 * description:delete data */ public static void delete() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "delete from user where id='001'"; PreparedStatement ps = conn.prepareStatement(sql); // execute upsert String msg = ps.executeUpdate() > 0 ? "delete success..." : "delete fail..."; // you must commit conn.commit(); System.out.println(msg); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
5.7 删除表
/** * * name:drop * time:2015年5月4日 下午4:03:35 * description:drop table */ public static void drop() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "drop table user"; PreparedStatement ps = conn.prepareStatement(sql); // execute ps.execute(); System.out.println("drop success..."); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
参考链接:https://blog.csdn.net/maomaosi2009/article/details/45582321
5. 使用JDBC的方式来对HBase中的数据进行CRUD操作。
将phoenix-4.2.2-client.jar(可以在下载的phoenix-4.2.2-bin.tar.gz中找到该jar包)添加到项目的classpath中,将HBase集群的hbase-site.xml配置文件添加到项目的resources目录下。为了查看日志输出配置了一个简单的log4j.properties,也一并放到resources目录下。
log4j.properties 内容如下:
log4j.rootLogger=WARN, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5.1 获得连接
/** * project:hadoop-phoenix * file:BaseDB.java * time:2015年5月4日 下午2:19:57 * description: */ package cn.com.dimensoft.hadoop.phoenix.jdbc; import java.sql.Connection; import java.sql.DriverManager; /** * class: BaseDB * package: cn.com.dimensoft.hadoop.phoenix.jdbc * time: 2015年5月4日 下午2:19:57 * description: */ public class BaseDB { /** * * name:getConnection * time:2015年5月6日 下午2:07:06 * description: get JDBC connection * @return connection */ public static Connection getConnection() { try { // load driver Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); // get connection // jdbc 的 url 类似为 jdbc:phoenix [ :<zookeeper quorum> [ :<port number> ] [ :<root node> ] ], // 需要引用三个参数:hbase.zookeeper.quorum、hbase.zookeeper.property.clientPort、and zookeeper.znode.parent, // 这些参数可以缺省不填而在 hbase-site.xml 中定义。 return DriverManager.getConnection("jdbc:phoenix"); } catch (Exception e) { e.printStackTrace(); return null; } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
5.2 创建表
/** * * name:create * time:2015年5月4日 下午2:58:31 * description:create table */ public static void create() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // check if the table exist ResultSet rs = conn.getMetaData().getTables(null, null, "USER", null); if (rs.next()) { System.out.println("table user is exist..."); return; } // create sql String sql = "CREATE TABLE user (id varchar PRIMARY KEY,INFO.account varchar ,INFO.passwd varchar)"; PreparedStatement ps = conn.prepareStatement(sql); // execute ps.execute(); System.out.println("create success..."); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
5.3 插入数据
/** * * name:insert * time:2015年5月4日 下午2:59:11 * description: */ public static void upsert() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "upsert into user(id, INFO.account, INFO.passwd) values('001', 'admin', 'admin')"; PreparedStatement ps = conn.prepareStatement(sql); // execute upsert String msg = ps.executeUpdate() > 0 ? "insert success..." : "insert fail..."; // you must commit conn.commit(); System.out.println(msg); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
5.4 查询数据
/** * * name:query * time:2015年5月4日 下午3:58:12 * description:query data */ public static void query() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "select * from user"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); System.out.println("id" + "\t" + "account" + "\t" + "passwd"); System.out.println("======================"); if (rs != null) { while (rs.next()) { System.out.print(rs.getString("id") + "\t"); System.out.print(rs.getString("account") + "\t"); System.out.println(rs.getString("passwd")); } } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
5.5 更新数据
更新数据的操作与插入数据相同
- 1
5.6 删除数据
/** * * name:delete * time:2015年5月4日 下午4:03:11 * description:delete data */ public static void delete() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "delete from user where id='001'"; PreparedStatement ps = conn.prepareStatement(sql); // execute upsert String msg = ps.executeUpdate() > 0 ? "delete success..." : "delete fail..."; // you must commit conn.commit(); System.out.println(msg); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
5.7 删除表
/** * * name:drop * time:2015年5月4日 下午4:03:35 * description:drop table */ public static void drop() { Connection conn = null; try { // get connection conn = BaseDB.getConnection(); // check connection if (conn == null) { System.out.println("conn is null..."); return; } // create sql String sql = "drop table user"; PreparedStatement ps = conn.prepareStatement(sql); // execute ps.execute(); System.out.println("drop success..."); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
参考链接:https://blog.csdn.net/maomaosi2009/article/details/45582321