Phoenix 的安裝使用

Phoenix 的安裝使用

phoenix 圖解詳細安裝地址:https://www.cnblogs.com/zlslch/p/7096353.html
1. 什麼是Phoenix?
 phoenix,中文譯爲“鳳凰”,很美的名字。Phoenix是由saleforce.com開源的一個項目,後又捐給了Apache基金會。它相當於一個Java中間件,提供jdbc連接,操作hbase數據表。Phoenix是一個HBase的開源SQL引擎。你可以使用標準的JDBC API代替HBase客戶端API來創建表,插入數據,查詢你的HBase數據。Phoenix的團隊用了一句話概括Phoenix:“We put the SQL back in NoSQL” 意思是:我們把SQL又放回NoSQL去了!這邊說的NoSQL專指HBase,意思是可以用SQL語句來查詢Hbase,你可能會說:“Hive和Impala也可以啊!”。但是Hive和Impala還可以查詢文本文件,Phoenix的特點就是,它只能查Hbase,別的類型都不支持!但是也因爲這種專一的態度,讓Phoenix在Hbase上查詢的性能超過了Hive和Impala!
2. Phoenix性能
 Phoenix是構建在HBase之上的SQL引擎。你也許會存在“Phoenix是否會降低HBase的效率?”或者“Phoenix效率是否很低?”這樣的疑慮,事實上並不會,Phoenix通過以下方式實現了比你自己手寫的方式相同或者可能是更好的性能(更不用說可以少寫了很多代碼):
 1. 編譯你的SQL查詢爲原生HBase的scan語句
檢測scan語句最佳的開始和結束的key。
精心編排你的scan語句讓他們並行執行
推送你的WHERE子句的謂詞到服務端過濾器處理。
執行聚合查詢通過服務端鉤子(稱爲協同處理器)。
2.除此之外,Phoenix還做了一些有趣的增強功能來更多地優化性能:
實現了二級索引來提升非主鍵字段查詢的性能
統計相關數據來提高並行化水平,並幫助選擇最佳優化方案。
跳過掃描過濾器來優化IN,LIKE,OR查詢
優化主鍵的來均勻分佈寫壓力。

  1. Phoenix的安裝部署
    3.1、準備工作
    提前安裝好ZK集羣、hadoop集羣、Hbase集羣3.2、安裝包
    從對應的地址下載:http://mirrors.cnnic.cn/apache/phoenix/
    這裏我們使用的是:
    phoenix-4.8.2-HBase-1.2-bin.tar.gz
    下載地址:http://archive.apache.org/dist/phoenix/

在這裏插入圖片描述
3.3、上傳、解壓
將對應的安裝包上傳到對應的Hbase集羣其中一個服務器的一個目錄下
解壓:tar -zxvf phoenix-4.8.2-HBase-1.2-bin.tar.gz -C /var/local
重命名:mv phoenix-4.8.2-HBase-1.2-bin phoenix
3.4、配置(成功:將集羣的每臺機器上 下面倆個jar 拷貝,然後主機那臺機器上做(2)操作
(1) 將phoenix目錄下的phoenix-4.8.2-HBase-1.2-server.jar、
phoenix-core-4.8.2-HBase-1.2.jar拷貝到各個 hbase的lib目錄下。

(2)將hbase的配置文件hbase-site.xml、 hadoop/etc/hadoop下的core-site.xml 、hdfs-site.xml放到phoenix/bin/下,替換phoenix原來的配置文件。
(3)重啓hbase集羣,使Phoenix的jar包生效。
3.5、驗證是否成功
(1)在phoenix/bin下輸入命令:
端口可以省略
[root@itcast01 bin]# ./sqlline.py itcast01:2181
出現如下界面說明啓動成功
在這裏插入圖片描述
(2)輸入!tables 查看都有哪些表。紅框部分是用戶建的表,其他爲Phoenix系統表,系統表中維護了用戶表的元數據信息。
±-----------±-------------±---------------±--------------±---------±-----------±----------------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCIN |
±-----------±-------------±---------------±--------------±---------±-----------±----------------+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | |
| | SYSTEM | FUNCTION | SYSTEM TABLE | | | |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | |
| | SYSTEM | STATS | SYSTEM TABLE | | | | |
±-----------±-------------±---------------±--------------±---------±-----------±----------------+
(3)退出Phoenix。輸入!quit
在這裏插入圖片描述
4. Phoenix使用

Phoenix可以有4種方式調用:

批處理方式
命令行方式
GUI方式
JDBC調用方式
4.1 批處理方式
4.1.1 創建user_phoenix.sql文件
內容如下
CREATE TABLE IF NOT EXISTS user_phoenix ( state CHAR(2) NOT NULL, city VARCHAR NOT NULL, population BIGINT CONSTRAINT my_pk PRIMARY KEY (state, city));
4.1.2 創建user_phoenix.csv數據文件
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

4.1.3 創建user_phoenix_query.sql文件
內容爲:
SELECT state as “State”,count(city) as “City Count”,sum(population) as “Population Sum” FROM user_phoenix GROUP BY state ORDER BY sum(population) DESC;
4.1.4 執行

/var/local/phoenix/bin/psql.py itcast01:2181 user_phoenix.sql user_phoenix.csv user_phoenix_query.sql

這條命令同時做了三件事:創建表、插入數據、查詢結果
在這裏插入圖片描述
用Hbase shell 看下會發現多出來一個 USER_PHOENIX 表,用scan 命令查看一下這個表的數據
hbase(main):054:0> scan ‘USER_PHOENIX’
ROW COLUMN+CELL
AZPhoenix column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x16
MG
AZPhoenix column=0:_0, timestamp=1484736243467, value=x
CALos Angeles column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00:\xA
A\xDD
CALos Angeles column=0:_0, timestamp=1484736243467, value=x
CASan Diego column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x13
(t
CASan Diego column=0:_0, timestamp=1484736243467, value=x
CASan Jose column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x0D
\xEB\xCC
CASan Jose column=0:0, timestamp=1484736243467, value=x
ILChicago column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00+
\x
96
ILChicago column=0:_0, timestamp=1484736243467, value=x
PAPhiladelphia column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x16
S\xF1
PAPhiladelphia column=0:_0, timestamp=1484736243467, value=x
TXDallas column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x12
\x85\x81
TXDallas column=0:_0, timestamp=1484736243467, value=x
TXHouston column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x1E
\xC5F
TXHouston column=0:_0, timestamp=1484736243467, value=x
TXSan Antonio column=0:POPULATION, timestamp=1484736243467, value=\x80\x00\x00\x00\x00\x13
,=
TXSan Antonio column=0:_0, timestamp=1484736243467, value=x
結論:

之前定義的Primary key 爲state,city, 於是Phoenix就把輸入的state,city的值拼起來成爲rowkey
其他的字段還是按照列名去保存,默認的列族爲0
還有一個0:_0這個列是沒有值的,這個是Phoenix處於性能方面考慮增加的一個列,不用管這個列

4.2 命令行方式

4.2.1 執行命令
./sqlline.py itcast01:2181

4.2.2 可以進入命令行模式
0: jdbc:phoenix:itcast01>

然後執行相關的命令
4.2.3 退出命令行方式
執行 !quit

4.2.4 命令開頭需要一個感嘆號
使用help可以打印出所有命令
0: jdbc:phoenix:itcast01> help
!all Execute the specified SQL against all the current connections
!autocommit Set autocommit mode on or off
!batch Start or execute a batch of statements
!brief Set verbose mode off
!call Execute a callable statement
!close Close the current connection to the database
!closeall Close all current open connections
!columns List all the columns for the specified table
!commit Commit the current transaction (if autocommit is off)
!connect Open a new connection to the database.
!dbinfo Give metadata information about the database
!describe Describe a table
!dropall Drop all tables in the current database
!exportedkeys List all the exported keys for the specified table
!go Select the current connection
!help Print a summary of command usage
!history Display the command history
!importedkeys List all the imported keys for the specified table
!indexes List all the indexes for the specified table
!isolation Set the transaction isolation for this connection
!list List the current connections
!manual Display the SQLLine manual
!metadata Obtain metadata information
!nativesql Show the native SQL for the specified statement
!outputformat Set the output format for displaying results
(table,vertical,csv,tsv,xmlattrs,xmlelements)
0: jdbc:phoenix:itcast01> help
!all Execute the specified SQL against all the current connections
!autocommit Set autocommit mode on or off
!batch Start or execute a batch of statements
!brief Set verbose mode off
!call Execute a callable statement
!close Close the current connection to the database
!closeall Close all current open connections
!columns List all the columns for the specified table
!commit Commit the current transaction (if autocommit is off)
!connect Open a new connection to the database.
!dbinfo Give metadata information about the database
!describe Describe a table
!dropall Drop all tables in the current database
!exportedkeys List all the exported keys for the specified table
!go Select the current connection
!help Print a summary of command usage
!history Display the command history
!importedkeys List all the imported keys for the specified table
!indexes List all the indexes for the specified table
!isolation Set the transaction isolation for this connection
!list List the current connections
!manual Display the SQLLine manual
!metadata Obtain metadata information
!nativesql Show the native SQL for the specified statement
!outputformat Set the output format for displaying results
(table,vertical,csv,tsv,xmlattrs,xmlelements)
!primarykeys List all the primary keys for the specified table
!procedures List all the procedures
!properties Connect to the database specified in the properties file(s)
!quit Exits the program
!reconnect Reconnect to the database
!record Record all output to the specified file
!rehash Fetch table and column names for command completion
!rollback Roll back the current transaction (if autocommit is off)
!run Run a script from the specified file
!save Save the current variabes and aliases
!scan Scan for installed JDBC drivers
!script Start saving a script to a file
!set Set a sqlline variable

4.2.5 建立employee的映射表—數據準備
數據準備然後我們來建立一個映射表,映射我之前建立過的一個hbase表 employee.有2個列族 company、family
create ‘employee’,‘company’,‘family’
put ‘employee’,‘row1’,‘company:name’,‘ted’
put ‘employee’,‘row1’,‘company:position’,‘worker’
put ‘employee’,‘row1’,‘family:tel’,‘13600912345’
put ‘employee’,‘row2’,‘company:name’,‘michael’
put ‘employee’,‘row2’,‘company:position’,‘manager’
put ‘employee’,‘row2’,‘family:tel’,‘1894225698’
scan ‘employee’

建立映射表之前要說明的是,Phoenix是大小寫敏感的,並且所有命令都是大寫,如果你建的表名沒有用雙引號括起來,那麼無論你輸入的是大寫還是小寫,建立出來的表名都是大寫的,如果你需要建立出同時包含大寫和小寫的表名和字段名,請把表名或者字段名用雙引號括起來。
你可以建立讀寫的表或者只讀的表,他們的區別如下

讀寫表:如果你定義的列簇不存在,會被自動建立出來,並且賦以空值
只讀表:你定義的列簇必須事先存在

4.2.6 建立映射表
0: jdbc:phoenix:itcast01>CREATE TABLE IF NOT EXISTS “employee” (“no” VARCHAR(10) NOT NULL PRIMARY KEY, “company”.“name” VARCHAR(30),“company”.“position” VARCHAR(20), “family”.“tel” VARCHAR(20), “family”.“age” INTEGER);
這個語句有幾個注意點
IF NOT EXISTS可以保證如果已經有建立過這個表,配置不會被覆蓋
作爲rowkey的字段用 PRIMARY KEY標定
列簇用 columnFamily.columnName 來表示
family.age 是新增的字段,我之前建立測試數據的時候沒有建立這個字段的原因是在hbase shell下無法直接寫入數字型,使用UPSERT 命令插入數據的時候你就可以看到真正的數字型在hbase 下是如何顯示的
建立好後,查詢一下數據

4.2.7 查詢映射表數據
0: jdbc:phoenix:itcast01> SELECT * FROM “employee”;
±------±---------±----------±-------------±------+
| no | name | position | tel | age |
±------±---------±----------±-------------±------+
| row1 | ted | worker | 13600912345 | null |
| row2 | michael | manager | 1894225698 | null |
±------±---------±----------±-------------±------+

4.2.8 插入數據、更改數據

插入或者更改數據在phoenix中使用upsert關鍵字,
如果表中不存在該數據則插入,否則更新

插入:
0:jdbc:phoenix:itcast01>
upsert into “employee” values(‘row3’,‘billy’,‘worker’,‘16974681345’,33);
修改數據:
0: jdbc:phoenix:itcast01:2181>
upsert into “employee” (“no”,“tel”) VALUES (‘row2’,‘13588888888’);

查詢:
0: jdbc:phoenix:itcast01:2181> select * from “employee”;
±------±---------±----------±-------------±------+
| no | name | position | tel | age |
±------±---------±----------±-------------±------+
| row1 | ted | worker | 13600912345 | null |
| row2 | michael | manager | 13588888888 | null |
| row3 | billy | worker | 16974681345 | 33 |
±------±---------±----------±-------------±------+
3 rows selected (0.06 seconds)
4.2.9 查詢Hbase數據
hbase(main):056:0> scan ‘employee’
ROW COLUMN+CELL
row1 column=company:_0, timestamp=1484730892661, value=
row1 column=company:name, timestamp=1484730892527, value=ted
row1 column=company:position, timestamp=1484730892565, value=worker
row1 column=family:tel, timestamp=1484730892661, value=13600912345
row2 column=company:_0, timestamp=1484730892762, value=
row2 column=company:name, timestamp=1484730892702, value=michael
row2 column=company:position, timestamp=1484730892730, value=manager
row2 column=family:tel, timestamp=1484730892762, value=13588888888
row3 column=company:_0, timestamp=1484809036587, value=x
row3 column=company:name, timestamp=1484809036587, value=billy
row3 column=company:position, timestamp=1484809036587, value=worker
row3 column=family:age, timestamp=1484809036587, value=\x80\x00\x00!
row3 column=family:tel, timestamp=1484809036587, value=16974681345
company:_0這個列是沒有值的,這個是Phoenix處於性能方面考慮增加的一個列,不用管這個列。
4.3 GUI方式
4.3.1 squirrel下載
 從網址http://www.squirrelsql.org/下載相應版本的squirrel的安裝jar包,比如下載squirrel-sql-3.7-standard.jar window版本。
4.3.2 squirrel安裝
Window下:通過cmd進入window控制檯,
輸入 java -jar squirrel-sql-3.7-standard.jar

顯示安裝界面。
在這裏插入圖片描述
在這裏插入圖片描述

4.3.3 squirrel配置連接Phoenix
(1)配置squirrel
解壓的phoenix-4.7.0-HBase-1.1-bin.tar.gz包的主目錄下將如下幾個jar包拷貝到squirrel安裝目錄的lib下
在這裏插入圖片描述
在安裝目錄下雙擊squirrel-sql.bat、點擊左側的Drivers,添加圖標
在這裏插入圖片描述
配置說明:
 在出現的窗口中填寫如下項
 Name:就是個名字任意取就可以,這裏使用phoenix
  Example URL :jdbc:phoenix:itcast01:2181(這裏是你的phonenix的jdbc地址,注意端口也可以不寫,多個用逗號隔開)
 Class Name:org.apache.phoenix.jdbc.PhoenixDriver
  4.3.4 連接Phoenix
點擊Aiiasses,點擊右邊的添加圖標
在這裏插入圖片描述
配置說明:
 這裏還是名字隨意寫(這裏使用phoenix),driver要選擇剛纔配置的可用的driver,我們剛纔配置的是phoenix
  url這裏就是連接phonex的url選擇了phoenix的driver以後自動出現也可以改,user name就是phoenix連接的主機的用戶名,密碼就是該機器的密碼,點擊自動登錄
   然後點擊test,顯示連接成功即可(在這裏最好不要直接點OK,先點Test,連接成功了再OK)
   在這裏插入圖片描述
   點擊OK按鈕
   在這裏插入圖片描述
   查看對應的表
   在這裏插入圖片描述
   通過sql語句查詢數據
   在這裏插入圖片描述
   3.5 JDBC調用方式

打開Eclipse建立一個簡單的Maven項目 phoenix
在這裏插入圖片描述
pom.xml文件內容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.itcast</groupId>
  <artifactId>phoenix</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>phoenix</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency>
	<dependency>
	    <groupId>org.apache.phoenix</groupId>
	    <artifactId>phoenix-core</artifactId>
	    <version>4.7.0-HBase-1.1</version>
	</dependency>
   </dependencies>
</project>	

建立一個類 PhoenixManager
package cn.itcast.phoenix;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**

  • @author lishas
    */
    public class PhoenixManager {
    public static void main(String[] args) throws SQLException {
    Connection conn=null;
    Statement state=null;
    ResultSet rs=null;
    try {
    Class.forName(“org.apache.phoenix.jdbc.PhoenixDriver”);
    conn = DriverManager.getConnection(“jdbc:phoenix:itcast01:2181”);
    state=conn.createStatement();
    rs= state.executeQuery(“select * from “employee””);

     	    while(rs.next()){
     	    	System.out.println("no:"+rs.getString("no"));
     	    	System.out.println("name:"+rs.getString("name"));
     	    	System.out.println("position:"+rs.getString("position"));
     	    	System.out.println("age:"+rs.getInt("age"));
    

    System.out.println("-------------------------");
    }

     	} catch (Exception e) {
     		e.printStackTrace();
     		
     	}finally{
     		
     		if(rs!=null)rs.close();
     		
     		if(state!=null) state.close();
     		
     		if(conn!=null) conn.close();
     		
     	}
     	
     }
    

}

運行結果展現:
no:row1
name:ted
position:worker
age:0

no:row2
name:michael
position:manager
age:0

no:row3
name:billy
position:worker
age:33

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章