使用RowFilter无法使用按键查询HBase表 [英] Query HBase table by key using RowFilter not working

查看:175
本文介绍了使用RowFilter无法使用按键查询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(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;
尝试{
htable = new HTable(HTableConfiguration.getHTableConfiguration(),table);
扫描扫描=新扫描();
scan.setFilter(filter);

ResultScanner resultScanner = htable.getScanner(scan);
迭代器<结果> results = resultScanner.iterator();

while(results.hasNext()){

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

HashMap< String,String> colData = new HashMap< String,String>(); (KeyValue列:列){
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();
}
}
返回数据;
}

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

outputMap映射的大小:0



我看到特性id的值是我想要,但即使key存在于hbase表中,我也总能得到上面的输出结果。任何人都可以告诉我,我做错了什么?



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

我正在尝试执行一个与 select * FROM mytable相同的SQL,其中featureId位于(80515900,80515901,80515902)我在HBase中实现相同的想法是为每个featureId创建一个带有一个过滤器的过滤器列表。这是正确的吗?



以下是我的表格内容

 扫描'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


解决方案

在将数据插入到hbase时,它不返回任何数据,

key的数据类型是'String'(来自您的扫描结果)&在获取时,在RowFilter中传递的值具有长数据类型。使用这个过滤器:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ .toString())));


I have a HBase table (from java) and i want to query the table by list of keys. I did the following, but its not working.

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 ,

Size of outputMap map: 0

I see that value of feature id is what i want , but I always get the above output even if the key is present in the hbase table. Can anyone tell me what am i doing wrong ?

EDIT: I posted the code for my hbase util method too above, so that you can point me to any bugs there.

I am trying to do an SQL equivalent of select * FROM mytable where featureId in (80515900, 80515901, 80515902) My idea to achieve the same in HBase was to create a filter list with one filter for each featureId. Is that correct ?

Here is the content of my table

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                                                                                                

解决方案

Its not returning any data as while inserting the data into hbase,
the data-type for key is 'String' (from your scan result) & while fetching, the value passed in RowFilter has 'long' data type. Use this filter:

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

这篇关于使用RowFilter无法使用按键查询HBase表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