Skip to content

Instantly share code, notes, and snippets.

@shrijeet
Created July 31, 2012 20:35
Show Gist options
  • Save shrijeet/3220267 to your computer and use it in GitHub Desktop.
Save shrijeet/3220267 to your computer and use it in GitHub Desktop.
Find all rows which do not have a cell corresponding to any of the columns in the given list of columns.
package hbase.experiments;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.SkipFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* Find all rows which do not have a cell corresponding to any of the columns in
* the given list of columns.
*/
public class FilterListExample {
public static void main(String args[]) throws IOException {
byte [] f = Bytes.toBytes("f");
byte [] c1 = Bytes.toBytes("w1");
byte [] c2 = Bytes.toBytes("w2");
Scan scan = new Scan();
scan.addFamily(f);
//Do not want rows which have either c1 or c2 or both.
Filter filter1 = new QualifierFilter(CompareOp.NOT_EQUAL,
new BinaryComparator(c1));
Filter filter2 = new QualifierFilter(CompareOp.NOT_EQUAL,
new BinaryComparator(c2));
List<Filter> valueFilters = new ArrayList<Filter>();
valueFilters.add(filter1);
valueFilters.add(filter2);
//SkipFilter will filter any row which has a c1 or c2.
Filter skipFilter = new SkipFilter(new FilterList(Operator.MUST_PASS_ALL, valueFilters));
scan.setFilter(skipFilter);
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "pww-30");
HTable ht = new HTable(config, "t0");
ResultScanner scanner = ht.getScanner(scan);
for (Result r : scanner) {
for (KeyValue kv : r.raw()) {
System.out.println("k=" + Bytes.toString(kv.getRow()) + ",v="
+ Bytes.toString(kv.getValue()) + ",col=" + Bytes.toString(kv.getQualifier()) + ",ts="
+ kv.getTimestamp());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment