使用过滤器查询指定行的数据
package step1;
import java.io.IOException;
import org.apache.hadoop.cli.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.*;
public class Task {
public void query(String tName) throws Exception {
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf(tName);
Table table = conn.getTable(tableName);
Scan scan1 = new Scan();
scan1.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("gender"));
Filter filter1 = new RowFilter(CompareOperator.EQUAL,
new BinaryComparator(Bytes.toBytes("2018")));
scan1.setFilter(filter1);
ResultScanner scanner1 = table.getScanner(scan1);
System.out.println("row:2018");
for (Result result : scanner1) {
for(Cell cell : result.listCells()){
System.out.println("basic_info:gender " + new String(CellUtil.cloneValue(cell),"utf-8") );
}
}
scanner1.close();
Scan scan2 = new Scan();
scan2.addColumn(Bytes.toBytes("school_info"), Bytes.toBytes("college"));
Filter filter2 = new RowFilter(CompareOperator.GREATER,
new BinaryComparator(Bytes.toBytes("2018")));
scan2.setFilter(filter2);
ResultScanner scanner2 = table.getScanner(scan2)