栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实验三熟悉常用的HBase操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实验三熟悉常用的HBase操作

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.zookeeper.Op;

import java.io.IOException;
public class test {
    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();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void createTable(String myTablename, String[] fields) throws  IOException{
        TableName tableName= TableName.valueOf(myTablename);
        if(admin.tableExists(tableName)) {
            System.out.println("table exist");
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
        TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
        for(String str:fields){
            ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
            tableDescriptor.setColumnFamily(family);
        }
        admin.createTable(tableDescriptor.build());
        System.out.println("table created");

    }
    public static void main(String[] args)throws IOException{
        init();
        createTable("student", new String[]{"score"});
        close();
        System.out.println(admin.listTableDescriptors().toString());
    }
}
一、实验目的
  • 理解Hbase在Hadoop体系结构中的角色;
  • 熟练使用Hbase操作常用的Shell命令;
  • 熟悉Hbase操作常用的Java API。
二、实验平台
  • 操作系统:Linux(建议CentOS);
  • Hadoop版本:2.6.1;
  • JDK版本:1.7或以上版本;
  • Java IDE:IDEA。
实验内容 (1)编程实现以下指定功能,并用Hadoop提供的Hbase Shell命令完成相同任务: ①列出Hbase所有的表的相关信息,例如表名;
 
②在终端打印出指定的表的所有记录数据; 
③向已经创建好的表添加和删除指定的列族或列; 
④清空指定的表的所有记录数据; 
⑤统计表的行数。 
(2)现有以下关系型数据库中的表和数据,要求将其转换为适合于Hbase存储的表并插入数据。 

学生表(Student)

学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)
2015001Zhangsanmale23
2015002Maryfemale22
2015003Lisimale24

课程表(Course)

课程号(C_No)课程名(C_Name)学分(C_Credit)
123001Math2.0
123002Computer Science5.0
123003English3.0

选课表(SC)

学号(SC_Sno)课程号(SC_Cno)学分(C_Credit)
201500112300186
201500112300369
201500212300277
201500212300399
201500212300277
201500312300198
201500312300295

同时,请编程实现以下功能:

①createTable(String tableName, String[] fields)

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

②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存储这三门课的成绩。

③scanColumn(String tableName, String column)

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

④modifyData(String tableName, String row, String column)

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

⑤deleteRow(String tableName, String row)

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/327575.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号