大数据开发进阶笔记——HBase的常用原子操作Java代码

来源:互联网 发布:中国港口数据 编辑:程序博客网 时间:2024/05/05 10:13

HBase的常用原子操作Java代码:

 

Append:追加,类似于put操作。

Configurationconf =HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn=HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

Append append = newAppend(Bytes.toBytes(“r1”));

append.add(Bytes.toBytes(“d”), Bytes.toBytes(“a”),Bytes.toBytes(“abcd”));

table.append(append);

table.close();

conn.close();

 

checkAndPut:检查是否存在参数描述的记录,如果存在,则不写入数据,如果不存在就写入数据。注意:是对某一个row_key的原子操作,不能跨row_key。

Configurationconf =HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn=HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

Put put = new Put(Bytes.toBytes(“r1”));

put.add(Bytes.toBytes(“d”), Bytes.toBytes(“b”),Bytes.toBytes(“bbbb”));

//不会写入,因为之前追加过有记录

table.checkAndPut(Bytes.toBytes(“r1”), Bytes.toBytes(“d”),Bytes.toBytes(“a”), Bytes.toBytes(“abcd”),put);// 参数:row_key,family,column,row_value

//会写入

table.checkAndPut(Bytes.toBytes(“r1”), Bytes.toBytes(“d”),Bytes.toBytes(“a”), Bytes.toBytes(“ad”),put);

table.close();

conn.close();

 

 

checkAndDelete:

Configurationconf =HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn=HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

Delete delete = newDelete(Bytes.toBytes(“r1”));

delete.deleteColumns(Bytes.toBytes(“d”), Bytes.toBytes(“a”));

//不会被删

table.checkAndDelete(Bytes.toBytes(“r1”), Bytes.toBytes(“d”),Bytes.toBytes(“a”), Bytes.toBytes(“b”),delete);//参数:是否有row_key,是否有column,是否有row_value,删除对象

//会被删

table.checkAndDelete(Bytes.toBytes(“r1”), Bytes.toBytes(“d”),Bytes.toBytes(“a”), Bytes.toBytes(“bbbb”),delete);

table.close();

conn.close();

 

计数器

Configurationconf = HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn=HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

lone result = table.incrementColumnValue(Bytes.toBytes(“r1”),Bytes.toBytes(“d”), Bytes.toBytes(“b”), 10);//10是追加计数的值

lone result =table.incrementColumnValue(Bytes.toBytes(“r1”), Bytes.toBytes(“d”), Bytes.toBytes(“b”),0);// 0是追加计数的值

lone result = table.incrementColumnValue(Bytes.toBytes(“r1”),Bytes.toBytes(“d”), Bytes.toBytes(“b”), -5);

lone result =table.incrementColumnValue(Bytes.toBytes(“r1”), Bytes.toBytes(“d”), Bytes.toBytes(“b”),+5);

System.out.print(result);

table.close();

conn.close();

 

Hbase重要组件——过滤器

Hbase过滤器:Filter:

在Hbase服务端将客户需要过滤掉数据过滤掉,减少传输到客户端的数据量,减轻负担。

//对row_key进行过滤

Configuration conf =HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn= HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

Scan scan = new Scan();

Scan.setCacheBlocks(flase);//产生的数据不会放到Blockcache 里

//Filter filter = newRowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(“abc”)));//过滤出row_key

//Filter filter = newRowFilter(CompareFilter.CompareOp.EQUAL,newBinaryPrefixComparator(Bytes.toBytes( “ab”)));//过滤出row_key前缀包含ab的

Filter filter = newRowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator ( “b”));//row_key包含b的被过滤出来

scan.setFilter(filter);

ResultScannerscanner = table.getScanner(scan);

for(Result result: scanner){

List<Cell> cell = result.listCells();

for(Cell cell:cells){

   String rowkey = newString(CellUnit.cloneRow(cell));

   String family = newString(CellUnit.cloneFamily(cell));

   String column = newString(CellUnit.clonecolumn(cell));

   String value = newString(CellUnit.clonecolumn(cell));

   System.out.println(rowkey+””+family+” ”+column+” ”+value);

 

}

}

scanner.close();

table.close();

conn.close();

 

//对column进行过滤

Configuration conf =HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn=HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

Scan scan = new Scan();

