大数据第七次作业

Hbase操作与编程使用

使用HBase Shell命令 或 使用HBase Java API完成

1. 任务:

点击展开代码列表
hbase> status  # 查看运行状态
hbase> create 'user','info'    # 创建表
hbase> list    # 查看所有表
hbase> describe 'user'     # 描述表详情
hbase> desc 'user'         # 描述表详情
hbase> exists  'user'      # 判断表是否存在
hbase> is_disabled  'user'     # 禁用表
hbase> is_enabled  'user'      # 启用表
hbase> put 'user','row1','info:name','xiaohong'    # 插入数据,增加列
hbase> scan 'user'     # 查询所有记录
hbase> put 'user','row2','info:age','20'   # 增加列,稀疏
hbase> scan 'user'
hbase> put 'user','row3','info:sex','female'
hbase> scan 'user' ,{LIMIT => 2}
hbase> get 'user','row2'
hbase> get 'user','row3','info:sex'
hbase> count 'user'
hbase> put 'user','row1','info:age','19'
hbase> put 'user','row2','info:age','19'
hbase> disable 'user'
hbase> alter 'user', NAME=>'info', VERSIONS=>3     # 配置版本数
hbase> enable 'user'
hbase> put 'user','row2','info:age','22'
hbase> scan 'user', {VERSIONS=>3}
hbase> alter 'user', {NAME => 'score', VERSIONS => 100}    # 添加和删除列族
hbase> alter 'user', {NAME => 'tel', VERSIONS => 8}
hbase> scan 'user',{VERSIONS=>3}
hbase> desc 'user'
hbase> alter 'user', {NAME => 'tel', METHOD => 'delete'}
hbase> delete 'user','row1','info:age'
hbase> delete 'user','row2'
hbase> truncate 'user'
hbase> disable 'user'
hbase> describe 'user'
hbase> enable 'user'
hbase> disable 'user'
hbase> drop 'user'
hbase> list

1.1. 列出HBase所有的表的相关信息,例如表名;

hbase> list

1.2. 在终端打印出指定的表的所有记录数据;

hbase> scan 'user'

1.3. 向已经创建好的表添加和删除指定的列族或列;

hbase> alter 'user', {NAME => 'score', VERSIONS => 100} 
hbase> alter 'user', {NAME => 'tel', VERSIONS => 8}
hbase> scan 'user',{VERSIONS=>3}
hbase> desc 'user'
hbase> alter 'user', {NAME => 'tel', METHOD => 'delete'}

1.4. 清空指定的表的所有记录数据;

hbase> truncate 'user'

1.5. 统计表的行数。

hbase> count 'user'

2. 关系型数据库中的表和数据(教材P92上),要求将其转换为适合于HBase存储的表并插入数据。

2.1. 学生表(Student)

hbase> create 'Student','S_No','S_Name','S_Sex','S_Age'
hbase> put 'Student','s001','S_No','2015001'
hbase> put 'Student','s001','S_Name','Zhangsan'
hbase> put 'Student','s001','S_Sex','male'
hbase> put 'Student','s001','S_Age','23'
hbase> put 'Student','s002','S_No','2015002'
hbase> put 'Student','s002','S_Name','Mary'
hbase> put 'Student','s002','S_Sex','female'
hbase> put 'Student','s002','S_Age','22'
hbase> put 'Student','s003','S_No','2015003'
hbase> put 'Student','s003','S_Name','Lisi'
hbase> put 'Student','s003','S_Sex','male'
hbase> put 'Student','s003','S_Age','24'

2.2. 课程表(Course)

hbase> create 'Course','C_No','C_Name','C_Credit'
hbase> put 'Course','c001','C_No','123001'
hbase> put 'Course','c001','C_Name','Math'
hbase> put 'Course','c001','C_Credit','2.0'
hbase> put 'Course','c002','C_No','123002'
hbase> put 'Course','c002','C_Name','Computer'
hbase> put 'Course','c002','C_Credit','5.0'
hbase> put 'Course','c003','C_No','123003'
hbase> put 'Course','c003','C_Name','English'
hbase> put 'Course','c003','C_Credit','3.0'

2.3. 选课表(SC)

hbase> create 'SC','SC_Sno','SC_Cno','SC_Score'
hbase> put 'SC','sc001','SC_Sno','2015001'
hbase> put 'SC','sc001','SC_Cno','123001'
hbase> put 'SC','sc001','SC_Score','86'
hbase> put 'SC','sc002','SC_Sno','2015001'
hbase> put 'SC','sc002','SC_Cno','123003'
hbase> put 'SC','sc002','SC_Score','69'
hbase> put 'SC','sc003','SC_Sno','2015002'
hbase> put 'SC','sc003','SC_Cno','123002'
hbase> put 'SC','sc003','SC_Score','77'
hbase> put 'SC','sc004','SC_Sno','2015002'
hbase> put 'SC','sc004','SC_Cno','123003'
hbase> put 'SC','sc004','SC_Score','99'
hbase> put 'SC','sc005','SC_Sno','2015003'
hbase> put 'SC','sc005','SC_Cno','123001'
hbase> put 'SC','sc005','SC_Score','98'
hbase> put 'SC','sc006','SC_Sno','2015003'
hbase> put 'SC','sc006','SC_Cno','123002'
hbase> put 'SC','sc006','SC_Score','95'

3. 编程完成以下指定功能(教材P92下):

3.1. createTable(String tableName, String[] fields)创建表。

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

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

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

3.3. scanColumn(String tableName, String column)

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(Bytes.toString(cell.getRowArray(),cell.getRowOffset(), cell.getRowLength()))+" ");
            System.out.println("Timetamp:"+cell.getTimestamp()+" ");
            System.out.println("column Family:"+new String(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getFamilyLength()))+" ");
            System.out.println("row Name:"+new String(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(), cell.getQualifierLength()))+" ");
            System.out.println("value:"+new String(Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength()))+" ");           
        }
    }

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

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());
    put.addColumn(column.getBytes(),null,val.getBytes());
    table.put(put);
    table.close();
    close();
}

3.5. deleteRow(String tableName, String row)

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();
    }
posted @ 2020-11-22 17:59  1After909  阅读(384)  评论(0编辑  收藏  举报