Linux 拨号vps windows公众号手机端

hbase如何查询某列的值

lewis 5年前 (2020-04-09) 阅读数 11 #大数据
文章标签 hbase

在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为实际的表名、列族、列和行键。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门