hbase如何查询某列的值

avatar
作者
猴君
阅读量:7

在HBase中,可以通过使用Scan或Get操作来查询某列的值。

使用Scan操作:

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes;  public class HBaseQuery {     public static void main(String[] args) throws Exception {         Configuration config = HBaseConfiguration.create();         Connection connection = ConnectionFactory.createConnection(config);         TableName tableName = TableName.valueOf("your_table_name");         Table table = connection.getTable(tableName);          Scan scan = new Scan();         scan.addColumn(Bytes.toBytes("your_column_family"), Bytes.toBytes("your_column"));          ResultScanner scanner = table.getScanner(scan);         for (Result result : scanner) {             byte[] value = result.getValue(Bytes.toBytes("your_column_family"), Bytes.toBytes("your_column"));             String valueString = Bytes.toString(value);             System.out.println("Value: " + valueString);         }                  scanner.close();         table.close();         connection.close();     } } 

使用Get操作:

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes;  public class HBaseQuery {     public static void main(String[] args) throws Exception {         Configuration config = HBaseConfiguration.create();         Connection connection = ConnectionFactory.createConnection(config);         TableName tableName = TableName.valueOf("your_table_name");         Table table = connection.getTable(tableName);          Get get = new Get(Bytes.toBytes("your_row_key"));         get.addColumn(Bytes.toBytes("your_column_family"), Bytes.toBytes("your_column"));          Result result = table.get(get);         byte[] value = result.getValue(Bytes.toBytes("your_column_family"), Bytes.toBytes("your_column"));         String valueString = Bytes.toString(value);         System.out.println("Value: " + valueString);          table.close();         connection.close();     } } 

在上面的代码中,需要替换your_table_nameyour_column_familyyour_columnyour_row_key为实际的表名、列族、列和行键。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!