hbase本身提供了 聚合方法可以服务端聚合操作

hbase中的CoprocessorProtocol机制.

CoprocessorProtocol的原理比较简单,近似于一个mapreduce框架。由client将scan分解为面向多个region的请求,并行发送请求到多个region,然后client做一个reduce的操作,得到最后的结果。

先看一个例子,使用hbase的AggregationClient可以做到简单的面向单个column的统计。

  1. @Test
  2. public void testAggregationClient() throws Throwable {
  3. LongColumnInterpreter columnInterpreter = new LongColumnInterpreter();
  4. AggregationClient aggregationClient = new AggregationClient(
  5. CommonConfig.getConfiguration());
  6. Scan scan = new Scan();
  7. scan.addColumn(ColumnFamilyName, QName1);
  8. Long max = aggregationClient.max(TableNameBytes, columnInterpreter,
  9. scan);
  10. Assert.assertTrue(max.longValue() == 100);
  11. Long min = aggregationClient.min(TableNameBytes, columnInterpreter,
  12. scan);
  13. Assert.assertTrue(min.longValue() == 20);
  14. Long sum = aggregationClient.sum(TableNameBytes, columnInterpreter,
  15. scan);
  16. Assert.assertTrue(sum.longValue() == 120);
  17. Long count = aggregationClient.rowCount(TableNameBytes,
  18. columnInterpreter, scan);
  19. Assert.assertTrue(count.longValue() == 4);
  20. }

看下hbase的源码。AggregateImplementation

  1. @Override
  2. public <T, S> T getMax(ColumnInterpreter<T, S> ci, Scan scan)
  3. throws IOException {
  4. T temp;
  5. T max = null;
  6. InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment())
  7. .getRegion().getScanner(scan);
  8. List<KeyValue> results = new ArrayList<KeyValue>();
  9. byte[] colFamily = scan.getFamilies()[0];
  10. byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst();
  11. // qualifier can be null.
  12. try {
  13. boolean hasMoreRows = false;
  14. do {
  15. hasMoreRows = scanner.next(results);
  16. for (KeyValue kv : results) {
  17. temp = ci.getValue(colFamily, qualifier, kv);
  18. max = (max == null || (temp != null && ci.compare(temp, max) > 0)) ? temp : max;
  19. }
  20. results.clear();
  21. } while (hasMoreRows);
  22. } finally {
  23. scanner.close();
  24. }
  25. log.info("Maximum from this region is "
  26. + ((RegionCoprocessorEnvironment) getEnvironment()).getRegion()
  27. .getRegionNameAsString() + ": " + max);
  28. return max;
  29. }

这里由于

  1. byte[] colFamily = scan.getFamilies()[0];
  2. 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 聚合操作的更多相关文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (27) ------ 第五章 加载实体和导航属性之关联实体过滤、排序、执行聚合操作

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-9  关联实体过滤和排序 问题 你有一实体的实例,你想加载应用了过滤和排序的相关 ...

  2. MongoDB 聚合操作

    在MongoDB中,有两种方式计算聚合:Pipeline 和 MapReduce.Pipeline查询速度快于MapReduce,但是MapReduce的强大之处在于能够在多台Server上并行执行复 ...

  3. .NET LINQ 聚合操作

    聚合操作      聚合运算从值集合计算单个值. 从一个月的日温度值计算日平均温度就是聚合运算的一个示例. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 ...

  4. HBase Shell操作

    Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统.     Hbase不同于一般的关系型数据库(RDBMS ...

  5. hbase连接操作

    hbase连接操作 package com.test; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...

  6. Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)

    在Linq中有一些这样的操作,根据集合计算某一单一值,比如集合的最大值,最小值,平均值等等.Linq中包含7种操作,这7种操作被称作聚合操作. 1.Count操作,计算序列中元素的个数,或者计算满足一 ...

  7. OpenStack/Gnocchi简介——时间序列数据聚合操作提前计算并存储起来,先算后取的理念

    先看下 http://www.cnblogs.com/bonelee/p/6236962.html 这里对于环形数据库的介绍,便于理解归档这个操作! 转自:http://blog.sina.com.c ...

  8. hbase日常操作及维护

    一,基本命令: 建表:create 'testtable','coulmn1','coulmn2' 也可以建表时加coulmn的属性如:create 'testtable',{NAME => ' ...

  9. JDK1.8聚合操作

    在java8 JDK包含许多聚合操作(如平均值,总和,最小,最大,和计数),返回一个计算流stream的聚合结果.这些聚合操作被称为聚合操作.JDK除返回单个值的聚合操作外,还有很多聚合操作返回一个c ...

随机推荐

  1. js加密数据爬取

    - 中国空气质量在线监测分析平台是一个收录全国各大城市天气数据的网站,包括温度.湿度.PM 2.5.AQI 等数据,链接为:https://www.aqistudy.cn/html/city_deta ...

  2. jquery对于ajax的封装

    第一层封装 $.ajax({ 属性名:值,属性名:值}) /* url: 请求服务器地址 data:请求参数 dataType:服务器返回数据类型 error 请求出错执行的功能 success 请求 ...

  3. C#查找List 某一段数据

    public void SelectData() { List<int> r = new List<int>(); r.Add(); r.Add(); r.Add(); r.A ...

  4. 如何将制定目录加入到PYTHONPATH

    1 问题背景 TensorFlow Object Detection API 是以Slim为基础实现的,需要将Slim的目录加入PYTHONPATH后才能正确运行. 2 机器环境 window10 a ...

  5. wangEditor富文本框——例

    官方文档:http://www.wangeditor.com/ 效果 html <!DOCTYPE html> <html> <head> <meta cha ...

  6. AutoIt自动化编程(3)【转】

    模拟鼠标点击(按钮等)控件 既然是模拟用户操作,自然就包括了模拟鼠标点击在内. 适用命令/函数:Click/MouseClick/ControlClick 其中Click/MouseClick用来模拟 ...

  7. JavaScript设置body高度为浏览器高度的方法

    document.getElementsByTagName('body')[0].style.height = window.innerHeight+'px';

  8. 完全卸载之前8.0的Mysql,安装5.5mysql

    完全卸载: https://blog.csdn.net/sxingming/article/details/52601250 安装mysql5.5: https://blog.csdn.net/fly ...

  9. vue如何关闭sourceMap

    Vue打包后出现一些map文件的解决办法: 问题: 可能很多人在做vue项目打包,打包之后js中,会自动生成一些map文件,那我们怎么把它去掉不要呢? 1,运行  cnpm run build  开始 ...

  10. js实现获取两个日期之间所有日期的方法

    function getDate(datestr){ var temp = datestr.split("-"); var date = new Date(temp[0],temp ...