日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

HBase 基本Java API

發(fā)布時(shí)間:2023/11/30 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HBase 基本Java API 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)庫:HBaseConfiguration ?HBaseAdmin ?

表:HTable ?HTableDescriptor

列族:HColumnDescriptor

行列操作:Put ?Get ?Scanner


HBaseConfiguration:

Configuration create()

//從classpath中查找hbase-site.xml初始化Configuration,如果沒有,默認(rèn)讀取HBase-版本號(hào).jar中的默認(rèn)配置文件

void merger(Configuration destConf, Configuration srcConf) ?//合并兩個(gè)Configuration

用法:Configuration config = HBaseConfiguration.create();


HBaseAdmin:

管理HBase數(shù)據(jù)庫信息,可以創(chuàng)建表、刪除表、列出表項(xiàng)、使表有效或無效、添加刪除列族

void addColumn (String tableName, HColumnDescriptor column) ?//添加列族

void checkHBaseAvailable (HBaseConfiguration conf) ?//查看HBase狀態(tài),靜態(tài)函數(shù)

void createTable (HTableDescriptor desc) ?//創(chuàng)建一個(gè)新表,同步操作

void deleteTable (byte[] tableName) //刪除表

void enableTable (byte[] tableName) //使表有效

void disableTable (byte[] tableName) //使表無效

HTableDescriptor[] listTable() ?//列出所有用戶空間表項(xiàng)

void modifyTable (byte[] tableName, HTableDescriptor htd) ?//修改表模式,異步操作,耗時(shí)

boolean tableExists (String tableName); //檢查表是否存在

用法:HBaseAdmin admin = new HBaseAdmin(config); ? ?admin.disableTable(Bytes.toBytes("tablename"));


HTableDescriptor:

包含了表的名字及列族

void addFamily (HColumnDescriptor cdesc) ?//添加一個(gè)列族

HColumnDescriptor removeFamily ( byte[] column) ?//移除一個(gè)列族

byte[] getName () ?//獲取表名

byte[] getValue (byte[] key) ?//獲取屬性值

void setValue (String key, String value ) ?//設(shè)置屬性值

用法:

HTableDescriptor htd = new HTableDescriptor ("tableName");

htd.addFamily (new HColumnDescriptor("Family"));


HColumnDescriptor:

維護(hù)列族信息,如版本號(hào)、壓縮設(shè)置等,通常在創(chuàng)建表或添加列族時(shí)使用

列族被創(chuàng)建后不能直接修改,只能刪除后重建

列族被刪除后,對(duì)應(yīng)數(shù)據(jù)一同被刪除

byte[] getName() ?//獲取列族名字

byte[] getValue() ?//獲取對(duì)應(yīng)屬性值

void setValue (String key, String value) ?//設(shè)置對(duì)應(yīng)屬性值


HTable:

用來與HBase表進(jìn)行通信,非線程安全,若有多個(gè)線程,使用HTablePool

void checkAndPut (byte[] row, byte[] family, byte[] quaalifier, byte[] value, Put put) ?//自動(dòng)檢查row/family/qualifier/是否與給定的值匹配

void close() ?//釋放資源

boolean exists (Get get) ?//檢查get鎖指定的值是否存在

Result get(Get get) ?//取出指定行的某些單元格對(duì)應(yīng)的值

byte[][] getEndKeys () ?//獲取表每個(gè)Region的結(jié)束鍵值

ResultScanner getScanner (byte[] family) ?//獲取給定列族的Scanner實(shí)例

HTableDescriptor getTableDescriptor() ?//獲取當(dāng)前表的HTableDescriptor實(shí)例

byte[] getTableName() ?//獲取表名

void put (Put put) ?//向表中添加值

用法:

HTable table = new HTable (conf, Bytes.toBytes("tablename"));

ResultScanner scanner = table.getScanner (Bytes.toBytes("cf"));


Put:

添加數(shù)據(jù)

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) ?//返回與指定 列族:列 匹配的項(xiàng)

boolean has (byte[] family, byte[] qualifier) ?//檢查是否有?列族:列

用法:

HTable table = new HTable(conf, Bytes.toBytes("tablename"));

Put p = new Put(Bytes.toBytes("row")); //為指定行建一個(gè)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")); //為指定行建一個(gè)Get操作

Result result = table.get(g);


Result:

存儲(chǔ)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中直接取出一個(gè)特定的value//遍歷出result中所有的鍵值對(duì)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"));//前綴過濾器----針對(duì)行鍵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);/*** 假設(shè)rowkey格式為:創(chuàng)建日期_發(fā)布日期_ID_TITLE* 目標(biāo):查找 發(fā)布日期 為 2014-12-21 的數(shù)據(jù)*/rf = new RowFilter(CompareOp.EQUAL , new SubstringComparator("_2014-12-21_"));//單值過濾器 1 完整匹配字節(jié)數(shù)組new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "zhangsan".getBytes());//單值過濾器2 匹配正則表達(dá)式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);//鍵值對(duì)元數(shù)據(jù)過濾-----family過濾----字節(jié)數(shù)組完整匹配FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL , new BinaryComparator(Bytes.toBytes("base_info")) //表中不存在inf列族,過濾結(jié)果為空);//鍵值對(duì)元數(shù)據(jù)過濾-----family過濾----字節(jié)數(shù)組前綴匹配ff = new FamilyFilter(CompareOp.EQUAL , new BinaryPrefixComparator(Bytes.toBytes("inf")) //表中存在以inf打頭的列族info,過濾結(jié)果為該列族所有行);//鍵值對(duì)元數(shù)據(jù)過濾-----qualifier過濾----字節(jié)數(shù)組完整匹配filter = new QualifierFilter(CompareOp.EQUAL , new BinaryComparator(Bytes.toBytes("na")) //表中不存在na列,過濾結(jié)果為空);filter = new QualifierFilter(CompareOp.EQUAL , new BinaryPrefixComparator(Bytes.toBytes("na")) //表中存在以na打頭的列name,過濾結(jié)果為所有行的該列數(shù)據(jù));//基于列名(即Qualifier)前綴過濾數(shù)據(jù)的ColumnPrefixFilterfilter = new ColumnPrefixFilter("na".getBytes());//基于列名(即Qualifier)多個(gè)前綴過濾數(shù)據(jù)的MultipleColumnPrefixFilterbyte[][] prefixes = new byte[][] {Bytes.toBytes("na"), Bytes.toBytes("me")};filter = new MultipleColumnPrefixFilter(prefixes);//為查詢?cè)O(shè)置過濾條件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中取到某個(gè)特定的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();}}





總結(jié)

以上是生活随笔為你收集整理的HBase 基本Java API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。