Hbase API中常用类介绍和使用
網上Hbase的介紹有很多,案例也不少。自己寫了個Demo,進行一些簡單的總結。
HBase?常用類介紹。
JAVA API?和?HBase數據庫模型之間的關系
| JAVA?類 | Hbase?數據模型 |
| HBaseAdmin | 數據庫(database) |
| HBaseConfiguration | |
| HTable | 表(table) |
| HTableDescriptor | 列族(Column Family) |
| Put | 行列操作 |
| Get | |
| Scanner |
?
下面說說JAVA API?提供的這些類的功能。和他們之間有什么樣的聯系。
1.HBaseConfiguration
關系:org.apache.hadoop.hbase.HBaseConfiguration
作用:通過此類可以對HBase進行配置
用法實例:?Configuration config = HBaseConfiguration.create();
說明:?HBaseConfiguration.create()?默認會從classpath?中查找?hbase-site.xml?中的配置信息,初始化?Configuration。
2.HBaseAdmin
關系:org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供接口關系HBase?數據庫中的表信息
用法:HBaseAdmin admin = new HBaseAdmin(config);
3.HTableDescriptor
關系:org.apache.hadoop.hbase.HTableDescriptor
作用:HTableDescriptor?類包含了表的名字以及表的列族信息
用法:HTableDescriptor htd =new HTableDescriptor(tablename);
???????????Htd.addFamily(new HColumnDescriptor(“myFamily”));
4.HColumnDescriptor
關系:org.apache.hadoop.hbase.HColumnDescriptor
作用:HColumnDescriptor?維護列族的信息
用法:HTableDescriptor htd =new HTableDescriptor(tablename);
???????????Htd.addFamily(new HColumnDescriptor(“myFamily”));
5.HTable
關系:org.apache.hadoop.hbase.client.HTable
作用:HTable?和?HBase?的表通信
用法:HTable tab = new HTable(config,Bytes.toBytes(tablename));
???????????ResultScanner sc = tab.getScanner(Bytes.toBytes(“familyName”));
說明:獲取表內列族?familyNme?的所有數據。
6.Put
關系:org.apache.hadoop.hbase.client.Put
作用:獲取單個行的數據
用法:HTable table = new HTable(config,Bytes.toBytes(tablename));
???????????Put put = new Put(row);
???????????p.add(family,qualifier,value);
說明:向表?tablename?添加?“family,qualifier,value”指定的值。
7.Get
關系:org.apache.hadoop.hbase.client.Get
作用:獲取單個行的數據
用法:HTable table = new HTable(config,Bytes.toBytes(tablename));
???????????Get get = new Get(Bytes.toBytes(row));
???????????Result result = table.get(get);
說明:獲取?tablename?表中?row?行的對應數據
8.ResultScanner
關系:Interface
作用:獲取值的接口
用法:ResultScanner scanner = table.getScanner(Bytes.toBytes(family));
???????????For(Result rowResult : scanner){
???????????????????Bytes[] str = rowResult.getValue(family,column);
}
說明:循環獲取行中列值。
下面例子使用的就是上面提供的類和接口。
例子1:
/**
?????*?獲取表中所有數據
?????*/
????@SuppressWarnings("unchecked")
????publicstatic?List<Map> getDateAll(String tablename){
???????ResultScanner rs =?null;
???????HTable table??=?null;
???????try?{
???????????table =?new?HTable(cfg,tablename);
???????????Scan s =?new?Scan();
???????????//掃描全表,性能不佳
???????????rs = table.getScanner(s);
???????????for(Result r=rs.next();r!=null;r=rs.next()){??????????????????????for(KeyValue kv : r.raw()){
??????????????????System.out.println(new?String(kv.getValue()));
??????????????}
???????????}
???????}?catch?(Exception e) {
???????????returnnull;
???????}finally{
???????????rs.close();
???????}
???????return?list;
????}
HBase是大數據的分布式數據庫,當使用全表掃描肯定是不合理。下面的例子相比較例子1做些優化。
例子2
/**
?????*?指定rowkey的開始和結束掃描表數據
?????*/
????@SuppressWarnings("unchecked")
????publicstatic?List<Map> getDateAll(String tablename){
???????... //篇幅原因省略
???????try?{
???????????table =?new?HTable(cfg,tablename);
???????????Scan s =?new?Scan();
???????????//通過rowkey來指定數據開始和結束,性能上較例子1高很多?
????????????s.setStartRow(Bytes.toBytes(“2012-12-22”));
???????????s.setStopRow(Bytes.toBytes(“2012-12-23”));
???????????rs = table.getScanner(s);
???????????... //篇幅原因省略
???????}?catch?(Exception e) {
???????????...//篇幅原因省略
????}
當使用掃描器?scan.setStartRow(Bytes)和scan.setStopRow(Bytes)查詢的數據還不能滿足結果集的話,下面的一些類就派上用場了,他就是Filter。
客戶端請求過濾器
A.??????逐一說一下Filter。
1.???????FilterList??
FilterList?代表一個過濾器列表,過濾器間具有
FilterList.Operator.MUST_PASS_ALL?和
FilterList.Operator.MUST_PASS_ONE?的關系,下面展示一個過濾器的?“或”關系。
下面FilterList?列表中檢查同一屬性的'value1'?或'value2'?。
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes(“cfamily”), Bytes.toBytes(“column”),CompareOp.EQUAL,Bytes.toBytes("value1"));
list.add(filter1);
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes(“cfamily”), Bytes.toBytes(“column”), CompareOp.EQUAL, Bytes.toBytes("value2"));
List.add(filter2);
?
2.???????SingleColumnValueFilter???
SingleColumnValueFilter?用于測試列值相等?(CompareOp.EQUAL ),?不等(CompareOp.NOT_EQUAL),或范圍?(e.g., CompareOp.GREATER).?下面示例檢查列值和字符串'my values'?相等...
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(“cFamily”), Bytes.toBytes(“column”), CompareOp.EQUAL, Bytes.toBytes("values")); scan.setFilter(filter);3.???????ColumnPrefixFilter
ColumnPrefixFilter?用于指定列名前綴值相等
Byte[] prefix = Bytes.toBytes(“values”); Filter f = new ColumnPrefixFilter(prefix); scan.setFilter(f);4.???????MultipleColumnPrefixFilter
MultipleColumnPrefixFilter?和?ColumnPrefixFilter?行為差不多,但可以指定多個前綴。
byte[][] prefixes = new byte[][] {Bytes.toBytes("value1"), Bytes.toBytes("value2")}; Filter f = new MultipleColumnPrefixFilter(prefixes); scan.setFilter(f);5.???????QualifierFilter
QualifierFilter?是基于列名的過濾器。
Filter f = new QualifierFilter(“QualifierName”); scan.setFilter(f);6.???????RowFilter
RowFilter?是rowkey過濾器,通常根據rowkey來指定范圍時,使用scan掃描器的StartRow和StopRow?方法比較好。Rowkey也可以使用。
Filter f = new RowFilter(“rowkey”); scan.setFilter(f);B.比較器
7.???????RegexStringComparator
RegexStringComparator?是支持正則表達式的比較器。
過濾器配合上比較器會很方便。看下面的代碼。
解釋一下:代碼中綠色字體標注的代碼就是正則比較器的使用方法。參數?reg?就是正則驗證的規則。
HTable table = new HTable(cfg,"datainfo"); Scan scan = new Scan(); String reg = "^136([0-9]{8})$";//滿足136開頭的手機號 RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(reg)); scan.setFilter(filter); ResultScanner rs = table.getScanner(scan); for(Result rr : rs){ for(KeyValue kv : rr.raw()){ ???????? ... } }8.???????SubstringComparator
SubstringComparator?用于檢測一個子串是否存在于值中。大小寫不敏感。
//檢測values 是否存在于查詢的列值中 SubstringComparator comp = new SubstringComparator("values"); SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(“family”), Bytes.toBytes(“column”),CompareOp.EQUAL, Bytes.toBytes(“value”)); scan.setFilter(filter);總結
以上是生活随笔為你收集整理的Hbase API中常用类介绍和使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HBase简单代码实例(Java)
- 下一篇: 配置Xmanager连接linux