在 hbase shell 上执行 ValueFilter 和 Count 值

doing a ValueFilter and Count values on hbase shell

我正在使用 HBase Shell 并且想知道是否可以计算以下扫描命令过滤的值?

scan 'table', { COLUMNS => 'cf:c', FILTER => "ValueFilter( =, 'substring:myvalue' )" }

它应该在 shell 上显示总和。有什么想法吗?

感谢您的帮助。

count 命令不支持过滤器。只有扫描可以。

hbase 中的 AFAIK shell 过滤器 + 计数是不可能的。

您可以对少量行执行以下操作。

对于小数据:

所以我建议你必须用 hbase java 客户端做这样的事情

scan with your value filter here ....

for (Result rs = scanner.next(); rs != null; rs = scanner.next()) {
    count++;
}

对于大数据(为了速度和并行性,我们需要在这里使用 Mapreduce 或其他一些分布式的东西......):

我建议使用 mapreduce 程序来计算行数。 在驱动程序扫描对象中,您需要设置值过滤器,如下例所示。

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class SimpleRowCounter extends Configured implements Tool {

  static class RowCounterMapper extends TableMapper<ImmutableBytesWritable, Result> {
    public static enum Counters { ROWS }

    @Override
    public void map(ImmutableBytesWritable row, Result value, Context context) {
      context.getCounter(Counters.ROWS).increment(1);
    }
  }

  @Override
  public int run(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: SimpleRowCounter <tablename>");
      return -1;
    }
    String tableName = args[0];
    Scan scan = new Scan();

Filter valFilter = new ValueFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("1500"))); scan.setFilter(valFilter );

    Job job = new Job(getConf(), getClass().getSimpleName());
    job.setJarByClass(getClass());
    TableMapReduceUtil.initTableMapperJob(tableName, scan,
        RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, job);
    job.setNumReduceTasks(0);
    job.setOutputFormatClass(NullOutputFormat.class);
    return job.waitForCompletion(true) ? 0 : 1;
  }

  public static void main(String[] args) throws Exception {
    int exitCode = ToolRunner.run(HBaseConfiguration.create(),
        new SimpleRowCounter(), args);
    System.exit(exitCode);
  }
}