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());
        }
    }
}
相关推荐
caoerzhong几秒前
跨境海外仓怎么管:JeeWMS 开源 Java 仓库管理系统打通头程、海外仓与尾程
java·开发语言·开源
MayBaymax6 分钟前
Elasticsearch 原理与用法
java·elasticsearch
马剑威(威哥爱编程)8 分钟前
【AI全栈后端12-02】Spring Boot 跑通第一个 AI 对话接口:HR 政策问答机器人实战
java·人工智能·spring boot·机器人
IT枫斗者枫哥10 分钟前
AI返回合法JSON,字段就可信吗?给抽取结果补一道业务校验
java·人工智能·后端
用户31268748772012 分钟前
Java IO/NIO/AIO 演进:从 BIO 到 Netty 的底层逻辑
java
IT枫斗者枫哥14 分钟前
同一个requestId换了参数,为什么不能直接返回旧结果?
java·后端
天天被压力15 分钟前
【别再到处找免费股票数据API了:官方204个接口,32篇一次讲透 #06】Python实时行情总报错?五档盘口+逐笔一次跑通
java·人工智能·python
今年下半年28 分钟前
【网站连通性检测】java项目telnet、curl命令的使用
java·curl·telnet
摇滚侠29 分钟前
《Spring Boot 3:高级与架构设计》第 2 章 IOC 容器的高级机制 编程式驱动 IOC 阅读笔记 7
java·spring boot·笔记
IT枫斗者枫哥31 分钟前
ORDER BY时间还会漏单?用相同时间和插入记录测一次分页
java·后端