hbase分页和按条件筛选

关于hbse我想大家都比较常用,这个博客只是当做一个备份。

调用接口:int count = utils.HBaseScan("PUBLIC_SENTIMENT_INFO", "taxesInfo", today);

hbase帮助类:

package com.boc.sql.utils;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.KeyValue;
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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;


/**
* hbase操作类
*
* @author 作者:刘杰
*
* @version 创建时间:2016年11月23日 下午2:04:21
*/


public class HBaseUtil {


private static Configuration conf = null;
static {
conf = HTableUtil.getConf();
}
private HTable table = null;


/**
* 插入一行数据到HBase,返回true表示插入成功,返回false表示插入失败
*
* @param tableName
* @param rowkey
* @param columnFamily
* @param column
* @param value
* @return
*/


@SuppressWarnings({ "deprecation", "resource" })
public static boolean insertOneRow(String tableName, String rowkey, String columnFamily, String column,
String value) {


try {
HTable table = new HTable(conf, tableName);
List<Put> lput = new ArrayList<Put>();
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
lput.add(put);
table.put(lput);// 放入表
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}


}


/**
* 对HBase表的批量插入
*
* @param tableName
* 表名
*/
@SuppressWarnings("deprecation")
public void batchSave(String tableName, List<Put> listPut) throws IOException {
HTable table = null;
table = new HTable(conf, tableName);
table.setAutoFlush(false);
table.put(listPut);
table.isAutoFlush();
table.close();
}


/**
* 查询全部
*
* @param hTable
* @param startRow
* @param endRow
* @throws IOException
*/
public static void scanRecord(String tableName, String family, List<String> list) throws IOException {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
if (null != list && list.size() > 0) {
for (String string : list) {
scan.addColumn(family.getBytes(), string.getBytes());
}
}
// scan.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("name"));
ResultScanner scanner = hTable.getScanner(scan);
int i = 0;
for (Result result : scanner) {
i += 1;
System.out.println("rowKey:" + new String(result.getRow()));
for (Cell cell : result.rawCells()) {
System.out.println("列族:" + new String(CellUtil.cloneFamily(cell)) + " 列:"
+ new String(CellUtil.cloneQualifier(cell)) + " 值:" + new String(CellUtil.cloneValue(cell)));
}
}
System.out.println(i);
}


/**
* 扫描HBase表 当sartRow和endRow为空的时候,全表扫描
*
* @param tableName
* 表名
* @param startRow
* 开始rowkey
* @param endRow
* 结束rowkey
* @return
* @throws IOException
*/


@SuppressWarnings("deprecation")
public int HBaseScan(String tableName, String family, String today) {
if (!family.equals("noticeInfo")) {
return 0;
}
ResultScanner scanner = null;
// this.table = new HTable(conf, tableName);
Scan scan = new Scan();
HTable hTable = null;
try {
hTable = new HTable(conf, tableName);
} catch (IOException e1) {
e1.printStackTrace();
}
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
FamilyFilter filter = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(family)));


BinaryPrefixComparator comp = new BinaryPrefixComparator(Bytes.toBytes(today));
SingleColumnValueFilter s_filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes("get_time"),
CompareOp.EQUAL, comp);
list.addFilter(filter);
list.addFilter(s_filter);
int num = 0;
scan.setFilter(list);
try {
scanner = hTable.getScanner(scan);
} catch (IOException e) {
System.out.println("hbase获得结果集出错,直接返回");
e.printStackTrace();
return 0;
}
for (Result r : scanner) {
int hive = 0;
for (KeyValue keyValue : r.raw()) {
if (new String(keyValue.getQualifier()).equals("get_time")) {
hive = 1;
System.out.println(new String(keyValue.getQualifier()) + " ## " + new String(keyValue.getValue()));
}
}
if (hive == 1) {
System.out.println("行:" + num);
num++;
}
}
try {
hTable.close();
} catch (IOException e) {
e.printStackTrace();
}
return num;
}


public static void main(String[] args) {


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值