數據庫:HBaseConfiguration ?HBaseAdmin ?
表:HTable ?HTableDescriptor
列族:HColumnDescriptor
行列操作:Put ?Get ?Scanner
HBaseConfiguration:
Configuration create()
//從classpath中查找hbase-site.xml初始化Configuration,如果沒有,默認讀取HBase-版本號.jar中的默認配置文件
void merger(Configuration destConf, Configuration srcConf) ?//合并兩個Configuration
用法:Configuration config = HBaseConfiguration.create();
HBaseAdmin:
管理HBase數據庫信息,可以創建表、刪除表、列出表項、使表有效或無效、添加刪除列族
void addColumn (String tableName, HColumnDescriptor column) ?//添加列族
void checkHBaseAvailable (HBaseConfiguration conf) ?//查看HBase狀態,靜態函數
void createTable (HTableDescriptor desc) ?//創建一個新表,同步操作
void deleteTable (byte[] tableName) //刪除表
void enableTable (byte[] tableName) //使表有效
void disableTable (byte[] tableName) //使表無效
HTableDescriptor[] listTable() ?//列出所有用戶空間表項
void modifyTable (byte[] tableName, HTableDescriptor htd) ?//修改表模式,異步操作,耗時
boolean tableExists (String tableName); //檢查表是否存在
用法:HBaseAdmin admin = new HBaseAdmin(config); ? ?admin.disableTable(Bytes.toBytes("tablename"));
HTableDescriptor:
包含了表的名字及列族
void addFamily (HColumnDescriptor cdesc) ?//添加一個列族
HColumnDescriptor removeFamily ( byte[] column) ?//移除一個列族
byte[] getName () ?//獲取表名
byte[] getValue (byte[] key) ?//獲取屬性值
void setValue (String key, String value ) ?//設置屬性值
用法:
HTableDescriptor htd = new HTableDescriptor ("tableName");
htd.addFamily (new HColumnDescriptor("Family"));
HColumnDescriptor:
維護列族信息,如版本號、壓縮設置等,通常在創建表或添加列族時使用
列族被創建后不能直接修改,只能刪除后重建
列族被刪除后,對應數據一同被刪除
byte[] getName() ?//獲取列族名字
byte[] getValue() ?//獲取對應屬性值
void setValue (String key, String value) ?//設置對應屬性值
HTable:
用來與HBase表進行通信,非線程安全,若有多個線程,使用HTablePool
void checkAndPut (byte[] row, byte[] family, byte[] quaalifier, byte[] value, Put put) ?//自動檢查row/family/qualifier/是否與給定的值匹配
void close() ?//釋放資源
boolean exists (Get get) ?//檢查get鎖指定的值是否存在
Result get(Get get) ?//取出指定行的某些單元格對應的值
byte[][] getEndKeys () ?//獲取表每個Region的結束鍵值
ResultScanner getScanner (byte[] family) ?//獲取給定列族的Scanner實例
HTableDescriptor getTableDescriptor() ?//獲取當前表的HTableDescriptor實例
byte[] getTableName() ?//獲取表名
void put (Put put) ?//向表中添加值
用法:
HTable table = new HTable (conf, Bytes.toBytes("tablename"));
ResultScanner scanner = table.getScanner (Bytes.toBytes("cf"));
Put:
添加數據
Put add (byte[] family, byte[] quaalifier, byte[] value)
Put add (byte[] family, byte[] quaalifier, long ts, byte[] value)
List<Key Value> get (byte[] family, byte[] qualifier) ?//返回與指定 列族:列 匹配的項
boolean has (byte[] family, byte[] qualifier) ?//檢查是否有?列族:列
用法:
HTable table = new HTable(conf, Bytes.toBytes("tablename"));
Put p = new Put(Bytes.toBytes("row")); //為指定行建一個Put操作
p.add(Bytes.toBytes("family"),?Bytes.toBytes("qulifier"),?Bytes.toBytes("tvalue"));
table.put(p);
Get:
獲取單行信息
Get addColumn (byte[] family, byte[] qualifier) ?
Get addFamily (byte[] family)
Get setTimeRange (long minStamp, long maxStamp)
Get setFilter (Filter filter)
用法:
HTable table = new HTable(conf, Bytes.toBytes("tablename"));
Get g = new Get(Bytes.toBytes("row")); //為指定行建一個Get操作
Result result = table.get(g);
Result:
存儲Get或Scan操作后獲取的表的單行值
boolean containsColumn (byte[] family, byte[] qualifier)
NavigableMap<byte[], byte[]> getFamilyMap (byte[] family) ?//返回列族的qulifier:value們
byte[] getValue (byte[] family, byte[] qualifier)?
ResultScanner:
void close()
Result next()
用法:
ResultScanner scanner = table.getScanner (Bytes.toBytes(family));
for ( Result rowResult : scanner )?
? ? byte[] str = rowResult.getValue ( Bytes.toBytes("family"), Bytes.toBytes("column") )
import java.util.List;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.master.TableNamespaceManager;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;public class HbaseDemo {private Configuration conf = null;@Beforepublic void init(){conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "hdp0,hdp1,hdp2");}@Testpublic void testDrop() throws Exception{HBaseAdmin admin = new HBaseAdmin(conf);admin.disableTable("account");admin.deleteTable("account");admin.close();}@Testpublic void testPut() throws Exception{HTable table = new HTable(conf, "person_info");Put p = new Put(Bytes.toBytes("person_rk_bj_zhang_000002"));p.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());table.put(p);table.close();}@Testpublic void testGet() throws Exception{HTable table = new HTable(conf, "person_info");Get get = new Get(Bytes.toBytes("person_rk_bj_zhang_000001"));get.setMaxVersions(5);Result result = table.get(get);List<Cell> cells = result.listCells();// result.getValue(family, qualifier); 可以從result中直接取出一個特定的value//遍歷出result中所有的鍵值對for(KeyValue kv : result.list()){String family = new String(kv.getFamily());System.out.println(family);String qualifier = new String(kv.getQualifier());System.out.println(qualifier);System.out.println(new String(kv.getValue()));}table.close();}/*** 多種過濾條件的使用方法* @throws Exception*/@Testpublic void testScan() throws Exception{HTable table = new HTable(conf, "person_info".getBytes());Scan scan = new Scan(Bytes.toBytes("person_rk_bj_zhang_000001"), Bytes.toBytes("person_rk_bj_zhang_000002"));//前綴過濾器----針對行鍵Filter filter = new PrefixFilter(Bytes.toBytes("rk"));//行過濾器ByteArrayComparable rowComparator = new BinaryComparator(Bytes.toBytes("person_rk_bj_zhang_000001"));RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);/*** 假設rowkey格式為:創建日期_發布日期_ID_TITLE* 目標:查找 發布日期 為 2014-12-21 的數據*/rf = new RowFilter(CompareOp.EQUAL , new SubstringComparator("_2014-12-21_"));//單值過濾器 1 完整匹配字節數組new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "zhangsan".getBytes());//單值過濾器2 匹配正則表達式ByteArrayComparable comparator = new RegexStringComparator("zhang.");new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);//單值過濾器2 匹配是否包含子串,大小寫不敏感comparator = new SubstringComparator("wu");new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);//鍵值對元數據過濾-----family過濾----字節數組完整匹配FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL , new BinaryComparator(Bytes.toBytes("base_info")) //表中不存在inf列族,過濾結果為空);//鍵值對元數據過濾-----family過濾----字節數組前綴匹配ff = new FamilyFilter(CompareOp.EQUAL , new BinaryPrefixComparator(Bytes.toBytes("inf")) //表中存在以inf打頭的列族info,過濾結果為該列族所有行);//鍵值對元數據過濾-----qualifier過濾----字節數組完整匹配filter = new QualifierFilter(CompareOp.EQUAL , new BinaryComparator(Bytes.toBytes("na")) //表中不存在na列,過濾結果為空);filter = new QualifierFilter(CompareOp.EQUAL , new BinaryPrefixComparator(Bytes.toBytes("na")) //表中存在以na打頭的列name,過濾結果為所有行的該列數據);//基于列名(即Qualifier)前綴過濾數據的ColumnPrefixFilterfilter = new ColumnPrefixFilter("na".getBytes());//基于列名(即Qualifier)多個前綴過濾數據的MultipleColumnPrefixFilterbyte[][] prefixes = new byte[][] {Bytes.toBytes("na"), Bytes.toBytes("me")};filter = new MultipleColumnPrefixFilter(prefixes);//為查詢設置過濾條件scan.setFilter(filter);scan.addFamily(Bytes.toBytes("base_info"));ResultScanner scanner = table.getScanner(scan);for(Result r : scanner){/**for(KeyValue kv : r.list()){String family = new String(kv.getFamily());System.out.println(family);String qualifier = new String(kv.getQualifier());System.out.println(qualifier);System.out.println(new String(kv.getValue()));}*///直接從result中取到某個特定的valuebyte[] value = r.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));System.out.println(new String(value));}table.close();}@Testpublic void testDel() throws Exception{HTable table = new HTable(conf, "user");Delete del = new Delete(Bytes.toBytes("rk0001"));del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));table.delete(del);table.close();}public static void main(String[] args) throws Exception {Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "hdp0:2181,hdp1:2181,hdp2:2181");HBaseAdmin admin = new HBaseAdmin(conf);TableName tableName = TableName.valueOf("person_info");HTableDescriptor td = new HTableDescriptor(tableName);HColumnDescriptor cd = new HColumnDescriptor("base_info");cd.setMaxVersions(10);td.addFamily(cd);admin.createTable(td);admin.close();}}