java: 从HBase中读取数据

一、添加依赖:

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.2</version>
        </dependency>


二、使用Scanner读取数据示例:

package cn.edu.tju;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

public class TestHBaseRead {
    public static void main(String[] args) throws Exception {
        Configuration config = HBaseConfiguration.create();// create configuration
        //zookeeper 地址
        config.set("hbase.zookeeper.quorum","xxx.xxx.xxx.xxx");//
        //zookeeper端口
        config.set("hbase.zookeeper.property.clientPort", "2181");//
        //表名,必须提前在hbase中创建
        String tableName ="c1";
        //row key
        String rowKey = "myKey2";
        //family,必须是hbase中有的family
        String familyName = "fm2";
        //指定的某个column name
        String specifiedColumnName = "by";

        Connection connection = ConnectionFactory.createConnection(config);

        Get g = new Get(rowKey.getBytes());
        g.addFamily(familyName.getBytes());
        try {
            Table table = connection.getTable(TableName.valueOf(tableName));
            Result result = table.get(g);
            CellScanner cellScanner =result.cellScanner();
            while (cellScanner.advance()){
                Cell cell = cellScanner.current();
                byte[] rowArray = cell.getRowArray(); //row key 字节数组
                byte[] familyArray = cell.getFamilyArray(); //列族 字节数组
                byte[] qualifierArray = cell.getQualifierArray();//列名 字节数据
                byte[] valueArray = cell.getValueArray();// 值 字节数组

                String columnName=new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength());
                String columnValue=new String(valueArray,cell.getValueOffset(),cell.getValueLength());
                if(specifiedColumnName.equals(columnName)){
                    System.out.println(columnValue);
                }
            }
        }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,可以使用HBase API来从HBase读取数据。以下是一个示例代码,演示如何从HBase读取数据: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class HBaseReader { public static void main(String[] args) throws Exception { // 创建HBase配置对象 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); // 设置ZooKeeper地址 // 创建HBase连接对象 Connection conn = ConnectionFactory.createConnection(conf); // 获取表对象 Table table = conn.getTable(TableName.valueOf("mytable")); // 创建Get对象 Get get = new Get(Bytes.toBytes("rowkey1")); // 读取数据 Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("column1")); // 输出数据 System.out.println(Bytes.toString(value)); // 关闭表和连接 table.close(); conn.close(); } } ``` 在上面的代码,我们首先创建了一个HBase配置对象,并将ZooKeeper地址设置为"localhost"。接着,我们创建了一个HBase连接对象,并从该连接获取了表对象。然后,我们创建了一个Get对象,并使用该对象读取了一行数据。最后,我们从Result对象获取了列族为"cf"、列名为"column1"的值,并将其输出。最后,我们关闭了表和连接对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值