phoenix:鳳凰 hbase的jdbc工具 數據庫:schema //ns1 表:table //ns1.t1 sqlline.py s102,s103,s104 phoenix的jdbc編程: driver:org.apache.phoenix.jdbc.PhoenixDriver url: jdbc:phoenix:s102 name: null pass: null conn.setAutoCommit(); //設置自動提交 st.execute(""); unsigned_int //hbase integer //phoenix phoenix性質: ========================== 1、phoenix中默認所有名稱均為大寫,若想變為小寫,需要加上"" select * from "test"."t1"; 2、phoenix是大小寫區分的 create table "test"."t3"("id" integer primary key, id integer, "name" varchar, "age" integer); //create table "test"."t3"("id" integer primary key,id integer, "name" varchar, "age" integer); phoenix和hbase表映射: ============================ rowKey f1:callee f1:caller f1:duration f1:time f2:IDCard f2:location f2:signal ns1:t1表映射 ====================================== 1、創建hbase表 ns1:t1 ====>row1 f1:id, f1:name, f1:age row2 f1:id, f1:name, f1:age row3 f1:id, f1:name, f1:age 2、添加incr自增字段 ====> 目的是向hbase表中添加long型數據 incr 'ns1:t1','row1','f1:incr',1 3、phoenix表和hbase表做映射 create table "ns1:t1"("rowKey" varchar primary key, "f1"."id" varchar,"f1"."name" varchar,"f1"."age" varchar, "f1"."incr" unsigned_long ); create table "ns1:t1"("rowKey" varchar primary key, "f1"."id" varchar,"f1"."name" varchar,"f1"."age" varchar, "f1"."incr" bigint ); calllog表映射: =========================================== 1、將hbase的calllog表用phoenix表做映射 create table "calllog"("rowKey" varchar primary key, "f1"."caller" varchar, "f1"."callee" varchar, "f1"."duration" varchar,"f1"."time" varchar ,"f2"."IDCard" varchar, "f2"."location" varchar,"f2"."signal" varchar); 2、修改hbase表數據 upsert into "calllog" values('85,13800006520,20180109122356,0,15100005135','13800001111','15100005135','120','20180109122356','110121xxxxxxxxxxxxx','shahe'); 注意:在做表映射時,務必移除hbase表中所有協處理器。否則可能協處理器會沖突 總結:在創建表的時候,最好使用varchar字段或String(phoenix沒有) integer -integer.MAX_VALUE --- integer.MAX_VALUE unsigned_int 0 --- integer.MAX_VALUE //hbase bigint unsigned_long //hbase tinyint unsigned_tinyint //hbase phoenix視圖:只讀表 ======================================== 相當於偽表: 和表映射的區別在於視圖(view)不會被修改 1、向ns1:t3表創建phoenix視圖 create view "ns1:t3"("rowKey" varchar primary key, "f1"."id" varchar,"f1"."name" varchar,"f1"."age" varchar, "f1"."incr" unsigned_long); 2、刪除視圖 drop view "ns1:t3"; phoenix索引: ======================================= 提升掃描效率 索引的value,指向表的key,避免hbase全表掃描 0、修改hbase配置文件:/soft/hbase/conf/hbase-site.xml <property> <name>hbase.regionserver.wal.codec</name> <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value> </property> 配置完成后分發配置文件 1、重啟hbase 2、創建索引 create index calllog_index on "calllog"("caller") ; 3、刪除索引 drop index calllog_index on "calllog" ; 4、模糊查詢(避免全表掃描) select * from "calllog" where "f1"."caller" like '%111'; phoenix修改表: =============================== 表中添加字段 alter table "ns1:t2" add sex boolean; 視圖中添加字段 alter view "ns1:t3" add sex boolean; 表中刪除字段 alter table "ns1:t2" drop column sex ; 視圖中刪除字段 alter view "ns1:t3" drop column sex ; 創建schema ================================= 在phoenix中不能顯式創建schema, 可以用過創建表的方式創建schema。 phoenix函數:時間和時間戳互轉,時間與字符串互轉 ==================================== select now(); //0時區時間,date格式 select cast(now() as bigint) ; //將時間轉換成時間戳 select cast(1504934664380 as date) ; //將時間戳轉換成時間 //13位時間戳 select to_date('2017/09/09 02:03:04', 'yyyy/MM/dd hh:mm:ss') ; //將時間串轉換成時間 //時間串格式需要與pattern相同 select to_char(now() , 'yyyy/MM/dd') ; //將時間轉換成時間串,任意格式 phoenix簡單聚合函數: ====================================== sum() min() max() avg() select FIRST_VALUE ("age") WITHIN GROUP ( ORDER BY "age" desc); phoenix字符串函數: ======================================== substr() //截串 trim() //去空格