简体   繁体   English

HBase聚合

[英]HBase Aggregation

I'm having some trouble doing aggregation on a particular column in HBase. 我在对HBase中的特定列进行聚合时遇到一些麻烦。

This is the snippet of code I tried: 这是我尝试的代码片段:

 Configuration config = HBaseConfiguration.create();
 AggregationClient aggregationClient = new AggregationClient(config);

 Scan scan = new Scan();
 scan.addColumn(Bytes.toBytes("drs"), Bytes.toBytes("count"));

 ColumnInterpreter<Long, Long> ci = new LongColumnInterpreter();

 Long sum = aggregationClient.sum(Bytes.toBytes("DEMO_CALCULATIONS"), ci , scan);
 System.out.println(sum);

sum returns a value of null. sum返回值为null。 The aggregationClient API works fine if I do a rowcount. 如果执行行计数,aggregationClient API可以正常工作。

I was trying to follow the directions in http://michaelmorello.blogspot.in/2012/01/row-count-hbase-aggregation-example.html 我试图按照http://michaelmorello.blogspot.in/2012/01/row-count-hbase-aggregation-example.html中的说明进行操作

Could there be a problem with me using a LongColumnInterpreter when the 'count' field was an int? 当'count'字段为int时,使用LongColumnInterpreter对我有问题吗? What am I missing in here? 我在这里想念什么?

You can only use long(8bytes) to do sum with default setting. 您只能使用long(8bytes)来进行默认设置的总和。

Cause in the code of AggregateImplementation's getSum method, it handle all the returned KeyValue as long. 由于在AggregateImplementation的getSum方法的代码中,它会尽可能长时间地处理所有返回的KeyValue。

List<KeyValue> results = new ArrayList<KeyValue>();
try {
  boolean hasMoreRows = false;
  do {
    hasMoreRows = scanner.next(results);
    for (KeyValue kv : results) {
      temp = ci.getValue(colFamily, qualifier, kv);
      if (temp != null)
        sumVal = ci.add(sumVal, ci.castToReturnType(temp));
    }
    results.clear();
  } while (hasMoreRows);
} finally {
  scanner.close();
}

and in LongColumnInterpreter 和在LongColumnInterpreter中

public Long getValue(byte[] colFamily, byte[] colQualifier, KeyValue kv)
  throws IOException {
if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG)
  return null;
return Bytes.toLong(kv.getBuffer(), kv.getValueOffset());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM