Skip to content

Commit

Permalink
Example filtering specific rows.
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgeorge committed Jul 21, 2016
1 parent 74178eb commit 490018d
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions ch04/src/main/java/filters/ValueNotNullFilterExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package filters;

// cc ValueNotNullFilterExample Example using the single value based filter in combination

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
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.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.NullComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;

import java.io.IOException;

public class ValueNotNullFilterExample {

public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();

HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", "colfam1");
System.out.println("Adding rows to table...");
helper.fillTable("testtable", 1, 10, 5, "colfam1");

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));

Put put = new Put(Bytes.toBytes("row-5"))
.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-6"),
Bytes.toBytes("val-6"));
table.put(put);
put = new Put(Bytes.toBytes("row-8"))
.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-6"),
Bytes.toBytes("val-6"));
table.put(put);

Delete delete = new Delete(Bytes.toBytes("row-5"))
.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-3"));
table.delete(delete);

// vv ValueNotNullFilterExample
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("colfam1"), Bytes.toBytes("col-6"),
CompareFilter.CompareOp.NOT_EQUAL, new NullComparator());
filter.setFilterIfMissing(true);
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
Bytes.toBytes("colfam1"), Bytes.toBytes("col-3"),
CompareFilter.CompareOp.NOT_EQUAL, new NullComparator());
filter2.setFilterIfMissing(true);

FilterList list = new FilterList();
list.addFilter(filter);
list.addFilter(filter2);

Scan scan = new Scan();
scan.setFilter(list);

ResultScanner scanner = table.getScanner(scan);
// ^^ ValueNotNullFilterExample
System.out.println("Results of scan:");
// vv ValueNotNullFilterExample
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println("Cell: " + cell + ", Value: " +
Bytes.toString(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength()));
}
}
scanner.close();
// ^^ ValueNotNullFilterExample
table.close();
connection.close();
}
}

0 comments on commit 490018d

Please sign in to comment.