NoSQL实验三 HBase编程实践

10 篇文章 0 订阅
1 篇文章 0 订阅

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

list

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

scan ‘student’

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

put ‘student’,’2018’,’Sname’,’boss’    

delete ‘student’,’2018’,’Sname’

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

truncate ‘student’  

5.统计表的行数。

count ‘student’

(二)HBase数据库操作

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

学生表(Student)

学号(S_No)

姓名(S_Name)

性别(S_Sex)

年龄(S_Age)

2015001

Zhangsan

male

23

2015003

Mary

female

22

2015003

Lisi

male

24

创建Student表 如图1.1

Create ‘Student’,’S_No’,’S_Name’,’S_Sex’,’S_Age’

 

图1.1

添加属性如图1.2

put 'Student','s01','S_No','2015001'

put 'Student','s01','S_Name','Zhangsan'

put 'Student','s01','S_Sex','male'

put 'Student','s01','S_Age','23'

 

图1.2

添加属性如图1.3

put 'Student','s02','S_No','2015002'

put 'Student','s02','S_Name','Mary'

put 'Student','s02','S_Sex','female'

put 'Student','s02','S_Age','22'

 

图1.3

添加属性如图1.4

put 'Student','s03','S_No','2015003'

put 'Student','s03','S_Name','Lisi'

put 'Student','s03','S_Sex','male'

put 'Student','s03','S_Age','24'

 

图1.4

 

 

课程表(Course)

课程号(C_No)

课程名(C_Name)

学分(C_Credit)

123001

Math

2.0

123002

Computer Science

5.0

123003

English

3.0

 

创建Course表如图2.1

create 'Course','C_No','C_Name','C_Credit'

 

图2.1

 

put 'Course','c01','C_No','123001'

put 'Course','c01','C_Name','Math'

put 'Course','c01','C_Credit','2.0'

put 'Course','c02','C_No','123002'

put 'Course','c02','C_Name','Computer Science'

put 'Course','c02','C_Credit','5.0'

put 'Course','c03','C_No','123003'

put 'Course','c03','C_Name','English'

put 'Course','c03','C_Credit','3.0'

 

图2.2

 

选课表(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 'SC','SC_Sno','SC_Cno','SC_Score'

 

图3.1

put 'SC','sc01','SC_Sno','2015001'

put 'SC','sc01','SC_Cno','123001'

put 'SC','sc01','SC_Score','86'

put 'SC','sc02','SC_Sno','2015001'

put 'SC','sc02','SC_Cno','123003'

put 'SC','sc02','SC_Score','69'

put 'SC','sc03','SC_Sno','2015002'

put 'SC','sc03','SC_Cno','123002'

put 'SC','sc03','SC_Score','77'

put 'SC','sc04','SC_Sno','2015002'

put 'SC','sc04','SC_Cno','123003'

put 'SC','sc04','SC_Score','99'

put 'SC','sc05','SC_Sno','2015003'

put 'SC','sc05','SC_Cno','123001'

put 'SC','sc05','SC_Score','98'

put 'SC','sc06','SC_Sno','2015003'

put 'SC','sc06','SC_Cno','123002'

put 'SC','sc06','SC_Score','95'

 

2. 请编程实现以下功能:

创建一个java项目图

 

图4.1

 

图4.2

代码

  1. //建立连接  
  2.     public static void init(){  
  3.         configuration  = HBaseConfiguration.create();  
  4.         configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");  
  5.         try{  
  6.             connection = ConnectionFactory.createConnection(configuration);  
  7.             admin = connection.getAdmin();  
  8.         }catch (IOException e){  
  9.             e.printStackTrace();  
  10.         }  
  11.     }  
  12.     //关闭连接  
  13.     public static void close(){  
  14.         try{  
  15.             if(admin != null){  
  16.                 admin.close();  
  17.             }  
  18.             if(null != connection){  
  19.                 connection.close();  
  20.             }  
  21.         }catch (IOException e){  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  

(1)createTable(String tableName, String[] fields)

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

  1. //建表  
  2.     public static void createTable(String tableName, String[] fields) throws IOException {  
  3.         init(); //建立与hbase的链接  
  4.         TableName tablename = TableName.valueOf(tableName);    
  5.         if (admin.tableExists(tablename)) { //如果表已经存在  
  6.             System.out.println("这个"+tablename+"表已经存在咯!");  //打印表存在  
  7.             admin.disableTable(tablename);  //禁用表  
  8.             admin.deleteTable(tablename);  //删除表  
  9.         }  
  10.         HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);  
  11.         for (String str : fields) { //将存在fields的多个属性值去除添加进表  
  12.             HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);  
  13.             hTableDescriptor.addFamily(hColumnDescriptor);  
  14.         }  
  15.         admin.createTable(hTableDescriptor);  
  16.         System.out.println("创建了"+tablename+"");   
  17.         close();  
  18.     }  
  19.     //删表  
  20.     public static void deleteTable(String tableName) throws IOException {  
  21.         init();  
  22.         TableName tn = TableName.valueOf(tableName);  
  23.         if (admin.tableExists(tn)) {  
  24.             admin.disableTable(tn);  
  25.             admin.deleteTable(tn);  
  26.         }  
  27.         close();  
  28.     }   

 

 

 

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

  1. public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {  
  2.         init();  
  3.         Table table = connection.getTable(TableName.valueOf(tableName));  
  4.         for (int i = 0; i != fields.length; i++) {  
  5.             Put put = new Put(row.getBytes());  
  6.             String[] cols = fields[i].split(":");  
  7.             put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());  
  8.             table.put(put);  
  9.         }  
  10.         table.close();  
  11.         System.out.println("这个"+row+"列放入了列限定和值!");  //打印列删除  
  12.         close();  
  13.     }  

