熟悉HBase常用操作

2024-01-07 21:27:09

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)
2015001 Zhangsan male 23
2015002 Mary female 22
2015003 Lisi male 24

表 A-2 课程表(Course)

课程号(C_No) 课程名(C_Name) 学分(C_Credit)
123001 Math 2.0
123002 Computer Science 5.0
123003 English 3.0

表 A-3 选课表(SC)

学号(SC_Sno) 课程号(SC_Cno) 成绩(SC_Score)
2015001 123001 86
2015001 123003 69
2015002 123002 77
2015002 123003 99
2015003 123001 98
2015003 123002 95

创建三张表:

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 的表的时候,先删除原有的表,然后再创建新的表。

    打开eclipse
    cd /usr/local
    cd eclipse
    ls
    ./eclipse
    
    package ops;
    import java.io.IOException;
    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.ColumnFamilyDescriptor;
    import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
    import org.apache.hadoop.hbase.util.Bytes;
    public class CreateTable {
         
    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(connection != null){
         
    connection.close();
    }
    }catch(IOException e){
         
    e.printStackTrace();
    }
    }
    public static void createTable(String tableName,String[] fields) throws
    IOException{
         
    init();
    TableName tablename = TableName.valueOf(tableName);//table
    name: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 disable
    table
    }
    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
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。