簡體   English   中英

如何獲取包含(或等於)HBase表中特定ID的所有行?

[英]How to get all rows containing (or equaling) a particular ID from an HBase table?

我有一個方法,選擇rowkey包含傳入參數的行。

HTable table = new HTable(Bytes.toBytes(objectsTableName), connection);

public List<ObjectId> lookUp(String partialId) {
    if (partialId.matches("[a-fA-F0-9]+")) {
        // create a regular expression from partialId, which can 
        //match any rowkey that contains partialId as a substring, 
        //and then get all the row with the specified rowkey 
    } else {
        throw new IllegalArgumentException(
                "query must be done with hexadecimal values only");
    }
}

我不知道如何完成上面的代碼。

我只知道以下代碼可以在Hbase中獲取具有指定rowkey的行。

String rowkey = "123";
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);

您可以將RowFilter過濾器與RegexStringComparator一起使用來執行此操作。 或者,如果只是為了獲取與給定子字符串匹配的行,則可以將RowFilter與SubstringComparator一起使用。 這就是你使用HBase過濾器的方法:

public static void main(String[] args) throws IOException {

        Configuration conf = HBaseConfiguration.create();
        HTable table = new HTable(conf, "demo");
        Scan s = new Scan();
        Filter f = new RowFilter(CompareOp.EQUAL, new SubstringComparator("abc"));
        s.setFilter(f);
        ResultScanner rs = table.getScanner(s);
        for(Result r : rs){
            System.out.println("RowKey : " + Bytes.toString(r.getRow()));
            //rest of your logic            
        }
        rs.close();
        table.close();
}

上面的代碼將為您提供包含abc作為其rowkeys的一部分的所有行。

HTH

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM