HBase实训:纸币冠字号查询任务

一、实验目的

1.  理解分布式数据存储系统HBase的架构和工作原理。

2.  掌握HBase表的设计原则,能够根据实际业务需求设计合理的表结构。

3.  学习使用HBase Java API进行数据的插入、查询和管理。

4.  实践分布式数据存储系统在大数据环境下的应用,提升实际操作能力和问题解决能力。

二、安装配置HBase集群

1、安装并配置一个HBase集群,确保集群中的各个组件正常运行。

2、确保集群中的主节点(Master)和多个从节点(RegionServers)都可以正常通信。

三、设计HBase表结构

根据钞票交易数据的业务需求,设计合适的HBase表结构,考虑如何存储和检索钞票交易数据。

HBase表设计:

    表名:currency_transactions

    行键:<冠字号>#<交易时间>(确保唯一性,按时间查询)

    列族:

        info:存储钞票基本信息,如面额、交易金额。

        transaction:存储交易相关信息,如交易时间、交易地点、类型。

        meta:其他信息。

四、插入部分钞票交易数据并探索数据特征

1、将一部分钞票交易数据插入到设计的HBase表中。

(1)创建表:create 'currency_transactions', 'info', 'transaction', 'meta'

插入数据:

put 'currency_transactions', '123456ABC#20241201', 'info:denomination', '100'
put 'currency_transactions', '123456ABC#20241201', 'info:amount', '100'
put 'currency_transactions', '123456ABC#20241201', 'transaction:time', '2024-12-01 10:00'
put 'currency_transactions', '123456ABC#20241201', 'transaction:location', 'Beijing'
put 'currency_transactions', '123456ABC#20241201', 'transaction:type', 'Deposit'
put 'currency_transactions', '123456ABC#20241201', 'meta:notes', 'First deposit'

使用基础的HBase查询语句,探索钞票交易数据的结构和特征,确保数据可以正确存储和访问。

(1)查看表中所有数据:scan 'currency_transactions'

(2)查询特定行键的数据:

get 'currency_transactions', '123456ABC#20241201'

(3)查询特定列的数据:get 'currency_transactions', '123456ABC#20241201', 'transaction:location'

五、使用HBase Java API进行操作

1、创建Maven项目并添加相关依赖

(1)在IDEA上面新建Maven项目,下载与虚拟机中HBase一致的jdk版本1.8

(2)新建项目

(3)编辑pom.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>javaapi</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!-- HBase Client --><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.5</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>
</project>
log4j.properties:
# Set root logger level and appender
log4j.rootLogger=INFO, console
# Console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

2、HBase Java API的基本操作之表的创建

(1)代码部分:

package com.example;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class HBaseTableCreator {public static void main(String[] args) throws IOException {Configuration config = HBaseConfiguration.create();config.addResource(new Path("hbase-site.xml"));config.set("hbase.zookeeper.quorum", "192.168.125.101");config.set("hbase.zookeeper.property.clientPort", "2181");try (Connection connection = ConnectionFactory.createConnection(config);Admin admin = connection.getAdmin()) {TableName tableName = TableName.valueOf("currency_transactions");// 使用 HBase 1.x API 定义列族HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);tableDescriptor.addFamily(new HColumnDescriptor("info"));tableDescriptor.addFamily(new HColumnDescriptor("transaction"));tableDescriptor.addFamily(new HColumnDescriptor("meta"));// 检查表是否存在if (admin.tableExists(tableName)) {System.out.println("Table already exists. Deleting and recreating...");admin.disableTable(tableName);admin.deleteTable(tableName);}// 创建表admin.createTable(tableDescriptor);System.out.println("Table created successfully.");}}
}

(2)运行结果:

3、HBase Java API的基本操作——数据的插入

(1)代码部分:

package com.example;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class HBaseDataInserter {public static void main(String[] args) throws IOException {// 配置 HBase 连接Configuration config = HBaseConfiguration.create();config.addResource(new Path("hbase-site.xml"));config.set("hbase.zookeeper.quorum", "192.168.125.101");config.set("hbase.zookeeper.property.clientPort", "2181");// 定义表名String tableName = "currency_transactions";try (Connection connection = ConnectionFactory.createConnection(config)) {// 插入多行数据addRows(connection, tableName);}}public static void addRows(Connection connection, String tableName) throws IOException {try (Table table = connection.getTable(TableName.valueOf(tableName))) {// 创建 Put 对象,插入行1 (Alice)Put put1 = new Put("row1".getBytes());put1.addColumn("info".getBytes(), "name".getBytes(), "Alice".getBytes());put1.addColumn("info".getBytes(), "age".getBytes(), "30".getBytes());put1.addColumn("transaction".getBytes(), "amount".getBytes(), "1000".getBytes());put1.addColumn("transaction".getBytes(), "currency".getBytes(), "USD".getBytes());// 创建 Put 对象,插入行2 (Bob)Put put2 = new Put("row2".getBytes());put2.addColumn("info".getBytes(), "name".getBytes(), "Bob".getBytes());put2.addColumn("info".getBytes(), "age".getBytes(), "40".getBytes());put2.addColumn("transaction".getBytes(), "amount".getBytes(), "500".getBytes());put2.addColumn("transaction".getBytes(), "currency".getBytes(), "EUR".getBytes());// 批量插入table.put(put1);table.put(put2);System.out.println("Rows added successfully.");}}
}

(2)运行结果:

4、根据冠字号(行键)查询单行数据

(1)实现代码

package com.example;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class HBaseDataRetriever {public static void main(String[] args) throws IOException {// 配置 HBase 连接Configuration config = HBaseConfiguration.create();config.addResource(new Path("hbase-site.xml"));config.set("hbase.zookeeper.quorum", "192.168.125.101");config.set("hbase.zookeeper.property.clientPort", "2181");// 定义表名和行键(冠字号)String tableName = "currency_transactions";String rowKey = "row1"; // 冠字号对应的行键try (Connection connection = ConnectionFactory.createConnection(config)) {// 查询单行数据retrieveRowByKey(connection, tableName, rowKey);}}public static void retrieveRowByKey(Connection connection, String tableName, String rowKey) throws IOException {try (Table table = connection.getTable(TableName.valueOf(tableName))) {// 创建 Get 对象Get get = new Get(rowKey.getBytes());// 指定需要的列族和列(可选)get.addFamily("info".getBytes()); // 获取 "info" 列族的所有列get.addColumn("transaction".getBytes(), "amount".getBytes()); // 获取特定列// 获取结果Result result = table.get(get);// 遍历结果System.out.println("Row Key: " + rowKey);result.listCells().forEach(cell -> {String family = new String(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());String qualifier = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());String value = new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());System.out.println(family + ":" + qualifier + " = " + value);});}}
}

(2)运行结果

5、批量检索所有数据(扫描)

(1)实现代码

package com.example;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class HBaseDataScanner {public static void main(String[] args) throws IOException {// 配置 HBase 连接Configuration config = HBaseConfiguration.create();config.addResource(new Path("hbase-site.xml"));config.set("hbase.zookeeper.quorum", "192.168.125.101");config.set("hbase.zookeeper.property.clientPort", "2181");// 定义表名String tableName = "currency_transactions";try (Connection connection = ConnectionFactory.createConnection(config)) {// 扫描表数据scanTableData(connection, tableName);}}public static void scanTableData(Connection connection, String tableName) throws IOException {try (Table table = connection.getTable(TableName.valueOf(tableName))) {// 创建 Scan 对象Scan scan = new Scan();// 指定列族或列(可选)scan.addFamily("info".getBytes()); // 扫描 "info" 列族scan.addColumn("transaction".getBytes(), "currency".getBytes()); // 扫描特定列// 获取结果ResultScanner scanner = table.getScanner(scan);// 遍历结果for (Result result : scanner) {String rowKey = new String(result.getRow());System.out.println("Row Key: " + rowKey);result.listCells().forEach(cell -> {String family = new String(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());String qualifier = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());String value = new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());System.out.println(family + ":" + qualifier + " = " + value);});System.out.println("------------");}}}
}

(2)运行结果

6、根据列值(如冠字号、金额等)过滤数据

(1)实现代码

package com.example;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.*;import java.io.IOException;public class HBaseFilteredScan {public static void main(String[] args) throws IOException {// 配置 HBase 连接Configuration config = HBaseConfiguration.create();config.addResource(new Path("hbase-site.xml"));config.set("hbase.zookeeper.quorum", "192.168.125.101");config.set("hbase.zookeeper.property.clientPort", "2181");// 定义表名String tableName = "currency_transactions";try (Connection connection = ConnectionFactory.createConnection(config)) {// 使用过滤器检索数据scanTableWithFilter(connection, tableName, "transaction", "currency", "USD");}}public static void scanTableWithFilter(Connection connection, String tableName, String columnFamily, String columnQualifier, String valueToFilter) throws IOException {try (Table table = connection.getTable(TableName.valueOf(tableName))) {// 创建 Scan 对象Scan scan = new Scan();// 添加列值过滤器SingleColumnValueFilter filter = new SingleColumnValueFilter(columnFamily.getBytes(),           // 列族columnQualifier.getBytes(),        // 列名CompareFilter.CompareOp.EQUAL,     // 比较操作valueToFilter.getBytes());         // 目标值// 设置过滤器scan.setFilter(filter);// 获取结果ResultScanner scanner = table.getScanner(scan);// 遍历结果for (Result result : scanner) {String rowKey = new String(result.getRow());System.out.println("Row Key: " + rowKey);result.listCells().forEach(cell -> {String family = new String(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());String qualifier = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());String value = new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());System.out.println(family + ":" + qualifier + " = " + value);});System.out.println("------------");}}}
}

(2)运行结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/3529.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算法与数据结构——复杂度

目录 一 数据结构前言 1 数据结构 2 算法 3 算法与数据结构的关系 二 算法效率 1 算法效率&#xff1a; 2 复杂度 2.1 概念&#xff1a; 2.2分类&#xff1a; 2.3 空间复杂度在计算机高速发展的现代重要吗&#xff1f; 3 复杂度的重要性 三 时间复杂度…

usb通过hdc连接鸿蒙next的常用指令

参考官方 注册报名https://www.hiascend.com/developer/activities/details/44de441ef599450596131c8cb52f7f8c/signup?channelCodeS1&recommended496144 hdc-调试命令-调测调优-系统 - 华为HarmonyOS开发者https://developer.huawei.com/consumer/cn/doc/harmonyos-guid…

论文笔记-arXiv2025-A survey about Cold Start Recommendation

论文笔记-arXiv2025-Cold-Start Recommendation towards the Era of Large Language Models: A Comprehensive Survey and Roadmap 面向大语言模型&#xff08;LLMs&#xff09;时代的冷启动推荐&#xff1a;全面调研与路线图1.引言2.前言3.内容特征3.1数据不完整学习3.1.1鲁棒…

如何将数据库字符集改为中文,让今后所有的数据库都支持中文

最后一行有我自己的my.ini文件 数据库输入中文数据时会变为乱码&#xff0c; 这个时候&#xff0c;我们为每个数据库设置字符集&#xff0c;太过于麻烦&#xff0c;为数据库单独设置重启后又会消失 Set character_set_database’utf8’; Set character_set_server’utf8’; …

AI刷题-小R的随机播放顺序、不同整数的计数问题

目录 一、小R的随机播放顺序 问题描述 测试样例 解题思路&#xff1a; 问题理解 数据结构选择 算法步骤 最终代码&#xff1a; 运行结果&#xff1a; 二、 不同整数的计数问题 问题描述 测试样例 解题思路&#xff1a; 问题理解 数据结构选择 算法步骤 最终…

从零搭建SpringBoot3+Vue3前后端分离项目基座,中小项目可用

文章目录 1. 后端项目搭建 1.1 环境准备1.2 数据表准备1.3 SpringBoot3项目创建1.4 MySql环境整合&#xff0c;使用druid连接池1.5 整合mybatis-plus 1.5.1 引入mybatis-plus1.5.2 配置代码生成器1.5.3 配置分页插件 1.6 整合swagger3&#xff08;knife4j&#xff09; 1.6.1 整…

在.NET用C#将Word文档转换为HTML格式

将Word文档转换为HTML格式尤其具有显著的优势&#xff0c;它不仅能够确保文档内容在多种设备和平台上保持一致灵活的显示&#xff0c;还便于通过网络进行传播和集成到各种Web应用中。随着越来越多的企业和开发者寻求更灵活、更具兼容性的文件处理方式&#xff0c;.NET框架下的C…

GPT-5 传言:一场正在幕后发生的 AI 变革

新的一年&#xff0c;让我们从一个引人入胜的话题开始&#xff1a;如果我告诉你&#xff0c;GPT-5 并非虚构&#xff0c;而是真实存在呢&#xff1f;它不仅真实存在&#xff0c;而且正在你看不见的地方悄然塑造着世界。我的基本假设是&#xff1a;OpenAI 已经秘密开发出 GPT-5&…

【机器学习实战】kaggle 欺诈检测---使用生成对抗网络(GAN)解决欺诈数据中正负样本极度不平衡问题

【机器学习实战】kaggle 欺诈检测---如何解决欺诈数据中正负样本极度不平衡问题https://blog.csdn.net/2302_79308082/article/details/145177242 本篇文章是基于上次文章中提到的对抗生成网络&#xff0c;通过对抗生成网络生成少数类样本&#xff0c;平衡欺诈数据中正类样本极…

ZNS SSD垃圾回收优化方案解读-2

四、Brick-ZNS 关键设计机制解析 Brick-ZNS 作为一种创新的 ZNS SSD 设计&#xff0c;聚焦于解决传统 ZNS SSDs 在垃圾回收&#xff08;GC&#xff09;过程中的数据迁移低效问题&#xff0c;其核心特色为存储内数据迁移与地址重映射功能。在应用场景中&#xff0c;针对如 Rock…

浅谈云计算14 | 云存储技术

云存储技术 一、云计算网络存储技术基础1.1 网络存储的基本概念1.2云存储系统结构模型1.1.1 存储层1.1.2 基础管理层1.1.3 应用接口层1.1.4 访问层 1.2 网络存储技术分类 二、云计算网络存储技术特点2.1 超大规模与高可扩展性2.1.1 存储规模优势2.1.2 动态扩展机制 2.2 高可用性…

C++ 强化记忆

1 预处理指令 # include <> #include <filename> 添加系统头文件。 #include "filename" 添加自定义头文件。 # include <iostream> 对应使用cout cin的情况下需要添加 # include <string> 对应使用字符串情况 # include <fstream> …

web worker 前端多线程一、

前言&#xff1a; JavaScript 语言采用的是单线程模型&#xff0c;也就是说&#xff0c;所有任务只能在一个线程上完成&#xff0c;一次只能做一件事。前面的任务没做完&#xff0c;后面的任务只能等着。随着电脑计算能力的增强&#xff0c;尤其是多核 CPU 的出现&#xff0c;单…

Vue项目搭建教程超详细

目录 一. 环境准备 1. 安装node.js 2. 安装Vue cli 二. 创建 Vue 2 项目 1. 命令行方式 2. vue ui方式 一. 环境准备 1. 安装node.js 可参考node.js卸载与安装超详细教程-CSDN博客 2. 安装Vue cli npm install -g vue/cli检查是否安装成功 vue --version Vue CLI …

IM聊天学习资源

文章目录 参考链接使用前端界面简单效果消息窗口平滑滚动至底部vue使用watch监听vuex中的变量变化 websocket握手认证ChatKeyCheckHandlerNettyChatServerNettyChatInitializer 参考链接 zzhua/netty-chat-web - 包括前后端 vue.js实现带表情评论功能前后端实现&#xff08;仿…

彻底理解JVM类加载机制

文章目录 一、类加载器和双亲委派机制1.1、类加载器1.2、双亲委派机制1.3、自定义类加载器1.4、打破双亲委派机制 二、类的加载 图片来源&#xff1a;图灵学院   由上图可知&#xff0c;创建对象&#xff0c;执行其中的方法&#xff0c;在java层面&#xff0c;最重要的有获取…

使用FRP进行内网穿透

一、基本概念 内网穿透&#xff1a;它是一种网络技术或方法&#xff0c;旨在允许外部网络&#xff08;如互联网&#xff09;访问位于内部网络&#xff08;内网&#xff09;中的设备或服务。由于内部网络通常处于NAT&#xff08;网络地址转换&#xff09;、防火墙或其他安全机制…

Mysql常见问题处理集锦

Mysql常见问题处理集锦 root用户密码忘记&#xff0c;重置的操作(windows上的操作)MySQL报错&#xff1a;ERROR 1118 (42000): Row size too large. 或者 Row size too large (&#xff1e; 8126).场景&#xff1a;报错原因解决办法 详解行大小限制示例&#xff1a;内容来源于网…

《计算机网络》课后探研题书面报告_网际校验和算法

网际校验和算法 摘 要 本文旨在研究和实现网际校验和&#xff08;Internet Checksum&#xff09;算法。通过阅读《RFC 1071》文档理解该算法的工作原理&#xff0c;并使用编程语言实现网际校验和的计算过程。本项目将对不同类型的网络报文&#xff08;包括ICMP、TCP、UDP等&a…