hbase过滤器查询列族名称含有“c”的所有数据
时间: 2023-11-18 13:06:24 浏览: 139
您可以使用HBase Java API中的Filter来查询列族名称含有“c”的所有数据,具体代码如下:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseFilterExample {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf("table_name");
Scan scan = new Scan();
Filter filter = new QualifierFilter(
org.apache.hadoop.hbase.filter.CompareOperator.EQUAL,
new SubstringComparator("c")
);
scan.setFilter(filter);
ResultScanner scanner = connection.getTable(tableName).getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.listCells()) {
String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
String columnFamily = Bytes.toString(CellUtil.cloneFamily(cell));
String columnQualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowKey=" + rowKey + ", columnFamily=" + columnFamily
+ ", columnQualifier=" + columnQualifier + ", value=" + value);
}
}
}
}
```
其中,`QualifierFilter`表示按列名过滤,`SubstringComparator`表示包含子串的过滤条件,`EQUAL`表示精确匹配,而在这里是使用`SubstringComparator`过滤包含子串的列族。
阅读全文