hbase 聚合操作
hbase本身提供了 聚合方法可以服务端聚合操作
hbase中的CoprocessorProtocol机制.
CoprocessorProtocol的原理比较简单,近似于一个mapreduce框架。由client将scan分解为面向多个region的请求,并行发送请求到多个region,然后client做一个reduce的操作,得到最后的结果。
先看一个例子,使用hbase的AggregationClient可以做到简单的面向单个column的统计。
- @Test
- public void testAggregationClient() throws Throwable {
- LongColumnInterpreter columnInterpreter = new LongColumnInterpreter();
- AggregationClient aggregationClient = new AggregationClient(
- CommonConfig.getConfiguration());
- Scan scan = new Scan();
- scan.addColumn(ColumnFamilyName, QName1);
- Long max = aggregationClient.max(TableNameBytes, columnInterpreter,
- scan);
- Assert.assertTrue(max.longValue() == 100);
- Long min = aggregationClient.min(TableNameBytes, columnInterpreter,
- scan);
- Assert.assertTrue(min.longValue() == 20);
- Long sum = aggregationClient.sum(TableNameBytes, columnInterpreter,
- scan);
- Assert.assertTrue(sum.longValue() == 120);
- Long count = aggregationClient.rowCount(TableNameBytes,
- columnInterpreter, scan);
- Assert.assertTrue(count.longValue() == 4);
- }
看下hbase的源码。AggregateImplementation
- @Override
- public <T, S> T getMax(ColumnInterpreter<T, S> ci, Scan scan)
- throws IOException {
- T temp;
- T max = null;
- InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment())
- .getRegion().getScanner(scan);
- List<KeyValue> results = new ArrayList<KeyValue>();
- byte[] colFamily = scan.getFamilies()[0];
- byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst();
- // qualifier can be null.
- try {
- boolean hasMoreRows = false;
- do {
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- temp = ci.getValue(colFamily, qualifier, kv);
- max = (max == null || (temp != null && ci.compare(temp, max) > 0)) ? temp : max;
- }
- results.clear();
- } while (hasMoreRows);
- } finally {
- scanner.close();
- }
- log.info("Maximum from this region is "
- + ((RegionCoprocessorEnvironment) getEnvironment()).getRegion()
- .getRegionNameAsString() + ": " + max);
- return max;
- }
这里由于
- byte[] colFamily = scan.getFamilies()[0];
- byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst();
所以,hbase自带的Aggregate函数,只能面向单列进行统计。
当我们想对多列进行Aggregate,并同时进行countRow时,有以下选择。
1 scan出所有的row,程序自己进行Aggregate和count。
2 使用AggregationClient,调用多次,得到所有的结果。由于多次调用,有一致性问题。
3 自己扩展CoprocessorProtocol。
这个是github的hbase集成插件
这个功能集成到simplehbase里面了。
https://github.com/zhang-xzhi/simplehbase
hbase 聚合操作的更多相关文章
- 《Entity Framework 6 Recipes》中文翻译系列 (27) ------ 第五章 加载实体和导航属性之关联实体过滤、排序、执行聚合操作
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-9 关联实体过滤和排序 问题 你有一实体的实例,你想加载应用了过滤和排序的相关 ...
- MongoDB 聚合操作
在MongoDB中,有两种方式计算聚合:Pipeline 和 MapReduce.Pipeline查询速度快于MapReduce,但是MapReduce的强大之处在于能够在多台Server上并行执行复 ...
- .NET LINQ 聚合操作
聚合操作 聚合运算从值集合计算单个值. 从一个月的日温度值计算日平均温度就是聚合运算的一个示例. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 ...
- HBase Shell操作
Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统. Hbase不同于一般的关系型数据库(RDBMS ...
- hbase连接操作
hbase连接操作 package com.test; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...
- Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)
在Linq中有一些这样的操作,根据集合计算某一单一值,比如集合的最大值,最小值,平均值等等.Linq中包含7种操作,这7种操作被称作聚合操作. 1.Count操作,计算序列中元素的个数,或者计算满足一 ...
- OpenStack/Gnocchi简介——时间序列数据聚合操作提前计算并存储起来,先算后取的理念
先看下 http://www.cnblogs.com/bonelee/p/6236962.html 这里对于环形数据库的介绍,便于理解归档这个操作! 转自:http://blog.sina.com.c ...
- hbase日常操作及维护
一,基本命令: 建表:create 'testtable','coulmn1','coulmn2' 也可以建表时加coulmn的属性如:create 'testtable',{NAME => ' ...
- JDK1.8聚合操作
在java8 JDK包含许多聚合操作(如平均值,总和,最小,最大,和计数),返回一个计算流stream的聚合结果.这些聚合操作被称为聚合操作.JDK除返回单个值的聚合操作外,还有很多聚合操作返回一个c ...
随机推荐
- js加密数据爬取
- 中国空气质量在线监测分析平台是一个收录全国各大城市天气数据的网站,包括温度.湿度.PM 2.5.AQI 等数据,链接为:https://www.aqistudy.cn/html/city_deta ...
- jquery对于ajax的封装
第一层封装 $.ajax({ 属性名:值,属性名:值}) /* url: 请求服务器地址 data:请求参数 dataType:服务器返回数据类型 error 请求出错执行的功能 success 请求 ...
- C#查找List 某一段数据
public void SelectData() { List<int> r = new List<int>(); r.Add(); r.Add(); r.Add(); r.A ...
- 如何将制定目录加入到PYTHONPATH
1 问题背景 TensorFlow Object Detection API 是以Slim为基础实现的,需要将Slim的目录加入PYTHONPATH后才能正确运行. 2 机器环境 window10 a ...
- wangEditor富文本框——例
官方文档:http://www.wangeditor.com/ 效果 html <!DOCTYPE html> <html> <head> <meta cha ...
- AutoIt自动化编程(3)【转】
模拟鼠标点击(按钮等)控件 既然是模拟用户操作,自然就包括了模拟鼠标点击在内. 适用命令/函数:Click/MouseClick/ControlClick 其中Click/MouseClick用来模拟 ...
- JavaScript设置body高度为浏览器高度的方法
document.getElementsByTagName('body')[0].style.height = window.innerHeight+'px';
- 完全卸载之前8.0的Mysql,安装5.5mysql
完全卸载: https://blog.csdn.net/sxingming/article/details/52601250 安装mysql5.5: https://blog.csdn.net/fly ...
- vue如何关闭sourceMap
Vue打包后出现一些map文件的解决办法: 问题: 可能很多人在做vue项目打包,打包之后js中,会自动生成一些map文件,那我们怎么把它去掉不要呢? 1,运行 cnpm run build 开始 ...
- js实现获取两个日期之间所有日期的方法
function getDate(datestr){ var temp = datestr.split("-"); var date = new Date(temp[0],temp ...