实验三:熟悉常用的HBase操作

news/2025/4/2 6:17:29/

实验环境:
(1)操作系统:Linux(建议 Ubuntu 16.04 或 Ubuntu 18.04)。
(2)Hadoop 版本:3.1.3。
(3)HBase 版本:2.2.2。
(4)JDK 版本:1.8。
(5)Java IDE: Eclipse。
实验内容与完成情况:
(1)现有以下关系数据库中的表和数据(见表14-3〜表14-5),要求将其转换为适合于
HBase存储的表并插入数据。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(1)学生Student表
创建表的HBase Shell命令语句如下:
在这里插入图片描述

第二行数据
在这里插入图片描述

第三行数据

在这里插入图片描述

(2)课程Course表
创建表的HBase Shell命令语句如下:
在这里插入图片描述
在这里插入图片描述

(3)选课表
创建表的HBase Shell命令语句如下:

在这里插入图片描述

(2)编程实现以下功能。
①createTable(String tableName, String]] fields)。
创建表,参数tableName 表的名称,字符串数组fields *存储记录各字段名的数组。 要求当HBase已经存在名为tableName的表时,先删除原有的表,再创建新的表。
Java代码:

package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;public class CreateTable {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void createTable(String tableName,String[] fields) throws IOException {init();TableName tablename = TableName.valueOf(tableName);if(admin.tableExists(tablename)){System.out.println("table is exists!");admin.disableTable(tablename);admin.deleteTable(tablename);//删除原来的表}TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);for(String str : fields){tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build());admin.createTable(tableDescriptor.build());}close();}//建立连接public static void init() {configuration = HBaseConfiguration.create();//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}//关闭连接public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String[] fields = {"Score"};try {createTable("person", fields);} catch (IOException e) {e.printStackTrace();}}}

结果:
在这里插入图片描述
在这里插入图片描述

②addRecord(String tableName, String row, String]] fields, String口 values) 。
向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对 应的数据valueso其中,fields中每个元素如果对应的列族下还有相应的列限定符,用 “columnFamily: column"表示。例如,同时向MathComputerEnglish三列添加成绩时,字 符串数组 fields 为( “Score: Math” ," Score: Computer" , “Score: English” },数组 values 存储 这三门课的成绩。

Java代码:

package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class addRecord {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {init();Table table = connection.getTable(TableName.valueOf(tableName));for (int i = 0; i != fields.length; i++) {Put put = new Put(row.getBytes());String[] cols = fields[i].split(":");put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());table.put(put);}table.close();close();}public static void init() {configuration = HBaseConfiguration.create();//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};String[] values = {"99", "80", "100"};try {addRecord("tableName", "Score", fields, values);} catch (IOException e) {e.printStackTrace();}}
}

结果:
在这里插入图片描述

③scanColumn(String tableName, String column)
浏览表tableName某列的数据,如果某行记录中该列数据不存在,则返回null。要求 当参数column为某列族名时,如果底下有若干个列限定符,则要列出每个列限定符代表 的列的数据;当参数column为某列具体名(例如“ Score: Math")时,只需要列出该列的 数据。

Java代码:

package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;public class scanColumn {public static Configuration configuration;public static Connection connection;public static Admin admin;public static void scanColumn(String tableName, String column) throws IOException {init();Table table = connection.getTable(TableName.valueOf(tableName));Scan scan = new Scan();scan.addFamily(Bytes.toBytes(column));ResultScanner scanner = table.getScanner(scan);for (Result result = scanner.next(); result != null; result = scanner.next()) {showCell(result);}table.close();close();}public static void showCell(Result result) {Cell[] cells = result.rawCells();for (Cell cell : cells) {System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");System.out.println("Timetamp:" + cell.getTimestamp() + " ");System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");}}public static void init() {configuration = HBaseConfiguration.create();//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}// 关闭连接public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {scanColumn("tableName", "Score");} catch (IOException e) {e.printStackTrace();}}
}

结果:
在这里插入图片描述

④modifyData(String tableName, String row, String column) 。
修改表tableName,即修改行row(可以用学生姓名S_Name表示)、列column指定的 单元格的数据。

Java代码:

package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;import java.io.IOException;public class modifyData {public static long ts;public static Configuration configuration;public static Connection connection;public static Admin admin;public static void modifyData(String tableName, String row, String column, String val) throws IOException {init();Table table = connection.getTable(TableName.valueOf(tableName));Put put = new Put(row.getBytes());Scan scan = new Scan();ResultScanner resultScanner = table.getScanner(scan);for (Result r : resultScanner) {for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {ts = cell.getTimestamp();}}put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());table.put(put);table.close();close();}public static void init() {configuration = HBaseConfiguration.create();//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {modifyData("tableName", "Score", "Math", "100");} catch (IOException e) {e.printStackTrace();}}
}

结果:
在这里插入图片描述

⑤deleteRow(String tableName, String row)。
删除表tableName中row指定的行的记录。

Java代码:

package com.xusheng.HBase.shiyan31;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;public class deleteRow {public static long ts;public static Configuration configuration;public static Connection connection;public static Admin admin;public static void deleteRow(String tableName, String row) throws IOException {init();Table table = connection.getTable(TableName.valueOf(tableName));Delete delete=new Delete(row.getBytes());table.delete(delete);table.close();close();}public static void init() {configuration = HBaseConfiguration.create();//configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void close() {try {if (admin != null) {admin.close();}if (null != connection) {connection.close();}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {try {deleteRow("tableName", "Score");} catch (IOException e) {e.printStackTrace();}}
}

结果:
在这里插入图片描述


http://www.ppmy.cn/news/84453.html

相关文章

[ubuntu]卸载ubuntu18.04显卡驱动报错cuda-libraries-11-4 : Depends: cuda-cudart-11-4

卸载ubuntu18.04上显卡驱动报错: The following packages have unmet dependencies: cuda-libraries-11-4 : Depends: cuda-cudart-11-4 (> 11.4.148) but 11.4.43-1 is installed Depends: cuda-nvrtc-11-4 (> 11.4.152) but 11.4.5…

数据驱动运营增长

利用数据来分析、优化和提升产品或服务的各个方面,从而实现业务目标的方法叫数据驱动运营增长。用好数据,在运营中能精准地定位用户需求、痛点、偏好和行为,细分用户群体以提供个性化的产品或服务,精细化地管理和优化每一个环节和…

Rust 一门赋予每个人构建可靠且高效软件能力的语言

目录 Rust 安装 尝试 hello, world 编译 链接出错 开启 Rust 之旅 官方教程 《Rust 程序设计语言》 《通过例子学 Rust》 核心文档 标准库 版本指南 CARGO 手册 RUSTDOC 手册 RUSTC 手册 编译错误索引表 非官方翻译教程 Rust 程序设计语言 简体中文版 通…

一分钟掌握技术术语:API(接口)|电商平台API接口展示示例

很多产品经理在项目开发过程中经常听到:你调我这个接口就好了;这个功能你写个接口给我;有什么不懂的就看下API接口文档。 开发经常说的接口是什么意思呢?术语解释:API(Application Programming Interface&a…

【算法题】6441. 求一个整数的惩罚数

插: 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 坚持不懈,越努力越幸运,大家一起学习鸭~~~ 题目: 给你一个正整数 n ,请你…

python list,dict操作

一、list 操作 Python中的列表是一种有序、可变的数据类型,可以存储任意类型的数据。以下是Python中常用的列表操作: 创建列表:使用[]或list()函数创建一个空列表,或者使用[value1, value2, ...]创建一个包含初始值的列表。 访问…

「恋爱事务」NFT 作品集

「恋爱事务 (Love Affairs)」服装 NFT系列允许玩家在体验中使用 Opera Garnier 歌剧院的 T台大厅,并参与互动竞赛。参与 The Sandbox 元宇宙首个以爱情为主题的概念时装秀! 该系列已于 5 月 22 日晚上 11 点在 The Sandbox 市场平台上线。 NFT 系列介绍 …

vue实现用户动态权限登录

一、使用vueelementUI搭登录框架,主要就是1、2、3、4 配置: ①vue.config.js use strict const path require(path)function resolve(dir) {return path.join(__dirname, dir) }// All configuration item explanations can be find in https://cli.v…