HBase表A有一个列族"info”,2个列为“info:name”、“info:age”,补全代码,实现查询name为“XiaoMing”的数据
时间: 2024-05-02 21:21:55 浏览: 79
以下是Java代码示例:
```
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("A"));
Scan scan = new Scan();
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOperator.EQUAL, Bytes.toBytes("XiaoMing"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] nameValue = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
byte[] ageValue = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
String name = Bytes.toString(nameValue);
String age = Bytes.toString(ageValue);
System.out.println("Name: " + name + ", Age: " + age);
}
table.close();
connection.close();
```
这段代码使用HBase Java API查询表A中列族为"info",列名为"name",值为"XiaoMing"的数据。通过Scan对象设置过滤器,使用SingleColumnValueFilter来指定列族、列名和比较运算符,最后通过ResultScanner遍历查询结果,输出name和age的值。
阅读全文