我试图获得一些限定符的平均值,该值在HBase中被写为具有特定前缀的键行的BigDecimal类型。
@Test(timeout = 300000)
public void testAvgWithFilter() throws Throwable {
AggregationClient aClient = new AggregationClient(conf);
Scan scan = new Scan();
scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
scan.setFilter(f);
final ColumnInterpreter<Double, Double, EmptyMsg, DoubleMsg, DoubleMsg> ci = new DoubleColumnInterpreter();
Double avg = null;
avg = aClient.avg(TEST_TABLE, ci, scan);
assertEquals(Double.NaN, avg, 0);
}
我在共享的代码中使用了片段 https://www.programcreek.com/java-api-examples/class=org.apache.hadoop.hbase.client.coprocessor.AggregationClient&method=avg。 在我的情况下,我使用SingleColumnValueFilter而不是PrefixFilter,当我初始化avg方法时,过滤器不起作用。 如果使用也支持的rowCount方法,则考虑SingleColumnValueFilter并且整个查询工作正常。
这是我的片段:
AggregationClient aggClient = new AggregationClient(conf);
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableName)));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes(columnFamily));
scan.setRowPrefixFilter(Bytes.toBytes(rowKeyPrefix));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
Bytes.toBytes(columnFamily),
Bytes.toBytes("columName"),
CompareOperator.EQUAL,
new BinaryComparator(Bytes.toBytes(searchValue1))
);
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
Bytes.toBytes(columnFamily),
Bytes.toBytes("columnName"),
CompareOperator.EQUAL,
new BinaryComparator(Bytes.toBytes(searchValue2))
);
SingleColumnValueFilter[] filters = {
filter1
filter2
};
for (Filter f : filters) {
filterList.addFilter(f);
}
scan.setFilter(filterList);
scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("sog"));
final ColumnInterpreter<BigDecimal,
BigDecimal,
HBaseProtos.EmptyMsg,
HBaseProtos.BigDecimalMsg,
HBaseProtos.BigDecimalMsg> columnInterpreter =
new BigDecimalColumnInterpreter();
Double avgx = null;
avgx = aggClient.avg(table, ci, scan);
为什么过滤器不起作用? 我的HBase版本是2.0.0, Hadoop 2.7.4