1.Java代码实现

import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.coprocessor.AggregateImplementation; /**
* <p>
* 协处理器统计HBase表数据量
* </p>
*
*/
public class HBaseRecordsCounter { /**
* HBase API添加协处理器
* */
public static void addCoprocessor(Configuration conf, String tableName) {
try {

byte[] tableNameBytes = Bytes.toBytes(tableName);
  HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
  HTableDescriptor htd = hbaseAdmin.getTableDescriptor(tableNameBytes);
  if (!htd.hasCoprocessor(AggregateImplementation.class.getName())) {
    hbaseAdmin.disableTable(tableNameBytes);
    htd.addCoprocessor(AggregateImplementation.class.getName());
    hbaseAdmin.modifyTable(tableNameBytes, htd);
    hbaseAdmin.enableTable(tableNameBytes);
  }

hbaseAdmin.close();

} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 统计表数量
*
*/
public static void exeCount(Configuration conf, String tableName, String family) { try {
// 使用hbase提供的聚合coprocessor
AggregationClient aggregationClient = new AggregationClient(conf);
Scan scan = new Scan();
// 指定扫描列族,唯一值
scan.addFamily(Bytes.toBytes(family));
long start = System.currentTimeMillis();
long rowCount = aggregationClient.rowCount(TableName.valueOf(tableName), new LongColumnInterpreter(), scan);
System.out
.println("Row count: " + rowCount + "; time cost: " + (System.currentTimeMillis() - start) + "ms");
} catch (Throwable e) {
e.printStackTrace();
}
} public static void main(String[] args) { String tableName = "test";
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "host1,host2,host3");
conf.set("hbase.rootdir", "hdfs://host:8020/hbase");
// 提高RPC通信时长
conf.setLong("hbase.rpc.timeout", 600000);
// 设置Scan缓存
conf.setLong("hbase.client.scanner.caching", 1000);
addCoprocessor(conf, tableName);
exeCount(conf, tableName, "info"); }
}

2. 启用协处理器

启用协处理器方法1.

启动全局aggregation,能过操纵所有的表上的数据。通过修改hbase-site.xml这个文件来实现,只需要添加如下代码:

<property>
<name>hbase.coprocessor.user.region.classes</name>
<value>org.apache.hadoop.hbase.coprocessor.AggregateImplementation</value>
</property>

启用协处理器方法2.

hbase shell添加coprocessor:

disable 'member'
alter 'member',METHOD => 'table_att','coprocessor' => 'hdfs://master24:9000/user/hadoop/jars/test.jar|mycoprocessor.SampleCoprocessor|1001|'
enable 'member'

hbase shell 删除coprocessor:

disable 'member'
alter 'member',METHOD => 'table_att_unset',NAME =>'coprocessor$1'
enable 'member'

HBase协处理器统计表数据量的更多相关文章

  1. 统计Azure存储的HBase各表数据量

    场景:HBase存储在Azure上,现在通过访问Azure Storage的接口,获取HBase中各个表的数据量. 注意: 1.Azure存储,默认的副本数为2,即共存3份,但只收1份的费用,取到的s ...

  2. hbase数据加盐(Salting)存储与协处理器查询数据的方法

    转自: https://blog.csdn.net/finad01/article/details/45952781 ----------------------------------------- ...

  3. HBase协处理器同步二级索引到Solr

    一. 背景二. 什么是HBase的协处理器三. HBase协处理器同步数据到Solr四. 添加协处理器五. 测试六. 协处理器动态加载 一. 背景 在实际生产中,HBase往往不能满足多维度分析,我们 ...

  4. 大数据量场景下storm自定义分组与Hbase预分区完美结合大幅度节省内存空间

    前言:在系统中向hbase中插入数据时,常常通过设置region的预分区来防止大数据量插入的热点问题,提高数据插入的效率,同时可以减少当数据猛增时由于Region split带来的资源消耗.大量的预分 ...

  5. pinpoint 单机HBASE数据量过大问题解决

    Pinpoint接入业务监控后数据量大涨,平均每周Hbase数据增量35G左右,数据量太大,需要对数据进行定期清理,否则监控可用性降低. 操作步骤 查找出数据大的hbase表 [root@iZ28ov ...

  6. 大数据开发--Hbase协处理器案例

    大数据开发--Hbase协处理器案例 1. 需求描述 在社交网站,社交APP上会存储有大量的用户数据以及用户之间的关系数据,比如A用户的好友列表会展示出他所有的好友,现有一张Hbase表,存储就是当前 ...

  7. [How to] 使用HBase协处理器---Endpoint客户端代码的实现

    1.简介 不同于Observer协处理器,EndPoint由于需要同region进行rpc服务的通信,以及客户端出数据的归并,需要自行实现客户端代码. 基于[How to] 使用HBase协处理器-- ...

  8. [How to] 使用HBase协处理器---Endpoint服务端的实现

    1.简介 前篇文章[How to] 使用HBase协处理器---基本概念和regionObserver的简单实现中提到了两种不同的协处理器,并且实现了regionObserver. 本文将介绍如何使用 ...

  9. HBase 协处理器编程详解第一部分:Server 端代码编写

    Hbase 协处理器 Coprocessor 简介 HBase 是一款基于 Hadoop 的 key-value 数据库,它提供了对 HDFS 上数据的高效随机读写服务,完美地填补了 Hadoop M ...

随机推荐

  1. 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System

    题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...

  2. POJ3321 Apple Tree(DFS序)

    题目,是对一颗树,单点修改.子树查询.典型的dfs序入门题. DFS序可以将一颗树与子树们表示为一个连续的区间,然后用线段树来维护:感觉算是树链剖分的一种吧,和轻重链剖分不同的是这是对子树进行剖分的. ...

  3. Eddy's problem partI

    Eddy's mistakes[HDU1161] Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. BZOJ3630 : [JLOI2014]镜面通道

    从左边不能到达右边当且仅当存在一条与上下底边相连的分割线将它们分开 设下底边为S,上底边为T,每个元件作为点,有公共部分的两个点互相连边 最后拆点求最小割 #include<cstdio> ...

  5. 【BZOJ】1076: [SCOI2008]奖励关(状压dp+数学期望)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1076 有时候人蠢还真是蠢.一开始我看不懂期望啊..白书上其实讲得很详细的,什么全概率,全期望(这个压 ...

  6. 《菊与刀》--[美]鲁思·本尼迪克特(Ruth Benedict)

    <菊与刀>这本书实在是好看. 下面是一些书摘: * 由在美国曾经全力以赴与之战斗的敌人中,日本人的脾气是最琢磨不透的. * “菊”本是日本皇家家微,“刀”是武家文化的象征. * 日本人的格 ...

  7. Phaser提供了Button对象简单的实现一个按钮

    Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...

  8. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  9. [APAC]自动发送邮件

    $lastMon = ((get-date).AddDays(-7) -split(" "))[0] $lastFri = ((get-date).AddDays(-3) -spl ...

  10. Office and Windows KMS使用帮助

    Office KMS Key: http://technet.microsoft.com/zh-cn/library/dn385360.aspx                         htt ...