hbase协处理器coprocessor 统计行数量 案例

其实可以通过 hbase shell 在配置文件里面增加  coprocessor 的选项,但是我个人觉得不太方便

代码方面可以对 单独的表进行控制,觉得更为实用和方便一些, 下面贴上了本人的代码,增加了是否注册协处理器的判断,以前的代码 如果查询两次,会因为注册了协处理器导致再次注册报错,而且表会进入 disable状态, 现在的代码不会出现问题,算是小小的改进,

package com.ruif.hbase.service;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import com.opensymphony.module.sitemesh.filter.PageFilter;
import com.ruif.utils.TimeUtil;

public class HbaseTest2 {

	public static void main(String[] args) throws Throwable {

		Configuration conf = HBaseConfiguration.create();
		System.setProperty("hadoop.home.dir", "E:/hadoop");

		myHbaseConf(conf);

		/**
		 * 通过协处理器获得行数,不修改 hbase.site
		 */
		rowCount2("a02", conf);

	}

	private static void rowCount2(String tableName, Configuration conf) throws IllegalArgumentException, Throwable {
		String coprocessorClassName = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";
		HBaseAdmin admin = new HBaseAdmin(conf);
		HTableDescriptor htd = admin.getTableDescriptor(Bytes.toBytes(tableName));
		boolean flag = htd.hasCoprocessor(coprocessorClassName);// 有就是true 没有就是 false
		if (!flag) {
			admin.disableTable(tableName);
			htd.addCoprocessor(coprocessorClassName);
			admin.modifyTable(Bytes.toBytes(tableName), htd);
			admin.enableTable(tableName);
		}

		AggregationClient ac = new AggregationClient(conf);
		Scan scan = new Scan();
		scan.addFamily(Bytes.toBytes("info"));
		long rowCount = 0;
		rowCount = ac.rowCount(TableName.valueOf(Bytes.toBytes(tableName)), new LongColumnInterpreter(), scan);
		System.out.println(rowCount);
	}

	private static void myHbaseConf(Configuration conf) {
		conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
	}

}


有协处理器和没有的表描述是有差别的

你可能感兴趣的:(hbase)