完整原版实验报告word文件:实验三:熟悉常用的HBase操作

—————————————————————————————————

"大数据技术原理与应用"课程实验报告

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

姓名:朱小凡

日期:2022/3/29

1、实验环境:

设备名称 LAPTOP-9KJS8HO6

处理器 Intel® Core™ i5-10300H CPU @ 2.50GHz 2.50 GHz

机带 RAM 16.0 GB (15.8 GB 可用)

主机操作系统 Windows 10 家庭中文版

虚拟机操作系统 ubuntukylin-16.04

Hadoop 版本 3.1.3

JDK 版本 1.8

Java IDE:Eclipse

系统类型 64 位操作系统, 基于 x64 的处理器

笔和触控 没有可用于此显示器的笔或触控输入

2、实验内容与完成情况:

1.编程实现以下指定功能,并用 Hadoop提供的 HBase Shell
命令完成相同任务

(1) 列出 HBase所有的表的相关信息,例如表名。

a.Shell命令

HBase Shell:List

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qKgysVBG-1651743333681)(media/image1.png)]{width="2.666897419072616in"
height="0.7417311898512686in"}

图1.列出表名

b.java命令

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.client.*;

public class test1 {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

/**

* @param args

* 建立连接

*/

public static void init(){

configuration = HBaseConfiguration.create();

configuration.set(“hbase.rootdir”, “hdfs://localhost:9000/hbase”);

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();

}

}

/*

* 查看已有表

* @throws IOException

*/

public static void listTables() throws IOException{

init();

@SuppressWarnings(“deprecation”)

HTableDescriptor hTableDescriptors [] = admin.listTables();

for(HTableDescriptor hTableDescriptor : hTableDescriptors){

System.out.println(hTableDescriptor.getNameAsString());

}

close();

}

public static void main(String[] args) {

// TODO Auto-generated method stub

test1 t = new test1();

try{

System.out.println(“以下为Hbase数据库中所存的表信息”);

t.listTables();

}catch (IOException e){

e.printStackTrace();

}

}

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b4T1utrr-1651743333682)(media/image2.png)]{width="6.0in" height="2.8569444444444443in"}

图2.列出表名(java)

(2)在终端打印出指定的表的所有记录数据。

a.Shell命令

scan ‘student’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4hm7IDjn-1651743333683)(media/image3.png)]{width="3.1586067366579176in"
height="0.6083858267716535in"}

图3.打印指定表数据

b.java代码

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

import java.util.Scanner;

public class Test_2 {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

/**

* @param args

*/

//建立连接

public static void init(){

configuration = HBaseConfiguration.create();

configuration.set(“hbase.rootdir”,“hdfs://localhost:9000/hbase”);

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 getData(String tableName)throws IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

for(Result result:scanner)

{

showCell((result));

}

close();

}

/**

* 格式化输出

* @param result

*/

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(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");

System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");

System.out.println();

}

}

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

Test_2 t =new Test_2();

System.out.println(“请输入要查看的表名”);

Scanner scan = new Scanner(System.in);

String tableName=scan.nextLine();

System.out.println(“信息如下:”);

t.getData(tableName);

}

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kI2qmLnf-1651743333685)(media/image4.png)]{width="6.0in" height="2.654861111111111in"}

图4.打印指定表数据(java)

(3)向已经创建好的表添加和删除指定的列族或列。

a.Shell命令

put ‘student’,‘95001’,‘Sname’,‘LiYing’

put ‘student’,‘95001’,‘Ssex’,‘male’

put ‘student’,‘95001’,‘Sage’,‘22’

put ‘student’,‘95001’,‘Sdept’,‘CS’

put ‘student’,‘95001’,‘course:math’,‘80’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tn7Ze3tF-1651743333686)(media/image5.png)]{width="6.0in" height="3.6326388888888888in"}

图5.添加或删除指定列族或列

b.java代码

import java.io.IOException;

import java.util.Scanner;

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.Admin;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.client.Table;

public class Test_3 {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

//建立连接

public static void init(){

configuration = HBaseConfiguration.create();

configuration.set(“hbase.rootdir”,“hdfs://localhost:9000/hbase”);

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();

}

}

/**

* 向某一行的某一列插入数据

* @param tableName 表名

* @param rowKey 行键

* @param colFamily 列族名

* @param col 列名(如果其列族下没有子列,此参数可为空)

* @param val 值

* @throws IOException

*/

public static void insertRow(String tableName,String rowKey,String
colFamily,String col,String val) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Put put = new Put(rowKey.getBytes());

put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());

table.put(put);

table.close();

close();

}

/**

* 根据表名查找表信息

*/

public static void getData(String tableName)throws IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

for(Result result:scanner)

{

showCell((result));

}

close();

}

/**

* 格式化输出

* @param result

*/

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(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");

System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");

System.out.println();

}

}

/**

* 删除数据

* @param tableName 表名

* @param rowKey 行键

* @param colFamily 列族名

* @param col 列名

* @throws IOException

*/

public static void deleteRow(String tableName,String rowKey,String
colFamily,String col) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(rowKey.getBytes());

boolean flag2 =true;

while(flag2)

{

System.out.println(“请输入你的选择 1-删除列族的所有数据
2-指定列的数据”);

Scanner scanner=new Scanner(System.in);

String chooseString = scanner.nextLine();

switch (chooseString) {

case “1”:

{

//删除指定列族的所有数据

delete.addFamily(colFamily.getBytes());

table.delete(delete);

table.close();

close();

break;

}

case “2”:

{

//删除指定列的数据

delete.addColumn(colFamily.getBytes(), col.getBytes());

table.delete(delete);

table.close();

close();

break;

}

default:

{

System.out.println(" 你的输入有误 !!! ");

table.close();

close();

break;

}

}

System.out.println(" 你要继续操作吗? 是-true 否-false ");

flag2=scanner.nextBoolean();

}

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Test_3 t =new Test_3();

boolean flag =true;

while(flag)

{

System.out.println(“------------向已经创建好的表中添加和删除指定的列簇或列--------------------”);

System.out.println(" 请输入您要进行的操作 1- 添加 2-删除 ");

Scanner scan = new Scanner(System.in);

String choose1=scan.nextLine();

switch (choose1) {

case “1”:

{

System.out.println(“请输入要添加的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要添加的表的行键”);

String rowKey=scan.nextLine();

System.out.println(“请输入要添加的表的列簇”);

String colFamily=scan.nextLine();

System.out.println(“请输入要添加的表的列名”);

String col=scan.nextLine();

System.out.println(“请输入要添加的值”);

String val=scan.nextLine();

try {

t.insertRow(tableName, rowKey, colFamily, col, val);

System.out.println(“插入成功:”);

t.getData(tableName);

} catch (IOException e) {

// TODO Auto-generated catch block

e.getMessage();

}

break;

}

case “2”:

{

System.out.println(“请输入要删除的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要删除的表的行键”);

String rowKey=scan.nextLine();

System.out.println(“请输入要删除的表的列簇”);

String colFamily=scan.nextLine();

System.out.println(“请输入要删除的表的列名”);

String col=scan.nextLine();

try {

System.out.println(“----------------------表的原本信息如下---------------------”);

t.getData(tableName);

System.out.println(“____________________________正在执行删除操作…\n”);

t.deleteRow(tableName, rowKey, colFamily, col);

System.out.println(“____________________________删除成功_______________\n”);

System.out.println(“---------------------删除后
表的信息如下---------------------”);

t.getData(tableName);

} catch (IOException e) {

// TODO Auto-generated catch block

e.getMessage();

}

break;

}

default:

{

System.out.println(" 你的操作有误 !!! ");

break;

}

}

System.out.println(" 你要继续操作吗? 是-true 否-false ");

flag=scan.nextBoolean();

}

System.out.println(" 程序已退出! ");

}

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7VD0OUPp-1651743333686)(media/image6.png)]{width="6.0in" height="2.645138888888889in"}

图5.添加或删除指定列族或列(java)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LRqvp5kd-1651743333687)(media/image7.png)]{width="3.8419991251093615in"
height="1.1000962379702537in"}

图6.添加或删除指定列族或列(java)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y5ItqBv7-1651743333687)(media/image8.png)]{width="5.050437445319335in"
height="1.9501695100612424in"}

图7.添加或删除指定列族或列(java)

(4)清空指定的表的所有记录数据。

a.Shell命令

truncate ‘student’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e8VlJl4p-1651743333688)(media/image9.png)]{width="6.0in" height="3.5868055555555554in"}

图8.清空指定表的所有记录

b.java代码

import java.io.IOException;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.List;

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.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.util.Bytes;

public class Test_4 {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

/**

* @param args

*/

//建立连接

public static void init(){

configuration = HBaseConfiguration.create();

configuration.set(“hbase.rootdir”,“hdfs://localhost:9000/hbase”);

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();

}

}

/**

* 清空制定的表的所有记录数据

* @param args

* @throws IOException

*/

public static void clearRows(String tableName) throws IOException{

init();

// HBaseAdmin admin1=new HBaseAdmin(configuration);

// HTableDescriptor tDescriptor
=admin1.getTableDescriptor(Bytes.toBytes(tableName));//读取了之前表的表名
列簇等信息,然后再进行删除操作。
总思想是先将原表结构保留下来,然后进行删除,再重新依据保存的信息重新创建表。

//备份表列族名

TableName tablename=TableName.valueOf(tableName);

HTableDescriptor tDescriptor = new

HTableDescriptor(TableName.valueOf(tableName));

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

List<String> cloFamily = new ArrayList<String>();

for(Result result:scanner)

{

Cell[] cells = result.rawCells();

for(Cell cell:cells){

// System.out.println(“column Family(列簇):”+new
String(CellUtil.cloneFamily(cell))+" ");

cloFamily.add(new String(CellUtil.cloneFamily(cell)));

}

}

//删除表

admin.disableTable(tablename);

admin.deleteTable(tablename);

//重新建表

// HTableDescriptor tDescriptor = new

// HTableDescriptor(TableName.valueOf(tableName));

// tDescriptor.addFamily(new HColumnDescriptor(“name”));

for(String cf : cloFamily)

{

tDescriptor.addFamily(new HColumnDescriptor(cf));

}

admin.createTable(tDescriptor);

close();

}

/**

* 根据表名查找表信息

*/

public static void getData(String tableName)throws IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

for(Result result:scanner)

{

showCell((result));

}

close();

}

/**

* 格式化输出

* @param result

*/

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(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");

System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");

System.out.println();

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Test_4 test_4=new Test_4();

Scanner scan = new Scanner(System.in);

System.out.println(“请输入要清空的表名”);

String tableName=scan.nextLine();

try {

System.out.println(“表原来的信息:”);

test_4.getData(tableName);

test_4.clearRows(tableName);

System.out.println(“表已清空:”);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8q1e7Svp-1651743333688)(media/image10.png)]{width="6.0in" height="3.2222222222222223in"}

图9.清空指定表的所有记录(java)

(5)统计表的行数

a.Shell命令

count ‘s1’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Gg45jv7s-1651743333689)(media/image11.png)]{width="6.0in" height="1.64375in"}

图10.统计表的行数

b.java代码

import java.io.IOException;

import java.util.Scanner;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.client.Table;

public class Test_5 {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

//建立连接

public static void init(){

configuration = HBaseConfiguration.create();

configuration.set(“hbase.rootdir”,“hdfs://localhost:9000/hbase”);

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 countRows (String tableName) throws IOException

{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner =table.getScanner(scan);

int num = 0;

for(Result result = scanner.next();result!=null;result=scanner.next())

{

num++;

}

System.out.println(“行数:”+num);

scanner.close();

close();

}

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

Test_5 test_5=new Test_5();

Scanner scan = new Scanner(System.in);

System.out.println(“请输入要统计行数的表名”);

String tableName=scan.nextLine();

test_5.countRows(tableName);

}

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6I7AVNRi-1651743333689)(media/image12.png)]{width="6.0in" height="3.21875in"}

图11.统计表的行数(java)

2.HBase数据库操作

(1)现有以下关系型数据库中的表和数据(见表A-1~表A-3),要求将其转换为适合于HBase存储的表并插人数据。

a.创建学生表

create ‘student’,‘S_No’,‘S_Name’,‘S_Sex’,‘S_Age’

插入数据:

插入shell命令

第一行数据 put ‘Student’,‘s001’,‘S_No’,‘2015001’ 
put ‘Student’,‘s001’,‘S_Name’,‘Zhangsan’ 
put ‘Student’,‘s001’,‘S_Sex’,‘male’ 
put ‘Student’,‘s001’,‘S_Age’,‘23’ 
第二行数据 put ‘Student’,‘s002’,‘S_No’,‘2015002’ 
put ‘Student’,‘s002’,‘S_Name’,‘Mary’ 
put ‘Student’,‘s002’,‘S_Sex’,‘female’ 
put ‘Student’,‘s002’,‘S_Age’,‘22’ 
第三行数据 put ‘Student’,‘s003’,‘S_No’,‘2015003’ 
put ‘Student’,‘s003’,‘S_Name’,‘Lisi’ 
put ‘Student’,‘s003’,‘S_Sex’,‘male’ 
put ‘Student’,‘s003’,‘S_Age’,‘24’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JjwRcJ9f-1651743333689)(media/image13.png)]{width="6.0in" height="3.525in"}

图12.创建学生表

b.创建选课表

create ‘Course’,‘C_No’,‘C_Name’,‘C_Credit’

创建Course表

±----------------------------------±----------------------------------+
| | 插入shell命令 |
第一行数据 put ‘Course’,‘c001’,‘C_No’,‘123001’
put ‘Course’,‘c001’,‘C_Name’,‘Math’
put ‘Course’,‘c001’,‘C_Credit’,‘2.0’

第二行数据 put ‘Course’,‘c002’,‘C_No’,‘123002’
put ‘Course’,‘c002’,‘C_Name’,‘Computer Science’
put ‘Course’,‘c002’,‘C_Credit’,‘5.0’

第三行数据 put ‘Course’,‘c003’,‘C_No’,‘123003’
put ‘Course’,‘c003’,‘C_Name’,‘English’
put ‘Course’,‘c003’,‘C_Credit’,‘3.0’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XFd8cs7k-1651743333690)(media/image14.png)]{width="6.0in" height="3.359332895888014in"}

图13.创建课程表

c.创建选课表

create ‘SC’,‘SC_Sno’,‘SC_Cno’,‘SC_Score’

插入数据:

±----------------------------------±----------------------------------+
| | 插入shell命令 |
+=+=+
第一行数据 put ‘SC’,‘sc001’,‘SC_Sno’,‘2015001’
put ‘SC’,‘sc001’,‘SC_Cno’,‘123001’
put ‘SC’,‘sc001’,‘SC_Score’,‘86’

第二行数据 put ‘SC’,‘sc002’,‘SC_Sno’,‘2015001’
put ‘SC’,‘sc002’,‘SC_Cno’,‘123003’
put ‘SC’,‘sc002’,‘SC_Score’,‘69’

第三行数据 put ‘SC’,‘sc003’,‘SC_Sno’,‘2015002’
put ‘SC’,‘sc003’,‘SC_Cno’,‘123002’
put ‘SC’,‘sc003’,‘SC_Score’,‘77’

第四行数据 put ‘SC’,‘sc004’,‘SC_Sno’,‘2015002’
put ‘SC’,‘sc004’,‘SC_Cno’,‘123003’
put ‘SC’,‘sc004’,‘SC_Score’,‘99’

第五行数据 put ‘SC’,‘sc005’,‘SC_Sno’,‘2015003’
put ‘SC’,‘sc005’,‘SC_Cno’,‘123001’
put ‘SC’,‘sc005’,‘SC_Score’,‘98’

第六行数据 put ‘SC’,‘sc006’,‘SC_Sno’,‘2015003’
put ‘SC’,‘sc006’,‘SC_Cno’,‘123002’
put ‘SC’,‘sc006’,‘SC_Score’,‘95’

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X2rUE1ha-1651743333690)(media/image15.png)]{width="6.0in" height="2.0in"}

图13.创建选课表

2.请编程实现以下功能

1.createTable(String tableName, String[] fields)

创建表,参数 tableName 为表的名称,字符串数组 fields 为存储记录各个字段名称的数组。要求当 HBase 已经存在名为 tableName
的表的时候,先删除原有的表,然后再创建新的表。

2.addRecord(String tableName, String row, String[] fields,
String[] values)

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

values 存储这三门课的成绩。

3.scanColumn(String tableName, String column)

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

4.modifyData(String tableName, String row, String column)

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

5.deleteRow(String tableName, String row)

删除表 tableName 中 row 指定的行的记录。

java代码:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

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.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.Admin;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.util.Bytes;

public class Test_Two {

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

//建立连接

public static void init(){

configuration = HBaseConfiguration.create();

configuration.set(“hbase.rootdir”,“hdfs://localhost:9000/hbase”);

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();

}

}

/**

*
建表。参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。

* 要求当HBase已经存在名为tableName的表时,先删除原有的表,然后再

* 创建新的表 field:列族

* @param myTableName 表名

* @param colFamily 列族名

* @throws IOException

*/

public static void createTable(String tableName,String[] fields)
throws IOException {

init();

TableName tablename = TableName.valueOf(tableName);

if(admin.tableExists(tablename)){

System.out.println(“表已存在,将执行删除原表,重建新表!”);

admin.disableTable(tablename);

admin.deleteTable(tablename);//删除原来的表

}

// HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

HTableDescriptor hTableDescriptor = new

HTableDescriptor(TableName.valueOf(tableName));

for(String str:fields){

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);

hTableDescriptor.addFamily(hColumnDescriptor);

}

admin.createTable(hTableDescriptor);

System.out.println(“表已创建成功”);

close();

}

/**

* 向表 tableName、行 row(用 S_Name 表示)和字符串数组 fields
指定的单元格中

* 添加对应的数据 values。

* 其中,fields 中每个元素如果对应的列族下还有相应的列限定符的话,

* 用"columnFamily:column"表示。

* 例如,同时向"Math"、“Computer Science”、"English"三列添加成绩时,

* 字符串数组 fields 为{“Score:Math”, “Score:Computer Science”,
“Score:English”},

* 数组values 存储这三门课的成绩。

*/

public static void addRecord(String tableName,String rowKey,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(rowKey.getBytes());

String [] cols = fields[i].split(“:”);

if(cols.length==1)

{

put.addColumn(cols[0].getBytes(), “”.getBytes(),
values[i].getBytes());//因为当输入的是单列族,split仅读出一个字符字符串,即cols仅有一个元素

}

else {

put.addColumn(cols[0].getBytes(), cols[1].getBytes(),
values[i].getBytes());

}

table.put(put);

}

table.close();

close();

}

/**

* 根据表名查找表信息

*/

public static void getData(String tableName)throws IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

for(Result result:scanner)

{

showCell((result));

}

close();

}

/**

* 格式化输出

* @param result

*/

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(“column Name(列名):”+new
String(CellUtil.cloneQualifier(cell))+" ");

System.out.println(“value:(值)”+new
String(CellUtil.cloneValue(cell))+" ");

System.out.println();

}

}

/**

* 浏览表 tableName 某一列的数据,如果某一行记录中该列数据不存在,则返回
null。

* 要求当参数 column
为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;

* 当参数 column
为某一列具体名称(例如"Score:Math")时,只需要列出该列的数据。

* @param tableName

* @param column

* @throws IOException

*/

public static void scanColumn (String tableName,String column) throws
IOException

{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

String [] cols = column.split(“:”);

if(cols.length==1)

{

scan.addFamily(Bytes.toBytes(column));

}

else {

scan.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]));

}

ResultScanner scanner = table.getScanner(scan);

for (Result result = scanner.next(); result !=null;result =
scanner.next()) {

showCell(result);

}

table.close();

close();

}

/**

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

* @throws IOException

*/

public static void modifyData(String tableName,String rowKey,String
column,String value) throws IOException

{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Put put = new Put(rowKey.getBytes());

String [] cols = column.split(“:”);

if(cols.length==1)

{

put.addColumn(column.getBytes(),“”.getBytes() ,
value.getBytes());//qualifier:列族下的列名

}

else {

put.addColumn(cols[0].getBytes(),cols[1].getBytes() ,
value.getBytes());//qualifier:列族下的列名

}

table.put(put);

table.close();

close();

}

/**

* 删除表 tableName 中 row 指定的行的记录。

* @throws IOException

*/

public static void deleteRow(String tableName,String rowKey) throws
IOException

{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(rowKey.getBytes());

table.delete(delete);

table.close();

close();

}

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

Test_Two test_Two = new Test_Two();

boolean flag =true;

while(flag)

{

System.out.println(“------------------------------------------------提供以下功能----------------------------------------------”);

System.out.println(" 1- createTable(创建表 ,提供表名、列族名) ");

System.out.println(" 2-addRecord (向已知表名、行键、列簇的表添加值)
");

System.out.println(" 3- ScanColumn(浏览表 某一列的数据) ");

System.out.println(" 4- modifyData(修改某表
某行,某一列,指定的单元格的数据) ");

System.out.println(" 5- deleteRow(删除 某表 某行的记录) ");

System.out.println(“------------------------------------------------------------------------------------------------------------------”);

Scanner scan = new Scanner(System.in);

String choose1=scan.nextLine();

switch (choose1) {

case “1”:

{

System.out.println(“请输入要创建的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要创建的表的列族个数”);

int Num=scan.nextInt();

String [] fields = new String[Num];

System.out.println(“请输入要创建的表的列族”);

/* Scanner scanner = new Scanner(System.in); scanner.next
如不是全局,即会记得上一次输出。相同地址读入值时*/

for(int i=0;i< fields.length;i++)

{

/*BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));

fields[i] = in.readLine();*/

/*fields[i]=scan.next(); 因为之前没有输入过,所以可以读入新值*/

scan = new Scanner(System.in);

fields[i]=scan.nextLine();

}

System.out.println(“正在执行创建表的操作”);

test_Two.createTable(tableName,fields);

break;

}

case “2”:

{

System.out.println(“请输入要添加数据的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要添加数据的表的行键”);

String rowKey=scan.nextLine();

System.out.println(“请输入要添加数据的表的列的个数”);

int num =scan.nextInt();

String fields[]=new String[num];

System.out.println(“请输入要添加数据的表的列信息 共”+num+“条信息”);

for(int i=0;i< fields.length;i++)

{

BufferedReader in3= new BufferedReader(new
InputStreamReader(System.in));

fields[i] = in3.readLine();

/*fields[i]=scan.next(); 因为之前没有输入过,所以可以读入新值*/

}

System.out.println(“请输入要添加的数据信息 共”+num+“条信息”);

String values[]=new String[num];

for(int i=0;i< values.length;i++)

{

BufferedReader in2 = new BufferedReader(new
InputStreamReader(System.in));

values[i] = in2.readLine();

}

System.out.println(“原表信息”);

test_Two.getData(tableName);

System.out.println(“正在执行向表中添加数据的操作…\n”);

test_Two.addRecord(tableName, rowKey, fields, values);

System.out.println(“\n添加后的表的信息…”);

test_Two.getData(tableName);

break;

}

case “3”:

{

System.out.println(“请输入要查看数据的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要查看数据的列名”);

String column=scan.nextLine();

System.out.println(“查看的信息如下:…\n”);

test_Two.scanColumn(tableName, column);

break;

}

case “4”:

{

System.out.println(“请输入要修改数据的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要修改数据的表的行键”);

String rowKey=scan.nextLine();

System.out.println(“请输入要修改数据的列名”);

String column=scan.nextLine();

System.out.println("请输入要修改的数据信息 ");

String value=scan.nextLine();

System.out.println(“原表信息如下:…\n”);

test_Two.getData(tableName);

System.out.println(“正在执行向表中修改数据的操作…\n”);

test_Two.modifyData(tableName, rowKey, column, value);

System.out.println(“\n修改后的信息如下:…\n”);

test_Two.getData(tableName);

break;

}

case “5”:

{

System.out.println(“请输入要删除指定行的表名”);

String tableName=scan.nextLine();

System.out.println(“请输入要删除指定行的行键”);

String rowKey=scan.nextLine();

System.out.println(“原表信息如下:…\n”);

test_Two.getData(tableName);

System.out.println(“正在执行向表中删除数据的操作…\n”);

test_Two.deleteRow(tableName, rowKey);

System.out.println(“\n删除后的信息如下:…\n”);

test_Two.getData(tableName);

break;

}

default:

{

System.out.println(" 你的操作有误 !!! ");

break;

}

}

System.out.println(" 你要继续操作吗? 是-true 否-false ");

flag=scan.nextBoolean();

}

System.out.println(" 程序已退出! ");

}

}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MmEoLEQi-1651743333691)(media/image16.png)]{width="6.0in" height="3.1590277777777778in"}

图14.创建表功能(java)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yOT52KJJ-1651743333692)(media/image17.png)]{width="5.633821084864392in"
height="2.8835837707786527in"}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5zmvZu6I-1651743333692)(media/image18.png)]{width="3.7836614173228345in"
height="1.7168153980752405in"}

图15.增加记录功能(java)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L9EVvn4b-1651743333693)(media/image19.png)]{width="6.0in" height="4.875694444444444in"}

图16.浏览表功能(java)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8MqVWL4l-1651743333693)(media/image20.png)]{width="5.0004330708661415in"
height="2.3252023184601924in"}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pe5TWc9v-1651743333694)(media/image21.png)]{width="4.47538823272091in"
height="1.8001563867016623in"}

图17.修改表功能(java)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OMRClwDa-1651743333695)(media/image22.png)]{width="6.0in" height="3.640277777777778in"}

图18.删除表功能(java)

3、出现的问题:

1、安装HBase2.2.2并测试HBase版本的时候遇到

错误: 找不到或无法加载主类
org.apache.hadoop.hbase.util.GetJavaProperty

![C:\Users\zsl\AppData\Roaming\Tencent\Users\1304355541\QQ\WinTemp\RichOle\}_6Q4Z2@}$9$F0_3(FL2})N.png](media/image23.png){width="7.85in"
height="3.675in"}

2、用java写清空数据表的时候遇到报错:

Table should have at least one column family. Set
hbase.table.sanity.checks to false at conf or table descriptor if you
want to bypass sanity checks

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bMbWSLdq-1651743333696)(media/image24.png)]{width="6.0in" height="0.8788888888888889in"}

3、按照教材写java代码时,遇到一个错误[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AGNy6BAN-1651743333697)(media/image25.png)]{width="6.0in"
height="0.4951388888888889in"}

Multiple markers at this line

- The type HTableDescriptor is deprecated

- The constructor HTableDescriptor(String) is

undefined

4、解决方案:

1、问题原因是:因为 Hbase 没有将其自身的依赖包添加到 classpath
配置路径所以才会导致找不到自身主类的报错。

查看了博客:https://blog.csdn.net/weixin_45702261/article/details/120587547?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_antiscanv2&utm_relevant_index=1

进入/usr/local/hbase/conf/hbase-env文件,将最后一行不允许注释,问题得到解决

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U8C3y00i-1651743333699)(media/image26.png)]{width="5.525479002624672in"
height="0.9167465004374453in"}

2、错误的原因是:创建Hbase数据表时至少要有一个列族名,而清空数据表的java代码其实是删除了整个数据表再重新建一个有着原表列族名的空表,此时要先将原表的列族名用String数组备份,再重新建表的时候依次重新添加列族名即可,代码如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SZJ6YvX4-1651743333701)(media/image27.png)]{width="6.0in" height="2.7819444444444446in"}

3、教材的Hbase版本较老,我使用的的是HBase2.2.2版本较新,那一句的语法API已经修改了,用HTableDescriptor
hTableDescriptor =new HTableDescriptor(TableName.valueOf(tableName));

代替HTableDescriptor hTableDescriptor = new
HTableDescriptor(tableName);即可。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