使用RowFilter无法正常查询HBase表

时间:2012-06-10 20:07:09

标签: java hbase

我有一个HBase表(来自java),我想通过键列表查询表。我做了以下,但它不起作用。

mFilterFeatureIt = mFeatureSet.iterator();
FilterList filterList=new FilterList(FilterList.Operator.MUST_PASS_ONE);

while (mFilterFeatureIt.hasNext()) {
    long myfeatureId = mFilterFeatureIt.next();
System.out.println("FeatureId:"+myfeatureId+" , ");
RowFilter filter = new RowFilter(CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(myfeatureId)) );
filterList.addFilter(filter);
}

outputMap  = HbaseUtils.getHbaseData("mytable", filterList);
System.out.println("Size of outputMap map:"+ outputMap.szie());

public static Map<String, Map<String, String>> getHbaseData(String table, FilterList filter) {

    Map<String, Map<String, String>> data = new HashMap<String, Map<String, String>>();
HTable htable = null;
try {
    htable = new HTable(HTableConfiguration.getHTableConfiguration(),table);
    Scan scan = new Scan();
    scan.setFilter(filter);

    ResultScanner resultScanner = htable.getScanner(scan);
    Iterator<Result> results = resultScanner.iterator();

    while (results.hasNext()) {

    Result result = results.next();
    String rowId = Bytes.toString(result.getRow());
    List<KeyValue> columns = result.list();
    if (null != columns) {

    HashMap<String, String> colData = new HashMap<String, String>();
    for (KeyValue column : columns) {
        colData.put(Bytes.toString(column.getFamily()) + ":"+ Bytes.toString(column.getQualifier()),Bytes.toString(column.getValue()));
    }
                data.put(rowId, colData);
    }

    }

    } catch (IOException e) {
    e.printStackTrace();
    } finally {
        if (htable != null)
    try {
    htable.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return data;
}

FeatureId:80515900, FeatureId:80515901, FeatureId:80515902,

outputMap map的大小:0

我看到功能ID的值是我想要的,但即使密钥存在于hbase表中,我总是得到上面的输出。谁能告诉我我做错了什么?

编辑: 我在上面发布了我的hbase util方法的代码,以便你可以指出我那里的任何错误。

我正在尝试做一个等同于select * FROM mytable where featureId in (80515900, 80515901, 80515902)的SQL。我想在HBase中实现相同的想法就是为每个featureId创建一个带有一个过滤器的过滤器列表。这是对的吗?

这是我表格的内容

scan 'mytable', {COLUMNS => ['sample:tag_count'] }

80515900                                            column=sample:tag_count, timestamp=1339304052748, value=4                                                                                                
80515901                                            column=sample:tag_count, timestamp=1339304052748, value=0                                                                                                
80515902                                            column=sample:tag_count, timestamp=1339304052748, value=3       
80515903                                            column=sample:tag_count, timestamp=1339304052748, value=1                                                                                                
80515904                                            column=sample:tag_count, timestamp=1339304052748, value=2                                                                                                

2 个答案:

答案 0 :(得分:3)

在将数据插入hbase时,它没有返回任何数据,
键的数据类型是'String'(来自您的扫描结果)&amp;在获取时,RowFilter中传递的值具有“long”数据类型。使用此过滤器:

RowFilter filter = new RowFilter(CompareOp.EQUAL,new    
BinaryComparator(Bytes.toBytes(myfeatureId.toString())) );

答案 1 :(得分:0)

while循环将始终生成新过滤器并添加到过滤器列表

电路是滤波器中的所有键。此过滤器永远不会应用于单行。在while循环中只创建一个指向知道“myfeatureId”的过滤器。

while (mFilterFeatureIt.hasNext()) {
    long myfeatureId = mFilterFeatureIt.next();
System.out.println("FeatureId:"+myfeatureId+" , ");
if ( myfeatureId=="80515902") {
     RowFilter filter = new RowFilter(CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(myfeatureId)) );
     filterList.addFilter(filter);
     }
}

修改

对于行数量,查询负责。 HBase不是

HBase过滤器

将推送行选择标准过滤到HBase。可以远程和并行过滤行。使用这些功能可以帮助您避免向客户端发送不需要的行。

要从钥匙中取出一部分,请从80515900获取所有内容.. 80515909试试这个

当然从循环中删除

RowFilter filter = new RowFilter(CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(myfeatureId)) );
filterList.addFilter(filter);

并在第outputMap = HbaseUtils.getHbaseData("mytable", filterList);

上方添加
....
RowFilter filter = new RowFilter(CompareOp.EQUAL,new SubStringComparator("8051590"));
filterList.addFilter(filter);
outputMap  = HbaseUtils.getHbaseData("mytable", filterList);