运用hbase好长时间了,今天利用闲暇时间把Hbase的各种查询总结下,以后有时间把协处理器和自定义File总结下。

查询条件分为:

  1、统计表数据

  2,hbase 简单分页

  3,like 查询

  4 ,  AND 查询

  5 ,  OR 查询

  6  ,rowkey 的  in 查询

  7 , 正则查询

上代码先。

package com.query;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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.client.Table;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger; import basecommon.PropertyUtil; public class HbaseConnectionUtils {
private static Logger log = Logger.getLogger(HbaseConnectionUtils.class);
private static HbaseConnectionUtils instance = null;
public static Configuration config = null;
private static Connection connection = null; private HbaseConnectionUtils() { } /**
* @author c_lishaoying 983068303@qq.com
*
* 加载集群配置
*/
static {
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum",
PropertyUtil.get("zookeeper.quorum"));
config.set("hbase.zookeeper.property.clientPort",
PropertyUtil.get("zookeeper.property.clientPort"));
config.setLong("hbase.client.scanner.timeout.period", Integer.MAX_VALUE);
try {
connection = ConnectionFactory.createConnection(config);
} catch (Exception e) {
log.debug("connection构建失败");
}
} public static HbaseConnectionUtils getInstance() {
if (instance == null) {
// 给类加锁 防止线程并发
synchronized (HbaseConnectionUtils.class) {
if (instance == null) {
instance = new HbaseConnectionUtils();
}
}
}
return instance;
} public Configuration getConfiguration() {
return config;
} public Connection getConnection() {
return connection;
} /**
* @author c_lishaoying 983068303@qq.com 获取表连接
*
*/
public static Table getTable(String tableName) {
try {
return connection.getTable(TableName.valueOf(tableName));
} catch (IOException e) {
e.printStackTrace();
}
return null;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 协处理器查询总数据
*
*/
public int getRecordCount(String tableName) {
AggregationClient aggregationClient = new AggregationClient(config);
Scan scan = new Scan();
try {
Long rowCount = aggregationClient.rowCount(
TableName.valueOf(tableName), new LongColumnInterpreter(),
scan);
aggregationClient.close();
return rowCount.intValue();
} catch (Throwable e) {
e.printStackTrace();
}
return 0;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 协处理器查询总数据 添加查询条件的Scan统计总数
*/
public static int getTotalRecord(Table keyIndexTable, Configuration config,
final Scan scan) {
int count = 0;
AggregationClient aggregationClient = new AggregationClient(config);
try {
Long rowCount = aggregationClient.rowCount(keyIndexTable,
new LongColumnInterpreter(), scan);
aggregationClient.close();
count = rowCount.intValue();
} catch (Throwable e) {
e.printStackTrace();
}
return count;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 协处理器查询总数据 添加查询条件的Scan统计总数 queryString数组长度为3
* queryString[0] 列族 queryString[1]字段 queryString[2] 字段值
*/
public static int queryByCloumn(String tablename, String[] queryString) { int count = 0;
if (tablename == null) {
log.info("配置的表名称为空!");
} else {
log.info("表名称为:" + tablename);
Table queryTablename = getTable(tablename);
Scan scan = new Scan();
scan = getScan(queryString);
count = getTotalRecord(queryTablename, config, scan);
} return count;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 非协处理器查询总数据 添加查询条件的Scan统计总数 queryString数组长度为3
* queryString[0] 列族 queryString[1]字段 queryString[2] 字段值
*/
public static int scan(String tablename, String[] condition) {
Table queryTablename = getTable(tablename);
String[] s = condition;
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(s[0]), Bytes.toBytes(s[1]));
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
Bytes.toBytes(s[0]), Bytes.toBytes(s[1]), CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes(s[2])));
scan.setFilter(singleColumnValueFilter);
int i = 0;
try {
ResultScanner rs = queryTablename.getScanner(scan);
/*
* for(Iterator it = rs.iterator(); it.hasNext();){ it.next(); }
*/
for (Result r : rs) {
i++;
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 查询字段相等的值 相当于where city = ‘上海’ queryString数组长度为3
* queryString[0] 列族 queryString[1]字段 queryString[2] 字段值
*/
public static Scan getScan(String[] condition) {
Scan scan = new Scan();
if (condition == null || condition.length != 3) {
return new Scan();
}
FilterList filterList = new FilterList();
String[] s = condition;
BinaryComparator comp = new BinaryComparator(Bytes.toBytes(s[2]));
filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(s[0]),
Bytes.toBytes(s[1]), CompareOp.EQUAL, Bytes.toBytes(s[2])));
scan.setFilter(filterList);
return scan;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 查询字段相等的值 应用正则表达式 相当于where city like ‘%上海%’
* queryString数组长度为3 queryString[0] 列族 queryString[1]字段
* queryString[2] 字段值
*/
public static Scan regexscan(String tablename, String[] condition) {
Table queryTablename = getTable(tablename);
String[] s = condition;
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(s[0]), Bytes.toBytes(s[1]));
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
Bytes.toBytes(s[0]), Bytes.toBytes(s[1]), CompareOp.EQUAL,
new RegexStringComparator(".*" + s[2] + ".*"));
scan.setFilter(singleColumnValueFilter);
return scan;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 查询该列的值
*/
public static Scan valuescan(String tablename, String[] condition) {
Table queryTablename = getTable(tablename);
String[] s = condition;
Scan scan = new Scan(); Filter filter1 = new ValueFilter(CompareOp.EQUAL,
new SubstringComparator(s[2]));
scan.setFilter(filter1);
return scan;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 根据rowkey 查询 相当于 where id in ()
* queryString数组都为rowkey
*/
public static Scan rowscan(String tablename, String[] condition) {
Scan scan = new Scan();
FilterList filterList = new FilterList(Operator.MUST_PASS_ONE);
for (String s : condition) {
Filter filter = new RowFilter(CompareOp.EQUAL,
new SubstringComparator(s));
filterList.addFilter(filter);
}
scan.setFilter(filterList);
return scan;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 查询该列的值 相当于where city = ‘上海’ AND name =‘酒店’
* queryString数组长度为3 queryString[0] 列族 queryString[1]字段
* queryString[2] 字段值
*/ public static Scan listAndColumnscan(String tablename,
List<String[]> condition) {
Scan scan = new Scan();
List<Filter> filters = new ArrayList<Filter>(); for (String[] s : condition) {
filters.add(new SingleColumnValueFilter(Bytes.toBytes(s[0]), // 列族
Bytes.toBytes(s[1]), // 列名
CompareOp.EQUAL, Bytes.toBytes(s[2]))); // 值
}
FilterList filterList = new FilterList(Operator.MUST_PASS_ALL, filters);
scan.setFilter(filterList);
return scan;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 查询该列的值 相当于where city = ‘上海’ OR name =‘酒店’
* queryString数组长度为3 queryString[0] 列族 queryString[1]字段
* queryString[2] 字段值
*/
public static Scan listOrColumnscan(String tablename,
List<String[]> condition) {
Scan scan = new Scan();
ArrayList<Filter> listForFilters = new ArrayList<Filter>();
Filter filter = null;
for (String[] s : condition) {
filter = new SingleColumnValueFilter(Bytes.toBytes(s[0]), // 列族
Bytes.toBytes(s[1]), // 列名
CompareOp.EQUAL, Bytes.toBytes(s[2]));
listForFilters.add(filter);
}
// 通过将operator参数设置为Operator.MUST_PASS_ONE,达到list中各filter为"或"的关系
// 默认operator参数的值为Operator.MUST_PASS_ALL,即list中各filter为"并"的关系
Filter filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE,
listForFilters);
scan.setFilter(filterList);// 多条件过滤
return scan;
} /**
* @author c_lishaoying
* @email 983068303@qq.com 通过多条件联合查询和限制返回页数 相当于mysql 中的limit 0,1000
*/
public Scan queryByFilter(String tablename, List<String[]> arr,
String starString, String stopString) throws IOException {
FilterList filterList = new FilterList();
Scan scan = new Scan();
for (String[] s : arr) {
SubstringComparator comp = new SubstringComparator(s[2]);
filterList
.addFilter(new SingleColumnValueFilter(Bytes.toBytes(s[0]),
Bytes.toBytes(s[1]), CompareOp.EQUAL, comp));
}
PageFilter pageFilter = new PageFilter(1000);
filterList.addFilter(pageFilter);
scan.setFilter(filterList);
scan.setStartRow(Bytes.toBytes(starString));
scan.setStartRow(Bytes.toBytes(stopString));
return scan;
} }

Hbase各种查询总结的更多相关文章

  1. hbase分页查询

    为了广大技术爱好者学习netty,在这里帮新浪微博@nettying宣传下他出版的新书 <netty权威指南>@nettying兄在华为NIO实践多年,这本书是他的技术和经验的一个结晶.N ...

  2. hadoop之根据Rowkey从HBase中查询数据

    1.Hbase 根据rowkey 查询 conf的配置信息如下: conf = new Configuration(); conf.set("hbase.zookeeper.quorum&q ...

  3. hbase的查询scan功能注意点(setStartRow, setStopRow)

    来自http://hi.baidu.com/7636553/blog/item/982beb17713bc004972b43ee.html hbase的scan查询功能注意项: Scan scan = ...

  4. 根据Rowkey从HBase中查询数据

    /** * @Title: queryData * @Description: 从HBase查询出数据 * @author xxxx * @param tableName * 表名 * @param ...

  5. 基于Solr的HBase实时查询方案

    实时查询方案 HBase+Solr+HBase-Indexer 1.HBase提供海量数据存储 2.solr提供索引构建与查询 3.HBase indexer提供自己主动化索引构建(从HBase到So ...

  6. 「从零单排HBase 04」HBase高性能查询揭秘

    先给结论吧:HBase利用compaction机制,通过大量的读延迟毛刺和一定的写阻塞,来换取整体上的读取延迟的平稳. 1.为什么要compaction 在上一篇 HBase读写 中我们提到了,HBa ...

  7. 吴超老师课程--HBASE的查询手机项目

    查询1.按RowKey查询2.按手机号码查询3.按手机号码的区域查询 //查询手机13450456688的所有上网记录 public static void scan(String tableName ...

  8. 云HBase发布全文索引服务,轻松应对复杂查询

    云HBase发布了“全文索引服务”功能,自2019年01月25日后创建的云HBase实例,可以在控制台免费开启此“全文索引服务”功能.使用此功能可以让用户在HBase之上构建功能更丰富的搜索业务,不再 ...

  9. HBase根据Rowkey批量查询数据JAVA API(一次查多条,返回多个记录)

    最近在生产中遇到了一个需求,前台给我多个rowkey的List,要在hbase中查询多个记录(返回给前台list).在网上也查了很多,不过自己都不太满意,filter的功能有可能查询结果不是准确值,而 ...

随机推荐

  1. Codeforces570C 【简单标记】

    题意: 给定一个长为n的字符串(包含小写字母和'.'),有m次操作 每次操作可以修改字符,并询问修改后有多少对相邻的'.' 思路: 标记. #include<bits/stdc++.h> ...

  2. poj3694(lca + tarjan求桥模板)

    题目链接: http://poj.org/problem?id=3694 题意: 给出一个 n 个节点 m 条边的图, 然后有 q 组形如 x, y 的询问, 在前面的基础上连接边 x, y, 输出当 ...

  3. Java实现发送邮箱验证码/注册验证链接

    本文以163邮箱为例 1.准备(邮箱账号,邮箱必须设置POP3/SMTP/IMAP,设置步骤如下:) >>步骤:1 开启授权码服务 >>步骤:2 手机获取验证码 >> ...

  4. 笔记-迎难而上之Java基础进阶1

    Collection集合 数组的长度是固定的,集合的长度是可变的 数组中存储的是同一类型的元素,可以存储基本数据类型值.集合存储的都是对象.而且对象的类型可以不一致. 集合框架 import java ...

  5. 最长双回文串(模板+dp)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ , f ...

  6. 再谈布局,栅栏式自适应布局的学习和实现(calc自适应布局)

    布局真的很重要.一个不好的布局后期会有很多很多的bug,就像是建房子的地基一样. 首先,再一次地圣杯布局的学习,来源于该教程: http://www.jianshu.com/p/f9bcddb0e8b ...

  7. PDO中构建事务处理的应用程序

    <meta http-equiv="Content-Type" content="text/html";charse="utf-8" ...

  8. BestCoder Round #86 1001

    链接http://acm.hdu.edu.cn/showproblem.php?pid=5804 题意:给你一些商店和他的商品价格,然后给你一个记账本,问你记大了就是1,否则是0 解法:模拟,注意测试 ...

  9. UVALive - 6442

    题目链接:https://vjudge.net/contest/241341#problem/I 题目大意:输入t,t组样例,输入n,m,有n个圆槽,m个硬币,接下来m行代表每个硬币所在的位子,要求你 ...

  10. SQL Server 脚本跟踪

    1.查询 DataBasesID select db_id('regdatas') 2.获取进程ID 3.过滤定位