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());
        }
    }
}
相关推荐
摇滚侠2 分钟前
2025最新 SpringCloud 教程,Nacos-总结,笔记19
java·笔记·spring cloud
在逃热干面6 分钟前
(笔记)获取终端输出保存到文件
java·笔记·spring
爱笑的眼睛117 分钟前
深入理解MongoDB PyMongo API:从基础到高级实战
java·人工智能·python·ai
笃行客从不躺平17 分钟前
遇到大SQL怎么处理
java·开发语言·数据库·sql
q***876024 分钟前
Spring Boot 整合 Keycloak
java·spring boot·后端
Billow_lamb25 分钟前
Spring Boot2.x.x全局拦截器
java·spring boot·后端
上不如老下不如小35 分钟前
2025年第七届全国高校计算机能力挑战赛初赛 Java组 编程题汇总
java·计算机能力挑战赛
泉城老铁1 小时前
Springboot对接mqtt
java·spring boot·后端
源码_V_saaskw1 小时前
JAVA国际版同城跑腿源码快递代取帮买帮送同城服务源码支持Android+IOS+H5
android·java·ios·微信小程序
TT哇1 小时前
消息推送机制——WebSocket
java·网络·websocket·网络协议