Java代码运行界面

 

Hbase Shell里检查school发现插入了这些值

 

      (3)scanColumn(String tableName, String column)

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

  1. public static void scanColumn(String tableName, String column) throws IOException {  
  2.        init();  
  3.        Table table = connection.getTable(TableName.valueOf(tableName));  
  4.        Scan scan = new Scan();  
  5.        scan.addFamily(Bytes.toBytes(column));  
  6.        ResultScanner scanner = table.getScanner(scan);  
  7.        for (Result result = scanner.next(); result != null; result = scanner.next()) {  
  8.            showCell(result);  
  9.        }  
  10.        table.close();  
  11.        close();  
  12.    }  

Java代码运行界面

 

 

 

      (4)modifyData(String tableName, String row, String column, String val)

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

Java代码运行界面

  1. public static void modifyData(String tableName, String row, String column, String val) throws IOException {  
  2.     long ts = 0;  
  3.     init();  
  4.        Table table = connection.getTable(TableName.valueOf(tableName));  
  5.        Put put = new Put(row.getBytes());  
  6.        Scan scan = new Scan();  
  7.        ResultScanner resultScanner = table.getScanner(scan);  
  8.        for (Result r : resultScanner) {  
  9.            for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {  
  10.                ts = cell.getTimestamp();  
  11.            }  
  12.        }  
  13.        put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());  
  14.        table.put(put);  
  15.        System.out.println(tableName+"表的"+row+"行的"+column+"列已经修改为:"+val);  //打印列删除  
  16.        table.close();  
  17.        close();  
  18.    }  

运行代码之前查看表 school

 

 

运行代码修改Score的Score:Math的值为-1533

 

进入hbase shell查看修改成功

 

 

(5)deleteRow(String tableName, String row)

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

  1. //删除数据  
  2.    public static void deleteRow(String tableName, String row) throws IOException {  
  3.        init();  
  4.        Table table = connection.getTable(TableName.valueOf(tableName));  
  5.        Delete delete=new Delete(row.getBytes());  
  6.        table.delete(delete);  
  7.        System.out.println("这个"+delete+"行已经删除咯!");  //打印列删除  
  8.        table.close();  
  9.  
  10.        close();  
  11.    } 
  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
一、实验目的                                                                                                                                                        1. 熟练使用Microsoft Visual Studio 2005开发平台建立数据库应用系统。                                                                                                                                                        2. 学会ASP.NET和ADO.NET数据库连接。                                                                                                                                                        3. 了解数据库应用系统开发的基本过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值