Scan.setCacheBlocks(flase);//产生的数据不会放到Blockcache 里

Filter filter =new QualiFilter(CompareFilter.CompareOp.EQUAL,newBinaryComparator(Bytes.toBytes( “abc”)));//过滤出column为abc的

scan.setFilter(filter);

ResultScannerscanner = table.getScanner(scan);

for(Result result: scanner){

List<Cell> cell = result.listCells();

for(Cell cell:cells){

   String rowkey = newString(CellUnit.cloneRow(cell));

   String family = newString(CellUnit.cloneFamily(cell));

   String column = newString(CellUnit.clonecolumn(cell));

   String value = newString(CellUnit.clonecolumn(cell));

   System.out.println(rowkey+””+family+” ”+column+” ”+value);

 

}

}

scanner.close();

table.close();

conn.close();

 

//FilterList

Configuration conf = HBaseConfiguration.create();

conf.set(“hbase.zookeeper.quorum”,”192.168.1.149”);

conf.set(“hbase.zookeeper.property.clientport”,”2181”);

HConnection conn=HConnectionManager.createConnection(conf);

HTableInterfacetable = conn.getTable(“创建的表名”);

Scan scan = new Scan();

Scan.setCacheBlocks(flase);//产生的数据不会放到Blockcache 里

SingleColumnValueFilterfilter1 = new SingleColumnValueFilter (Bytes.toBytes( “d”),

Bytes.toBytes(“b”),

CompareFilter.CompareOp.EQUAL,

newBinaryPrefixComparator(Bytes.toBytes(“abc”)));//column_family,column,比较符,column_value

filter1.setFilterIfMissing(true);

 

SingleColumnValueFilterfilter2 = new SingleColumnValueFilter (Bytes.toBytes( “d”),

Bytes.toBytes(“c”),

CompareFilter.CompareOp.EQUAL,

newRegexStringComparator(“1|2|3”));//正则:要么为1要么为2要么为3

filter2.setFilterIfMissing(true);

List<Filter>filters = new ArrayList<Filter>();

filters.add(filter1);

filters.add(filter2);

FilterListfilterlist = new FilterList(filters);

scan.setFilter(filterlist);

ResultScannerscanner = table.getScanner(scan);

for(Result result: scanner){

List<Cell> cell = result.listCells();

for(Cell cell:cells){

   String rowkey = newString(CellUnit.cloneRow(cell));

   String family = newString(CellUnit.cloneFamily(cell));

   String column = newString(CellUnit.clonecolumn(cell));

   String value = newString(CellUnit.clonecolumn(cell));

   System.out.println(rowkey+””+family+” ”+column+” ”+value);

 

}

}

scanner.close();

table.close();

conn.close();

 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一年级孩子做题粗心怎么办 一年级的孩子做题粗心怎么办 一年级娃娃做题粗心怎么办 有同学抄作业该怎么办 同学要抄我作业怎么办 同桌抄我作业我该怎么办 孩子想换同桌老师不同意怎么办 尴尬的事情心里难受怎么办 孩子撒谎不完成作业怎么办 孩子撒谎不交作业怎么办 孩子不爱写作业撒谎怎么办 大便又粗又硬怎么办 二年级末考紧张怎么办 孩子粗心做数学题老出错怎么办 生地会考考了140怎么办 数学成绩考砸了怎么办? 考砸了家长发火怎么办 大型考试考砸了怎么办 能力考试考砸了怎么办 考砸了老师批评怎么办 英语抽测考砸了老师怎么办? 辅导班学生考砸了老师怎么办 好学生考砸了怎么办? 孩子学习不认真马虎怎么办 孩子做作业不认真马虎怎么办 一年级小孩学习不认真怎么办 孩子做题审题不认真怎么办 初二孩子不认真审题怎么办 孩子总是粗心抄错题看错题怎么办 孩子总马虎我要怎么办 年检标志跟车牌号不一样怎么办 纹完身后喝酒了怎么办 孩子学习态度不端正怎么办 孩子高考三模考差了家长怎么办 一年级学生学习马虎大意怎么办 一年级下册孩子做作业马虎怎么办 一年级孩子做题马虎怎么办 四年级小孩不喜欢数学该怎么办 工作出了大错误怎么办 遇到不讲道理的人怎么办 四岁宝宝爱生闷气怎么办