Phoenix 介紹和基本用法
阿新 • • 發佈:2019-02-04
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
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
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 classBaseDB {
/**
*
* 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