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());
        }
    }
}
相关推荐
可乐_不渴13 分钟前
【Linux】 进程虚拟地址空间
java·linux·javascript
SimonKing15 分钟前
号外号外,Stream-Nexus推送项目支持集群部署了
java·后端·程序员
vipxieliang26 分钟前
ValidX vs 正则表达式:可读性与维护性对比
java·spring boot
3378033 分钟前
5分钟搞懂登录认证和授权框架Spring Security
java·spring boot
vipxieliang33 分钟前
Spring Security 6.x 实战:认证、授权与 JWT 集成
java·spring boot
world781122637 分钟前
【C++】set 和 map
java·前端·c++·set·map
学长毕设37 分钟前
基于SpringBoot的社区体育馆预约系统的设计与实现
java·spring boot·后端
Rain的Java大神之路1 小时前
100 亿条数据如何快速写入 MySQL?从 231 天到 3.5 小时,我把面试官问懵了
java·后端·面试
xyouyuweizhu1 小时前
Java IO 流详解:流的作用、流的划分与标准输入输出流
java·python·php
范同学~2 小时前
项目添加水印
java·前端·javascript