hbase(二)
hbase與hive的整合
資料儲存、查詢 資料分析 整合的目的: hbase中表的資料在hive中能夠查詢到 hive中表的資料在hbase中能夠查詢到 整合的步驟: 1、在hive中建立hbase能看到的表
create table if not exists hbase2hive( uid int, uname string, age int ) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties( "hbase.columns.mapping"=":key,f1:name,f1:age" ) tblproperties("hbase.table.name"="hh1") ;
出現錯誤: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org.apache.hadoop.hbase.HTableDescriptor.addFamily(Lorg/apache/hadoop/hbase/HColumnDescriptor;)V – 版本錯誤
解決方案: 將hive-hbase-handler的原始碼包重新打包,然後,將重新打的包以及依賴包都上傳到$HIVE_HOME/lib目錄中,再重啟hive
建立一個臨時表並匯入資料
載入資料:
insert into table hbase2hive select * from stu_score ;
2、hbase中已經存在表,並且存在資料
create external table if not exists hbase_user_info1( uid string, uname string, uage int ) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties( "hbase.columns.mapping"=":key,base_info:name,base_info:age" ) tblproperties("hbase.table.name"="ns1:t_user_info") ;
對映多列:
注意事項: 1、對映hbase中多列,要麼寫:key,要麼不寫,因為預設使用:key來匹配第一個欄位 2、hbase中表存在的時候,在hive中要建立對應的表時需要加關鍵字external 3、如果刪除hbase中的表時,在hive中不能查詢出資料 4、hbase中的列和hive中的列的個數和型別最好要一致(hive和hbase的表中欄位的陪陪關係不是按照欄位名來匹配的,而是按照順序來匹配) 5、hive和hbase、mysql等可以使用第三方的工具來相互整合資料,比如藍燈、shell指令碼
hbase的高階應用
1、協處理器(Coprocessor) 反向索引的需求 2、二級索引
繼承BaseRegionObserver類 實現prePut或者postPut方法 create ‘t_guanzhu’,‘cf1’,‘cf2’ create ‘t_fensi’,‘cf1’
將jar包上傳到hdfs之上 hdfs dfs -mkdir /hbaseObServer hdfs dfs -put gp1813Demo-1.0-SNAPSHOT.jar /hbaseObServer
將協處理器註冊到表上 alter ‘t_guanzhu’,METHOD => ‘table_att’,‘coprocessor’=>‘hdfs://qianfeng/hbaseObServer/gp1813Demo-1.0-SNAPSHOT.jar|com.qfedu.bigdata.hbaseObServer.InverIndexCoprocessor|1001|’
hbase的優化
hbase需要注意的事項: 屬性設定: memstore 的重新整理閥值: hbase.hregion.memstore.flush.size=134217728 128M region切分的閥值: hbase.hregion.max.filesize=10737418240 10G regionserver的操作執行緒數: hbase.regionserver.handler.count=30
hbase的優化
客戶端的優化: 1、關閉自動重新整理: htable.setAutoFlush(true/false) 2、儘量批量寫入(put、delete) 3、謹慎關閉Hlog: ht.setDurability(Durability.SKIP_WAL); 4、儘量把資料放到快取中 hc1.setInMemory(true); 5、儘量不要太多的列簇,最多兩個。(因為hbase在重新整理資料的時候會把相鄰的列簇也重新整理) 6、rowkey的長度儘量短。最大64K 7、儘量將該關閉的物件關閉。admin、table、resultScanner。