How to scan and filter data in HBase?
In HBase, data can be scanned and filtered using the Scan object. This object allows users to specify the table to scan, starting and ending row keys, and add filters to filter the scanned results.
The following is a sample code demonstrating how to scan and filter data in HBase.
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf("my_table");
Table table = connection.getTable(tableName);
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes("start_row_key"));
scan.withStopRow(Bytes.toBytes("end_row_key"));
Filter filter = new SingleColumnValueFilter(
Bytes.toBytes("cf"),
Bytes.toBytes("col"),
CompareOperator.EQUAL,
Bytes.toBytes("value")
);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
// 处理扫描结果
for (Cell cell : result.rawCells()) {
System.out.println("Row key: " + Bytes.toString(CellUtil.cloneRow(cell)) +
" Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
scanner.close();
table.close();
connection.close();
In the example above, a Scan object is first created and the start row key, end row key, and filter are set. Then a ResultScanner object is obtained through the table.getScanner(scan) method to retrieve the scan results. Finally, iterate through the ResultScanner object to process the results of each row of data.
It’s important to note that there are other types of filters available in HBase, such as PrefixFilter, RowFilter, and FamilyFilter. Users can choose the appropriate filter based on their specific needs to filter scan results.