Hyperbase常用SQL


1、創建表
1.1 建HBase內表 
CREATE TABLE hbase_inner_table(
  key1 string,
  bi bigint,
  dc decimal(10,2),
  ch varchar(10),
  ts timestamp,
  en string
)STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler';
-- 創建了一張名為 hbase_inner_table 的內表,存儲格式為 HBaseStorageHandler 。

1.2 建HBase外表 
CREATE EXTERNAL TABLE hbase_external_table(
  key1 string,
  ex1 double,
  ex3 date,
  ex5 string
)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping"=":key,f:q1,f:q4,f:q5") ① 
TBLPROPERTIES ("hbase.table.name"="test.hbase_inner_table");② 
-- ① 指定外表 hbase_external_table 和已存在的 hbase_inner_table 表的列映射關系。
-- ② 指定外表 hbase_external_table 中的列與源表的映射關系。映射時數據類型強行轉換,轉換失敗則 為NULL。

2、為內表 hbase_inner_table 添加列
ALTER TABLE hbase_inner_table ADD COLUMNS (bl boolean);
-- 對表hbase_inner_table添加了一個數據類型為 boolean 列bl。

3、清空內表 hbase_inner_table
TRUNCATE TABLE hbase_inner_table;
-- 表中數據被清空,但表的元數據信息仍存在,可通過 DESCRIBE FORMATTED 查看。

4、刪除表
DROP TABLE <tableName>;

5、創建索引
5.1 為內表 hbase_inner_table 創建全局索引
CREATE GLOBAL INDEX ch_global ON hbase_inner_table(ch(10));
-- 根據列ch創建一個名為ch_global的全局索引,並指定該索引字段的長度為10.

5.2 為內表 hbase_inner_table 創建全文索引
CREATE FULLTEXT INDEX ON hbase_inner_table(bi,ch,en) SHARD NUM 1;
-- 為內表hbase_inner_table根據列en、cn來創建全文索引

6、刪除索引
6.1 刪除全局索引:ch_global
DROP INDEX ch_global ON hbase_inner_table;

6.2 刪除內表 hbase_inner_table 的全文索引
DROP FULLTEXT INDEX ON hbase_inner_table;
-- 目前HBase不支持使用SQL生成索引,您可以從hbase shell中執行 rebuild 指令來生成索引

7、插入數據
7.1 向hbase_inner_table表中單條插入數據
INSERT INTO hbase_inner_table
VALUES('001',1,1.01,'Hyperbase','2017-01-08 20:31:46','sunday',true);
INSERT INTO hbase_inner_table
VALUES('002',2,2.01,'transwarp hbase','2017-01-09 10:25:45','monday',true);
INSERT INTO hbase_inner_table
VALUES('003',3,3.01,'hbase','2017-01-10 15:05:20','tuesday',false);

7.2 向hbase_inner_table表中批量插入數據
BATCHINSERT INTO hbase_inner_table BATCHVALUES (
VALUES('004',4,4.01,'esdrive','2017-01-11 15:05:20','wednesday',false),
VALUES('005',5,5.01,'transwarp es','2017-01-12 15:18:18','thursday',false),
VALUES('006',6,6.01,'hyperdrive','2017-01-13 05:13:13','friday',false),
VALUES('007',7,7.01,'inceptor','2017-01-14 10:55:20','saturday',false),
VALUES('008',8,8.01,'fulltext','2017-01-15 17:23:40','tuestuesday',false)
);

8、更新數據
update hbase_inner_table set bl=false where key1='001';

9、刪除記錄
DELETE FROM hbase_inner_table WHERE key1='001';

10、利用索引查詢
set ngmr.exec.mode=local;
-- 該參數設置完成后才能利用索引進行有效的查詢

10.1 全局索引
10.1.1 用全局索引ch_global查詢
select/*+USE_INDEX(t1 USING ch_global)*/ * from hbase_inner_table t1 where ch='hbase'and bl=false;
-- 利用全局索引‘ch_global’查詢列ch中值為‘hbase’,且列bl值為false的記錄。

10.1.2 不使用全局索引進行查詢
select/*+USE_INDEX(t1 USING NOT_USE_INDEX)*/ key1, bi, dc, ch, ts, en, bl from hbase_inner_table t1 where ch='hbase'and bl=false;
-- 不利用索引查詢列ch值為‘hbase’,且列bl值為false的記錄。

10.2 全文索引
10.2.1 精確匹配(term)
select * from hbase_inner_table where contains(en, "term 'tuesday'");

10.2.2 前綴匹配(prefix)
select * from hbase_inner_table where contains(en, "prefix 'tues'");

10.2.3 模糊查詢(wildcard)
select * from hbase_inner_table where contains(en, "wildcard 'tues*day'");
-- 查詢 \* 前的字符 tues出現任意次,且以 day結尾記錄。

10.2.4 多個操作符查詢
select * from hbase_inner_table where contains(en, "wildcard 'tues*day'") and contains(bi,"term '3'");
 
10.2.5 in表達式(in)//枚舉
select * from hbase_inner_table where contains(en, "in 'sunday,monday'");

10.2.6 正則表達式(regexp)
select * from hbase_inner_table where contains(en, "regexp 's.*y'");
-- 查詢en列滿足正則表達式為 's.*y' 的記錄,.* 表示 s和 y間可出現任意個字符。

10.2.7 全文檢索(match)
select * from hbase_inner_table where contains(en, "match 'tuesday'");
-- 不要對非 STRING 類型進行模糊、前綴、正則等查詢。如果是對數字類型的進行范圍查詢,則需保證該列的數據類型為 #b 才可以

10.2.8 范圍查詢
select * from hbase_inner_table where contains(bi, "> '6'");

10.2.9 范圍表達式(range)
select * from hbase_inner_table where contains(bi, "range '[1,3)'");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM