大数据hadoop学习【10】-----根据关系型数据库表,分别通过HBase Shell命令和JAVA编程设计并创建适宜的HBase数据表目录
每一种数据库的学习,都要学会怎么样对关系数据库表进行联系创建,当然,Hbase数据库也一样。但是Hbase数据库也有不同的地方,那就是HBase数据库是建立在列的基础上建立的关系,其他关系型数据库基本都是建立在行上面建立的关系,这就是不同的地方,加入给大家几张有关系的表,让我们建立起他们的联系,这时候就需要我们对表进行规划了,然后创建出他们的关系,本次博客,林君学长主要带大家了解如何通过Hbase Shell命令,进行关系数据库表的创建
- 操作系统:ubuntuKylin-16.04
- hadoop版本:hadoop-2.7.7
- hbase版本:hbase-1.4.13
一、Hbase关系数据库表
题目:根据以下关系型数据库表,使用HBase Shell设计并创建适宜的HBase数据表
1、学生信息表
学号(S_No) | 姓名(S_Name) | 性别(S_Sex) | 年龄(S_Age) |
---|---|---|---|
2015001 | ZhangSan | male | 23 |
2015002 | Mary | female | 22 |
2015003 | LiSi | male | 24 |
2、课程信息表
课程号(C_No) | 课程名(C_Name) | 学分(C_Credit) |
---|---|---|
123001 | Math | 2.0 |
123002 | Computer Science | 5.0 |
123003 | English | 3.0 |
3、选课信息表
学号(SC_Sno) | 课程号(SC_Cno) | 成绩(SC_Score) |
---|---|---|
2015001 | 123001 | 86 |
2015001 | 123003 | 69 |
2015002 | 123002 | 77 |
2015002 | 123003 | 99 |
2015003 | 123001 | 98 |
2015003 | 123002 | 95 |
二、对Hbase关系数据库表进行分析
对于上面的学生信息以及学生选课信息,关系数据库表已经给大家给出,那么我们如何进行关系模拟来将学生和课程产生关系,使得我们在查询学生的时候,能够自动的也他选择的课程也一并显示出现,这就需要我们构建!在很多时候,不是一看到表就直接进行创建,然后进行联系,Hbase关系型数据库和其他数据库不一样,所以需要大家进行关系表的规划,自由明白他们之间的关系,我们才能创建出合适这个关系的表格!
1、规划思路
1)、上面我们可以看出,学生的选课直接和学生信息和课程信息有关,而直接相关的就是学生学号和课程号,第三章选课表也就是根据学生信息和课程信息的综合而来,学号和课程号是直接的关系纽带!
2、规划结果
1)、通过以上关系,那么我们可以进行综合,直接将学生信息和他选择的课程信息进行综合,进而联系在一起,形成下面的综合关系表;将学生信息表、课程信息表、选课信息表综合在一起,然后进行建表的规划:
3、结果分析
1)、通过上面的表格规划,那么我们就可以来进行建表,建表的思路就和上面的表格思路一样,我们需要建立的表格就是学生选课信息表,从中不仅可以查询学生信息,更可以查询课程信息,以及每个学生选课的成绩!
2)、所有的关系整个为一个学生信息资源表,studentInformation,该表中包含三个列族名:学习信息、数学选课信息、计算机选课信息、英选课信息,而每个列族名下面包括几个子列名,例如:
学生信息的子列是:学生学号、学生姓名、性别、年龄
选课信息的子列是:课程号、课程名、学分、成绩
由此,我们便可以通过以上关系进行建表了!
三、通过Shell命令创建Hbase关系数据表
1、终端运行hbase数据库,并进入hbase的shell界面
1)、运行SSH
ssh localhost
2)、运行hadoop
start-dfs.sh
3)、运行hbase
start-hbase.sh
4)、查看是否成功启动
jps
出现以上7个节点,则表明hbase成功启动!前提是hbase和hadoop是进行的伪分布配置哦!
2、创建综合关系表
1)、创建综合关系表
create 'studentInformation','Student','Math','ComputerScience','English'
上图可以看到,综合关系表我们已经创建成功,接下来,我们需要进行的就是写入相关数据啦!
3、添加学生信息
1)、添加学号为2015001的学生信息,学号作为行键
put 'studentInformation','2015001','Student:S_No','2015001'
put 'studentInformation','2015001','Student:S_Name','ZhangSan'
put 'studentInformation','2015001','Student:S_Sex','male'
put 'studentInformation','2015001','Student:S_Age','23'
2)、添加学号为2015002的学生信息,学号作为行键
put 'studentInformation','2015002','Student:S_No','2015002'
put 'studentInformation','2015002','Student:S_Name','Mary'
put 'studentInformation','2015002','Student:S_Sex','female'
put 'studentInformation','2015002','Student:S_Age','22'
3)、添加学号为2015003的学生信息,学号作为行键
put 'studentInformation','2015003','Student:S_No','2015003'
put 'studentInformation','2015003','Student:S_Name','LiSi'
put 'studentInformation','2015003','Student:S_Sex','male'
put 'studentInformation','2015003','Student:S_Age','24'
4、添加学生选择课程的基本信息
1)、添加学号为:2015001学生的选课信息
由上面总结的综合关系表可以看出,学号为2015001的同学选择了数学和英语课程,因此,我们需要添加的也就是这两门课程的信息
①.添加该同学选择的数学课程的基本信息
put 'studentInformation','2015001','Math:C_No','123001'
put 'studentInformation','2015001','Math:C_Name','Math'
put 'studentInformation','2015001','Math:C_Credit','2.0'
②.添加该同学选择的英语课程的基本信息
put 'studentInformation','2015001','English:C_No','123002'
put 'studentInformation','2015001','English:C_Name','English'
put 'studentInformation','2015001','English:C_Credit','3.0'
2)、添加学号为:2015002学生的选课信息
由上面总结的综合关系表可以看出,学号为2015002的同学选择了英语和计算机科学课程,因此,我们需要添加的也就是这两门课程的信息
①.添加该同学选择的英语课程的基本信息
put 'studentInformation','2015002','English:C_No','123002'
put 'studentInformation','2015002','English:C_Name','English'
put 'studentInformation','2015002','English:C_Credit','3.0'
②.添加该同学选择的计算机科学课程的基本信息
put 'studentInformation','2015002','ComputerScience:C_No','123003'
put 'studentInformation','2015002','ComputerScience:C_Name','ComputerScience'
put 'studentInformation','2015002','ComputerScience:C_Credit','5.0'
3)、添加学号为:2015003学生的选课信息
由上面总结的综合关系表可以看出,学号为2015003的同学选择了数学和计算机科学课程,因此,我们需要添加的也就是这两门课程的信息
①.添加该同学选择的数学课程的基本信息
put 'studentInformation','2015003','Math:C_No','123001'
put 'studentInformation','2015003','Math:C_Name','Math'
put 'studentInformation','2015003','Math:C_Credit','2.0'
②.添加该同学选择的计算机科学课程的基本信息
put 'studentInformation','2015003','ComputerScience:C_No','123003'
put 'studentInformation','2015003','ComputerScience:C_Name','ComputerScience'
put 'studentInformation','2015003','ComputerScience:C_Credit','5.0'
现在我们学生所选择课程的基本信息我们已经添加完成,接下来当然是添加的学生所选择课程的成绩啦!
5、添加学生所选课程的成绩
1)、添加学号为:2015001学生所选课的成绩
学号为2015001的同学选择了数学和英语课程,所以他只有这两门课程会有成绩
put 'studentInformation','2015001','Math:SC_Score','86'
put 'studentInformation','2015001','English:SC_Score','69'
2)、添加学号为:2015002学生所选课的成绩
学号为2015002的同学选择了英语和计算机科学课程,所以他只有这两门课程会有成绩
put 'studentInformation','2015002','English:SC_Score','77'
put 'studentInformation','2015002','ComputerScience:SC_Score','99'
3)、添加学号为:2015003学生所选课的成绩
学号为2015003的同学选择了数学和计算机科学课程,所以他只有这两门课程会有成绩
put 'studentInformation','2015003','Math:SC_Score','98'
put 'studentInformation','2015003','ComputerScience:SC_Score','95'
6、学生选课信息查询
1)、查询学号为:2015001同学的学生信息及选课信息和所得的成绩
get 'studentInformation','2015001'
从查询的结果可以看出该同学的基本信息,和选择了两门课程:英语、数学;分别的成绩为:69、86
2)、查询学号为:2015002同学的学生信息及选课信息和所得的成绩
get 'studentInformation','2015002'
从查询的结果可以看出该同学的基本信息,和选择了两门课程:英语、计算机科学;分别的成绩为:77、99
3)、查询学号为:2015003同学的学生信息及选课信息和所得的成绩
get 'studentInformation','2015003'
从查询的结果可以看出该同学的基本信息,和选择了两门课程:数学、计算机科学;分别的成绩为:98、95!
7、关系Hbase数据库,结束该实验
exit
stop-hbase.sh
stop-dfs.sh
exit
由上面的添加数据的操作过程,我们可以知道,通过hbase的shell命令进行数据库表中数据的添加是一个非常痛苦的过程、因为只能一列一列的添加,而且还只能添加一列中一个数据,也就是以单元格为单位,一个一个单元格的添加! 啊,无情!
四、通过JAVA编程,实现学生选课信息表的创建及相关操作
上面我们也可以知道,通过HBase shell命令进行数据的数据不仅麻烦、而且费时,因此,在现实生活中一般是通过JAVA编程语言来实现对表的创建以及表中数据的操作,接下来,林君学长就带大家了解如何通过java语言实现对刚刚创建的表中数据的操作吧!
1、创建对Hbase数据库的实现功能类和菜单类
1)、这里就不给大家介绍怎么创建了,直接看林君学长的截图吧!
创建比较简单,可以根据上面的项目目录进行创建哦
2)、连接和关闭Hbase数据库的JAVA代码,在功能类里面哦!
//建立连接
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();
}
}
JAVA程序命令就是通过以上建立连接和关闭连接来进行访问的哦,这是开启JAVA访问的第一步,也是必须的一步,每一步操作命令都必须先与Hbase数据库建立连接,然后再关闭连接
2、编写创建学生信息表的方法
1)、要求:createTable(String tableName, String[] fields)
创建表,参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
2)、java程序代码:
public void createTable(String myTableName,String[] colFamily) throws IOException {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("该表已经存在!");
System.out.println("是否删除该表重新创建?");
System.out.println("1、是");
System.out.println("2、否");
System.out.print("请输入你的选择:");
int a=in.nextInt();
switch(a){
case 1:if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表:"+tableName+"已成功删除");
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("表创建成功!");break;
case 2:break;
}
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("表创建成功!");
}
close();
}
3)、运行截图:
在菜单函数中调用功能类中的上面的这个方法,进行学生Zhangsan选课信息表的创建:
createTable("StudentScore",new String[]{"student","Math","ComputerScience","English"})
再次创建,提示该表已经存在:
3、编写添加学生选课信息的方法
1)、要求:addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row(用S_Name表示)和字符串数组files指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“ComputerScience”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”,”Score:ComputerScience”,”Score:English”},数组values存储这三门课的成绩
2)、java程序代码:
public 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);
}
System.out.println("数据插入成功");
table.close();
close();
}
3)、运行截图:
在菜单函数中调用功能类中的上面的这个方法,进行学生Zhangsan选课信息的录入:
addRecord("StudentScore","Zhangsan",new String[]{"student:S_No","student:S_Name","student:S_Sex","student:S_Age","Math:C_No","Math:C_Name","Math:C_Credit","Math:SC_Score","English:C_No","English:C_Name","English:C_Credit","English:SC_Score"}, new String[]{"2015001","Zhangsan","male","23","123001","Math","2.0","86","123003","English","3.0","69"})
查看是否插入成功:
可以看出,张三的个人信息和选课信息已经插入成功!
4、编写浏览学生选课信息表的方法
1)、要求:scanColumn(String tableName, String column)
浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
2)、java程序代码:
public 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();
}
3)、运行截图:
在菜单函数中调用功能类中的上面的这个方法,进行学生Zhangsan数学课程信息的查询:
scanColumn("StudentScore", "Math")
可以看出,张三选择数学课程的所有信息已经全部列出来了哦!
5、编写修改学生选课信息的方法
1)、要求:modifyData(String tableName, String row, String column)
修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
2)、java程序代码:
public 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:列族下的列名
}
System.out.println("信息修改成功!");
table.put(put);
table.close();
close();
}
public 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))+" ");
}
}
3)、运行截图:
在菜单函数中调用功能类中的上面的这个方法,进行学生Zhangsan数学成绩的修改:
modifyData("StudentScore","Zhangsan","Math:SC_Score","89")
查看是否修改成功:
可以看出,已经修改成功,之前是86分,现在是89分哦!
6、编写删除学生选课信息的方法
1)、要求:deleteRow(String tableName, String row)
删除表tableName中row指定的行的记录。
2)、java程序代码:
public 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);
System.out.println("删除成功!");
table.close();
close();
}
3)、运行截图:
在菜单函数中调用功能类中的上面的这个方法,进行学生Zhangsan一行的信息的删除:
deleteRow("StudentScore","Zhangsan")
查看是否删除成功:
同学张三的信息已经被全部删除了,由于只有一条信息,所以删除之后就没有信息啦!小伙伴们可以将上面三个同学的数据都添加进去哦!
7、菜单函数
1)、java程序代码:
import java.io.IOException;
import java.util.Scanner;
import studentHbaseForJava.StudentForJava;
public class Menu1 {
public static void main(String[] args) throws IOException{
StudentForJava h1=new StudentForJava();
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
while(true){
System.out.println("**********************基于JAVA的Hbase数据库表及表的基本操作**********************");
System.out.println("1、创建学生选课表");
System.out.println("2、向创建的表中添加学生信息");
System.out.println("3、浏览创建的选课表中指定列的数据");
System.out.println("4、修改指定单元格的数据");
System.out.println("5、删除指定行的数据");
System.out.println("0、退出!");
System.out.print("请输入你的选择:");
int a=in.nextInt();
switch(a){
case 1: h1.createTable("StudentScore",new String[]{"student","Math","ComputerScience","English"});break;
case 2: h1.addRecord("StudentScore","Zhangsan",new String[]{"student:S_No","student:S_Name","student:S_Sex","student:S_Age","Math:C_No","Math:C_Name","Math:C_Credit","Math:SC_Score","English:C_No","English:C_Name","English:C_Credit","English:SC_Score"}, new String[]{"2015001","Zhangsan","male","23","123001","Math","2.0","86","123003","English","3.0","69"});break;
case 3: h1.scanColumn("StudentScore", "Math");break;
case 4: h1.modifyData("StudentScore","Zhangsan","Math:SC_Score","89");break;
case 5: h1.deleteRow("StudentScore","Zhangsan");break;
case 0: return;
}
}
}
}
2)、运行截图:
通过以上的关系数据库的分析步骤,林君学长就将Hbase关系数据库的分析分享给大家了,希望大家可以花时间下去消化一下,假如不是学生与课程的关系数据库变为教师与授课的关系数据库表又应该怎么分析;假如关系表不只有这个三个,而是有更过的关系表,我们又应该怎么进行分析,这都可以帮助自己加强自己的思维哦!
对以上分析有不懂的小伙伴,记得评论区留言,林君学长看到会为大家解答的,这个学长不太冷!
陈一月的又一天编程岁月^ _ ^