0


熟悉HBase常用操作

1. 用Hadoop提供的HBase Shell命令完成以下任务

(1)列出HBase所有表的相关信息,如表名、创建时间等。

启动HBase:

cd /usr/local/hbase
bin/start-hbase.sh
bin/hbase shell

在这里插入图片描述

列出HBase所有表的信息:

hbase(main):001:0> list

在这里插入图片描述

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

查看记录数据:
scan 'student'
查看表的信息:
describe 'student'

在这里插入图片描述
在这里插入图片描述

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

添加列族或列:
alter 'student','NAME'=>'Sid'
删除列族或列:
alter 'student','NAME'=>'Sid',METHOD=>'delete'

在这里插入图片描述

在这里插入图片描述

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

create 'teacher','Tname','Tsex','Tage','Tdept','Tcourse'
禁用表 teacher
disable 'teacher'
删除表
drop 'teacher'

在这里插入图片描述

(5)统计表的行数。

count 'student'

在这里插入图片描述

2. 现有以下关系型数据库中的表和数据,要求将其转换为适合于 HBase 存储的表并插入数据:

表 A-1 学生表(Student)
学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)2015001Zhangsanmale232015002Maryfemale222015003Lisimale24
表 A-2 课程表(Course)
课程号(C_No)课程名(C_Name)学分(C_Credit)123001Math2.0123002Computer Science5.0123003English3.0
表 A-3 选课表(SC)
学号(SC_Sno)课程号(SC_Cno)成绩(SC_Score)201500112300186201500112300369201500212300277201500212300399201500312300198201500312300295
创建三张表:

create 'Student','S_No','S_Name','S_Sex','S_Age'
create 'Course','C_No','C_Name','C_Credit'
create 'SC','SC_Sno','SC_Cno','SC_Score'

在这里插入图片描述

插入数据:

put 'Student','1','S_No','2015001'
put 'Student','1','S_Name','Zhangsan'
put 'Student','1','S_Sex','male'
put 'Student','1','S_Age','23'
put 'Student','2','S_No','2015002'
put 'Student','2','S_Name','Mary'
put 'Student','2','S_Sex','female'
put 'Student','2','S_Age','22'
put 'Student','3','S_No','2015003'
put 'Student','3','S_Name','Lisi'
put 'Student','3','S_Sex','male'
put 'Student','3','S_Age','24'

在这里插入图片描述

put 'Course','1','C_No','123001'
put 'Course','1','C_Name','Math'
put 'Course','1','C_Credit','2.0'
put 'Course','2','C_No','123002'
put 'Course','2','C_Name','Computer Science'
put 'Course','2','C_Credit','5.0'
put 'Course','3','C_No','123003'
put 'Course','3','C_Name','English'
put 'Course','3','C_Credit','3.0'

在这里插入图片描述

put 'SC','1','SC_Sno','2015001'
put 'SC','1','SC_Cno','123001'
put 'SC','1','SC_Score','86'
put 'SC','2','SC_Sno','2015001'
put 'SC','2','SC_Cno','12303'
put 'SC','2','SC_Score','69'
put 'SC','3','SC_Sno','2015002'
put 'SC','3','SC_Cno','123002'
put 'SC','3','SC_Score','77'
put 'SC','4','SC_Sno','2015002'
put 'SC','4','SC_Cno','123003'
put 'SC','4','SC_Score','99'
put 'SC','5','SC_Sno','2015003'
put 'SC','5','SC_Cno','123001'
put 'SC','5','SC_Score','98'
put 'SC','6','SC_Sno','2015003'
put 'SC','6','SC_Cno','123002'
put 'SC','6','SC_Score','95'

在这里插入图片描述

  • 请编程实现以下功能:1. createTable(String tableName, String[] fields)创建表,参数 tableName 为表的名称,字符串数组 fields 为存储记录各个字段名称的数组。要求当 HBase 已经存在名为 tableName 的表的时候,先删除原有的表,然后再创建新的表。打开eclipsecd /usr/localcd eclipsels./eclipse``````packageops;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.ColumnFamilyDescriptor;importorg.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.TableDescriptorBuilder;importorg.apache.hadoop.hbase.util.Bytes;publicclassCreateTable{ publicstaticConfiguration configuration;publicstaticConnection connection;publicstaticAdmin admin;//建立连接publicstaticvoidinit(){ configuration =HBaseConfiguration.create();configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");try{ connection =ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();}catch(IOException e){ e.printStackTrace();}}//关闭连接publicstaticvoidclose(){ try{ if(admin !=null){ admin.close();}if(connection !=null){ connection.close();}}catch(IOException e){ e.printStackTrace();}}publicstaticvoidcreateTable(String tableName,String[] fields)throwsIOException{ init();TableName tablename =TableName.valueOf(tableName);//tablename:tableName// If tableName exists,then disable that,then delete that.if(admin.tableExists(tablename)){ System.out.println("table is exists!");admin.disableTable(tablename);admin.deleteTable(tablename);//before delete table,must disabletable}TableDescriptorBuilder tableDescriptor =TableDescriptorBuilder.newBuilder(tablename);for(int i=0;i<fields.length;i++){ //每个字段被转换为列族描述符,并添加到表描述符中ColumnFamilyDescriptor family =ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(fields[i])).build(

本文转载自: https://blog.csdn.net/whdehcy/article/details/135437716
版权归原作者 cwn_ 所有, 如有侵权,请联系我们删除。

“熟悉HBase常用操作”的评论:

还没有评论