39

I cannot figure out how to use filters in the HBase REST interface (HBase 0.90.4-cdh3u3). The documentation just gives me a schema definition for a "string", but doesn't show how to use it.

So, I'm able to do this:

curl -v -H 'Content-Type: text/xml' -d '<Scanner startRow="ddo" stopRow="ddp" batch="1024"/>' 'http://hbasegw:8080/table/scanner'

and then retrieve with

curl -s -H "Content-Type: text/xml" http://hbasegw:8080/table/scanner/13293426893883128482b | tidy -i -q -xml

But now I want to use a SingleColumnValueFilter and have to encode that somehow in the XML. Does anyone have an example for this?

Thanks, Mario

Tiago Peczenyj
  • 4,387
  • 2
  • 22
  • 35
Mario
  • 1,801
  • 3
  • 20
  • 32
  • Ok, so I figured out that I can make a JSON representation of the filter with ScannerModel->stringifyFilter(), but it still won't work. – Mario Feb 21 '12 at 09:52
  • See [HBASE-3482](https://issues.apache.org/jira/browse/HBASE-3482), when using XML format you need to XML encode the FilterModel somehow... Maybe you can figure out the right format based on the source of [ScannerModel.java](http://hbase.apache.org/xref/org/apache/hadoop/hbase/rest/model/ScannerModel.html) (specifically the inner class FilterModel) – J-B May 28 '12 at 16:11
  • 2
    One important observation: you should type `endRow` instead of `stopRow` in the Scanner XML. – André Staltz Jul 11 '12 at 13:06

1 Answers1

14

Filter fields in the Scanner XML are strings formatted as JSON. Since the JSON for the filter has many quotes in it, I recommend using a separate file for curl's -d parameter, to avoid the single quote.

curl -v -H "Content-Type:text/xml" -d @args.txt http://hbasegw:8080/table/scanner

Where the file args.txt is:

<Scanner startRow="cm93MDE=" endRow="cm93MDg=" batch="1024">
    <filter>
    {
        "latestVersion":true, "ifMissing":true, 
        "qualifier":"Y29sMQ==", "family":"ZmFtaWx5", 
        "op":"EQUAL", "type":"SingleColumnValueFilter", 
        "comparator":{"value":"MQ==","type":"BinaryComparator"}
    }
    </filter>
</Scanner>

How do you discover how the JSON filter string should look like? Here's an easy way through Java code that spits out the stringified filter given a standard Filter object from HBase's Java API.

SingleColumnValueFilter filter = new SingleColumnValueFilter(
    Bytes.toBytes("family"),
    Bytes.toBytes("col1"),
    CompareFilter.CompareOp.EQUAL,
    Bytes.toBytes("1")
);
System.out.println(ScannerModel.stringifyFilter(filter));

Notice that the JSON and the XML require data encoded in Base64. I've tested the above curl command on a table and it worked just fine.

In case you are wondering, yes, the REST API for scanners is not yet as developer-friendly as it can get.

André Staltz
  • 13,304
  • 9
  • 48
  • 58