Phoneix(一)簡介及經常使用命令

1、簡介python

Apache Phoneix是運行在HBase之上的高性能關係型數據庫,經過Phoneix能夠像使用jdbc訪問關係型數據庫同樣訪問HBase。算法

Phoneix操做的表以及數據存儲在HBase上,phoneix只須要和HBase進行表關聯。而後在用工具進行一些讀寫操做。sql

能夠把Phoneix只當作一種代替HBase語法的工具。雖然Java能夠用jdbc來鏈接phoneix操做,可是在生成環境找那個,不能夠用OLTP。phoenix在查詢hbase時,雖然作了一些優化,可是延遲仍是不小。因此依然用在OLAT中,在將結果返回存儲下來。shell

說明:數據庫

  當今的數據處理大體能夠分紅兩大類:聯機事務處理OLTP(on-line transaction processing)、聯機分析處理OLAP(On-Line Analytical Processing)。OLTP是傳統的關係型數據庫的主要應用,主要是基本的、平常的事務處理,例如銀行交易。OLAP是數據倉庫系統的主要應用,支持複雜的分析操做,側重決策支持,而且提供直觀易懂的查詢結果.數組

 

2、經常使用命令緩存

 一、登陸phoneix shellapp

python sqlline.py master:2181

二、基本命令函數

查看table列表  !tables
查看錶字段信息  !describe tablename
查看執行歷史  !history
查看table 的索引  !index tablename
其餘操做 help

三、插入數據工具

在Phoneix是沒有Insert語句的,取而代之的是upsert 。
Upsert有兩種用法:
1、upsert into旨在單條插入
upsert into tb values('ak','hhh',222)
upsert into tb(stat,city,num) values('ak','hhh',222)
2、upsert select旨在批量插入
upsert into tb1 (state,city,population) select state,city,population from tb2 where population < 40000;
upsert into tb1 select state,city,population from tb2 where population > 40000;
upsert into tb1 select * from tb2 where population > 40000;

     注意:

    在Phoenix在插入語句並不會像傳統數據庫同樣存在重複數據,由於phoneix是構建在HBase之上的,也就是主鍵惟一。後面插入數據會覆蓋前面的,可是時間戳不同。

四、刪除數據

delete from tb; 
清空表中全部記錄,Phoenix中不能使用truncate table tb;
delete from tb where city = 'kenai';
drop table tb;刪除表
delete from system.catalog where table_name = 'int_s6a';
drop table if exists tb;
drop table my_schema.tb;
drop table my_schema.tb cascade;用於刪除表的同時刪除基於該表的全部視圖。

五、更新數據

因爲HBase的主鍵設計,相同rowkey的內容能夠直接覆蓋,這就變相的更新了數據。
因此Phoenix的更新操做仍舊是upsert into 和 upsert select
upsert into us_population (state,city,population) values('ak','juneau',40711);

六、查詢數據

union all, group by, order by, limit 都支持
select * from test limit 1000;
select * from test limit 1000 offset 100;
select full_name from sales_person where ranking >= 5.0
union all select reviewer_name from customer_review where score >= 8.0

七、建立表

a、加鹽(SALT_BUCKETS)

加鹽Salting可以經過預分區(pre-splitting)數據到多個region中來顯著提高讀寫性能。本質是在hbase中,rowkey的byte數組的第一個字節位置設定一個系統生成的byte值,這個byte值是由rowkey的byte數組作一個哈希算法,計算來的。

SALT_BUCKETS的值範圍在(1-256):

create table TEST1(host varchar not null primary key, description  varchar)salt_buckets=16;
upsert into TEST1 (host,description) values ('192.168.0.1','s1');
upsert into TEST1 (host,description) values ('192.168.0.2','s2');
upsert into TEST1 (host,description) values ('192.168.0.3','s3');

salted table能夠自動在每個rowkey前面加上一個字節,這樣對於一段連續的rowkeys,
它們在表中實際存儲時,就被自動地分佈到不一樣的region中去了。當指定要讀寫該段區間內的數據時,
也就避免了讀寫操做都集中在同一個region上。簡而言之,若是咱們用Phoenix建立了一個saltedtable,
那麼向該表中寫入數據時,原始的rowkey的前面會被自動地加上一個byte(不一樣的rowkey會被分配不一樣的byte),
使得連續的rowkeys也能被均勻地分佈到多個regions。

結果:如圖;

b、Pre-split(預分區)

Salting可以自動的設置表預分區,可是咱們得控制表是如何分區的,因此在使用phoneix建表時,能夠精確的指定要根據什麼值來作預分區,以下實例:

create table TEST2 (host varchar not null primary key, description varchar) split on ('cs','eu','na');

c、使用多列簇

列簇包含相關的數據都在獨立的文件中,在Phoneix設置多個列簇能夠提升查詢性能。

以下建表語句,建立了a,b兩個列簇

create table TEST3 ( mykey varchar not null primary key, a.col1 varchar, a.col2 varchar,  b.col3 varchar);
upsert into TEST3 values ('key1','a1','b1','c1');
upsert into TEST3 values ('key2','a2','b2','c2');

d、使用壓縮

create table test (host varchar not null primary key, description varchar) compression='snappy';

八、建立視圖,刪除視圖

create view "my_hbase_table"( k varchar primary key, "v" unsigned_long) default_column_family='a';
create view my_view ( new_col smallint ) as select * from my_table where k = 100;
create view my_view_on_view as select * from my_view where new_col > 70;
create view v1 as select *  from test where description in ('s1','s2','s3');
drop view my_view;
drop view if exists my_schema.my_view;
drop view if exists my_schema.my_view cascade;

九、建立索引

建立二級索引支持可變數據和不可變數據(數據插入後再也不更新)上創建二級索引

create index my_idx on opportunity(last_updated_date desc);全局索引
create index my_idx on event(created_date desc) include (name, payload) salt_buckets=10;覆蓋索引並加鹽
create index my_idx on sales.opportunity(upper(contact_name));函數索引
create index test_index on test (host) include (description);覆蓋索引

十、刪除索引

drop index 索引名 on 表名;

十一、默認是可變表,手動建立不可變表

create table hao2 (k varchar primary key, v varchar) immutable_rows=true;
alter table HAO2 set IMMUTABLE_ROWS = false;    修改成可變
alter index index1 on tb rebuild;索引重建是把索引表清空後從新裝配數據。

十二、Global Indexing多讀少寫,適合條件較少

CREATE INDEX IPINDEX ON TEST(IP);
調用方法:強制索引 SELECT
/*+ INDEX(TEST IPINDEX) */ * FROM TEST WHERE IP='139.204.122.144';

1三、覆蓋索引 Covered Indexes,須要include包含須要返回數據結果的列

create index index1_c on hao1 (age) include(name);  name已經被緩存在這張索引表裏了。
對於select name from hao1 where age=2,查詢效率和速度最快
select * from hao1 where age =2,其餘列不在索引表內,會全表掃描

1四、Local Indexing寫多讀少,不是索引字段索引表也會被使用,索引數據和真實數據存儲在同一臺機器上

CREATE LOCAL INDEX IPINDEX ON TEST(IP);

相關文章
相關標籤/搜索