Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
简介: Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表

点一下关注吧!!!非常感谢!!持续更新!!!

目前已经更新到了:

Hadoop

HDFS

MapReduce

Hive

Flume

Sqoop

Zookeeper

HBase 正在···

章节内容

上一节我们完成了:


HBase Shell 的使用

HBase 增、删、改、查等操作

HBase列族相关的操作

背景介绍

这里是三台公网云服务器,每台 2C4G,搭建一个Hadoop的学习环境,供我学习。

之前已经在 VM 虚拟机上搭建过一次,但是没留下笔记,这次趁着前几天薅羊毛的3台机器,赶紧尝试在公网上搭建体验一下。


2C4G 编号 h121

2C4G 编号 h122

2C2G 编号 h123

新建工程

新建一个Maven工程,这里就跳过了,不重复描述了。

POM

更新我们的POM文档,加入如下的依赖:

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

建立新表

public class Test01 {

    public static void main(String[] args) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        // 创建表描述器
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("test01"));
        // 设置列族描述器
        descriptor.addFamily(new HColumnDescriptor("base_info"));

        // 建立表
        admin.createTable(descriptor);
        System.out.println("test01 表建立完毕");

        admin.close();
        connection.close();
    }

}

运行上面的代码,可以得到如下的结果:

插入数据

public class Test02 {

    public static void main(String[] args) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        // 插入数据
        Table table = connection.getTable(TableName.valueOf("test01"));
        // 设定 row key
        Put put = new Put(Bytes.toBytes("rk1"));
        // 列族 列 值
        put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("wuzikang"));
        // 执行插入
        table.put(put);
        table.close();
        System.out.println("rk1 base_info:name wuzikang 数据插入成功!");

        admin.close();
        connection.close();
    }

}

运行可以获得如下的结果:

删除数据

public class Test03 {

    public static void main(String[] args) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        // 删除数据
        Table table02 = connection.getTable(TableName.valueOf("test01"));
        Delete delete = new Delete(Bytes.toBytes("rk1"));
        table02.delete(delete);
        table02.close();
        System.out.println("rk1 数据删除成功!");

        admin.close();
        connection.close();
    }

}

运行可以获得如下的结果:

获取列族

public class Test04 {

    public static void main(String[] args) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        // 获取某个列族信息
        HTable table = (HTable) connection.getTable(TableName.valueOf("test01"));
        Get get = new Get(Bytes.toBytes("rk1"));
        get.addFamily(Bytes.toBytes("base_info"));
        // 执行查询
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            String cf = Bytes.toString(CellUtil.cloneFamily(cell));
            String column = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
            System.out.println("rowKey: " + rowKey + ", " + cf + ", " + column + ", " + value);
        }
        table.close();

        admin.close();
        connection.close();
    }

}

运行可以获得如下的结果:

扫描全表

public class Test05 {

    public static void main(String[] args) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        HTable table = (HTable) connection.getTable(TableName.valueOf("test01"));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                System.out.println("rowKey: " + rowkey + ", " + cf + ", " + column + ", " + value);
            }
        }
        table.close();

        admin.close();
        connection.close();
    }

}

运行可以获得如下结果:

Scan+Row

public class Test06 {

    public static void main(String[] args) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        HTable table = (HTable) connection.getTable(TableName.valueOf("test01"));
        Scan scan = new Scan();
        scan.setStartRow("rk1".getBytes());
        scan.setStopRow("rk2".getBytes());

        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                System.out.println("rowKey: " + rowkey + ", " + cf + ", " + column + ", " + value);
            }
        }
        table.close();


        admin.close();
        connection.close();
    }

}


相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
分布式计算 Hadoop Shell
Hadoop-35 HBase 集群配置和启动 3节点云服务器 集群效果测试 Shell测试
Hadoop-35 HBase 集群配置和启动 3节点云服务器 集群效果测试 Shell测试
24 4
|
5天前
|
分布式计算 Hadoop Shell
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
23 3
|
5天前
|
SQL 分布式计算 Hadoop
Hadoop-34 HBase 安装部署 单节点配置 hbase-env hbase-site 超详细图文 附带配置文件
Hadoop-34 HBase 安装部署 单节点配置 hbase-env hbase-site 超详细图文 附带配置文件
20 2
|
1天前
|
存储 机器学习/深度学习 应用服务中间件
阿里云倚天云服务器实例:计算型c8y、通用型g8y、内存型r8y实例介绍
阿里云倚天云服务器是基于阿里云自研的倚天710 ARM架构CPU打造的高性能计算产品系列,它依托先进的第四代神龙架构,旨在为用户提供稳定可预期的超高效能体验。倚天云服务器在存储、网络性能及计算稳定性方面实现了显著提升,主要得益于其芯片级的快速路径加速技术。本文将深度解析阿里云倚天云服务器的计算型c8y、通用型g8y、内存型r8y实例,探讨其优势及适用场景,以供选择参考。
|
2天前
|
网络协议 应用服务中间件 网络安全
阿里云轻量应用服务器的使用限制
阿里云轻量应用服务器的使用限制
|
5天前
|
弹性计算 负载均衡 算法
负载均衡如何帮助阿里云国际服务器搭建的网站或应用程序?
负载均衡如何帮助阿里云国际服务器搭建的网站或应用程序?
|
4天前
阿里云国际版购买了服务器后如何下载发票?
阿里云国际版购买了服务器后如何下载发票?
|
5天前
|
弹性计算 Linux 数据库
阿里云国际版如何迁移Linux云服务器系统盘中的数据
阿里云国际版如何迁移Linux云服务器系统盘中的数据
|
5天前
|
弹性计算 网络协议 Ubuntu
如何在阿里云国际版Linux云服务器中自定义配置DNS
如何在阿里云国际版Linux云服务器中自定义配置DNS
|
5天前
|
弹性计算 安全 Linux
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法