ToB企服应用市场:ToB评测及商务社交产业平台

标题: 大数据实行 实行三:熟悉常用的HBase操纵 [打印本页]

作者: 美食家大橙子    时间: 2024-12-5 22:13
标题: 大数据实行 实行三:熟悉常用的HBase操纵
实行三 熟悉常用的HBase操纵

一、实行目标

(1)理解HBase在Hadoop体系布局中的角色;
(2)熟练使用HBase操纵常用的Shell下令;
(3)熟悉HBase操纵常用的Java API。
二、实行平台

操纵体系:centos7;
Hadoop版本:3.3;
HBase版本:2.2.2;
JDK版本:1.8;
Java IDE:IDEA。
三、实行内容和要求

(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell下令完成相同任务:

(1) 列出HBase全部的表的相干信息,例如表名、创建时间等;


(2) 在终端打印出指定的表的全部记录数据;


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


(4) 清空指定的表的全部记录数据;


(5) 统计表的行数。


(二)HBase数据库操纵

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

学生表(Student)
学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)2015001Zhangsanmale232015003Maryfemale222015003Lisimale24 课程表(Course)
课程号(C_No)课程名(C_Name)学分(C_Credit)123001Math2.0123002Computer5.0123003English3.0 选课表(SC)
学号(SC_Sno)课程号(SC_Cno)成绩(SC_Score)201500112300186201500112300369201500212300277201500212300399201500312300198201500312300295 2 请编程实现以下功能:

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

创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
  1. package Main;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.TableName;
  5. import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
  6. import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
  7. import org.apache.hadoop.hbase.client.Connection;
  8. import org.apache.hadoop.hbase.client.Admin;
  9. import org.apache.hadoop.hbase.client.ConnectionFactory;
  10. import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
  11. import org.apache.hadoop.hbase.util.Bytes;
  12. import java.io.IOException;
  13. public class main {
  14.     public static Configuration configuration;
  15.     public static Connection connection;
  16.     public static Admin admin;
  17.     public static void init(){//建立连接
  18.         configuration = HBaseConfiguration.create();
  19.         configuration.set("hbase.rootdir","hdfs://127.0.0.1:8020/hbase");
  20.         try{
  21.             connection = ConnectionFactory.createConnection(configuration);
  22.             admin = connection.getAdmin();
  23.         }catch(IOException e){
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.     public static void close(){//关闭连接
  28.         try{
  29.             if(admin != null){
  30.                 admin.close();
  31.             }
  32.             if(connection != null){
  33.                 connection.close();
  34.             }
  35.         }catch(IOException e){
  36.             e.printStackTrace();
  37.         }
  38.     }
  39.     public static void createTable(String tableName,String[] fields) throws IOException{
  40.         init();
  41.         TableName tablename = TableName.valueOf(tableName);//定义表名
  42.         if(admin.tableExists(tablename)){
  43.             System.out.println("table is exists!");
  44.             admin.disableTable(tablename);
  45.             admin.deleteTable(tablename);
  46.         }
  47.         TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
  48.         for(int i=0;i<fields.length;i++){
  49.             ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(fields[i])).build();
  50.             tableDescriptor.setColumnFamily(family);
  51.         }
  52.         admin.createTable(tableDescriptor.build());
  53.         close();
  54.     }
  55.     public static void main(String[] args){
  56.         String[] fields = {"id","score"};
  57.         try{
  58.             createTable("test",fields);
  59.         }catch(IOException e){
  60.             e.printStackTrace();
  61.         }
  62.     }
  63. }
复制代码
运行结果

(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. package Main;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.TableName;
  6. import org.apache.hadoop.hbase.client.Admin;
  7. import org.apache.hadoop.hbase.client.Connection;
  8. import org.apache.hadoop.hbase.client.ConnectionFactory;
  9. import org.apache.hadoop.hbase.client.Put;
  10. import org.apache.hadoop.hbase.client.Table;
  11. public class main {
  12.     public static Configuration configuration;
  13.     public static Connection connection;
  14.     public static Admin admin;
  15.     public static void init(){//建立连接
  16.         configuration = HBaseConfiguration.create();
  17.         configuration.set("hbase.rootdir","hdfs://127.0.0.1:8020/hbase");
  18.         try{
  19.             connection = ConnectionFactory.createConnection(configuration);
  20.             admin = connection.getAdmin();
  21.         }catch(IOException e){
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     public static void close(){//关闭连接
  26.         try{
  27.             if(admin != null){
  28.                 admin.close();
  29.             }
  30.             if(connection != null){
  31.                 connection.close();
  32.             }
  33.         }catch(IOException e){
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.     public static void addRecord(String tableName,String row,String[] fields,String[] values) throws IOException{
  38.         init();//连接Hbase
  39.         Table table = connection.getTable(TableName.valueOf(tableName));//表连接
  40.         Put put = new Put(row.getBytes());//创建put对象
  41.         for(int i=0;i<fields.length;i++){
  42.             String[] cols = fields[i].split(":");
  43.             if(cols.length == 1){
  44.                 put.addColumn(fields[i].getBytes(),"".getBytes(),values[i].getBytes());
  45.             }
  46.             else{
  47.                 put.addColumn(cols[0].getBytes(),cols[1].getBytes(),values[i].getBytes());
  48.             }
  49.             table.put(put);//向表中添加数据
  50.         }
  51.         close();//关闭连接
  52.     }
  53.     public static void main(String[] args){
  54.         String[] fields = {"Score:Math","Score:Computer Science","Score:English"};
  55.         String[] values = {"85","80","90"};
  56.         try{
  57.             addRecord("grade","S_Name",fields,values);
  58.         }catch(IOException e){
  59.             e.printStackTrace();
  60.         }
  61.     }
  62. }
复制代码
3)scanColumn(String tableName, String column)

欣赏表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列
  1. package Main;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.Cell;
  5. import org.apache.hadoop.hbase.CellUtil;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.TableName;
  8. import org.apache.hadoop.hbase.client.Admin;
  9. import org.apache.hadoop.hbase.client.Connection;
  10. import org.apache.hadoop.hbase.client.ConnectionFactory;
  11. import org.apache.hadoop.hbase.client.Result;
  12. import org.apache.hadoop.hbase.client.ResultScanner;
  13. import org.apache.hadoop.hbase.client.Scan;
  14. import org.apache.hadoop.hbase.client.Table;
  15. import org.apache.hadoop.hbase.util.Bytes;
  16. public class main {
  17.     public static Configuration configuration;
  18.     public static Connection connection;
  19.     public static Admin admin;
  20.     public static void init(){//建立连接
  21.         configuration = HBaseConfiguration.create();
  22.         configuration.set("hbase.rootdir","hdfs://localhost:8020/hbase");
  23.         try{
  24.             connection = ConnectionFactory.createConnection(configuration);
  25.             admin = connection.getAdmin();
  26.         }catch(IOException e){
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.     public static void close(){//关闭连接
  31.         try{
  32.             if(admin != null){
  33.                 admin.close();
  34.             }
  35.             if(connection != null){
  36.                 connection.close();
  37.             }
  38.         }catch(IOException e){
  39.             e.printStackTrace();
  40.         }
  41.     }
  42.     public static void showResult(Result result){
  43.         Cell[] cells = result.rawCells();
  44.         for(int i=0;i<cells.length;i++){
  45.             System.out.println("RowName:"+new String(CellUtil.cloneRow(cells[i])));//打印行键
  46.             System.out.println("ColumnName:"+new String(CellUtil.cloneQualifier(cells[i])));//打印列名
  47.             System.out.println("Value:"+new String(CellUtil.cloneValue(cells[i])));//打印值
  48.             System.out.println("Column Family:"+new String(CellUtil.cloneFamily(cells[i])));//打印列簇
  49.             System.out.println();
  50.         }
  51.     }
  52.     public static void scanColumn(String tableName,String column){
  53.         init();
  54.         try {
  55.             Table table = connection.getTable(TableName.valueOf(tableName));
  56.             Scan scan  = new Scan();
  57.             scan.addFamily(Bytes.toBytes(column));
  58.             ResultScanner scanner = table.getScanner(scan);
  59.             for(Result result = scanner.next();result != null;result = scanner.next()){
  60.                 showResult(result);
  61.             }
  62.         } catch (IOException e) {
  63.             e.printStackTrace();
  64.         }
  65.         finally{
  66.             close();
  67.         }
  68.     }
  69.     public static void main(String[] args){
  70.         scanColumn("test","id");
  71.     }
  72. }
复制代码
运行结果

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

修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
  1. package Main;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.TableName;
  6. import org.apache.hadoop.hbase.client.Admin;
  7. import org.apache.hadoop.hbase.client.Connection;
  8. import org.apache.hadoop.hbase.client.ConnectionFactory;
  9. import org.apache.hadoop.hbase.client.Put;
  10. import org.apache.hadoop.hbase.client.Table;
  11. public class main{
  12.     public static Configuration configuration;
  13.     public static Connection connection;
  14.     public static Admin admin;
  15.     public static void init(){//建立连接
  16.         configuration = HBaseConfiguration.create();
  17.         configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
  18.         try{
  19.             connection = ConnectionFactory.createConnection(configuration);
  20.             admin = connection.getAdmin();
  21.         }catch(IOException e){
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     public static void close(){//关闭连接
  26.         try{
  27.             if(admin != null){
  28.                 admin.close();
  29.             }
  30.             if(connection != null){
  31.                 connection.close();
  32.             }
  33.         }catch(IOException e){
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.     public static void modifyData(String tableName,String row,String column,String value) throws IOException{
  38.         init();
  39.         Table table = connection.getTable(TableName.valueOf(tableName));
  40.         Put put = new Put(row.getBytes());
  41.         String[] cols = column.split(":");
  42.         if(cols.length == 1){
  43.             put.addColumn(column.getBytes(),"".getBytes(), value.getBytes());
  44.         }
  45.         else{
  46.             put.addColumn(cols[0].getBytes(), cols[1].getBytes(), value.getBytes());
  47.         }
  48.         table.put(put);
  49.         close();
  50.     }
  51.     public static void main(String[] args){
  52.         try{
  53.             modifyData("test","1","score","100");
  54.         }
  55.         catch(Exception e){
  56.             e.printStackTrace();
  57.         }
  58.     }
  59. }
复制代码
运行结果

此时row为1的score已经改为100
(5)deleteRow(String tableName, String row)

  1. 删除表tableName中row指定的行的记录。
复制代码
  1. package Main;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.TableName;
  6. import org.apache.hadoop.hbase.client.Admin;
  7. import org.apache.hadoop.hbase.client.Connection;
  8. import org.apache.hadoop.hbase.client.ConnectionFactory;
  9. import org.apache.hadoop.hbase.client.Delete;
  10. import org.apache.hadoop.hbase.client.Table;
  11. public class main {
  12.     public static Configuration configuration;
  13.     public static Connection connection;
  14.     public static Admin admin;
  15.     public static void init(){//建立连接
  16.         configuration = HBaseConfiguration.create();
  17.         configuration.set("hbase.rootdir","hdfs://localhost:8020/hbase");
  18.         try{
  19.             connection = ConnectionFactory.createConnection(configuration);
  20.             admin = connection.getAdmin();
  21.         }catch(IOException e){
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     public static void close(){//关闭连接
  26.         try{
  27.             if(admin != null){
  28.                 admin.close();
  29.             }
  30.             if(connection != null){
  31.                 connection.close();
  32.             }
  33.         }catch(IOException e){
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.     public static void deleteRow(String tableName,String row) throws IOException{
  38.         init();
  39.         Table table = connection.getTable(TableName.valueOf(tableName));
  40.         Delete delete = new Delete(row.getBytes());
  41.         table.delete(delete);
  42.         close();
  43.     }
  44.     public static void main(String[] args){
  45.         try{
  46.             deleteRow("test","2");
  47.         }catch(Exception e){
  48.             e.printStackTrace();
  49.         }
  50.     }
  51. }
复制代码

此时row=2已经被删除

出现的题目

题目一
在安装hbase后master-status用欣赏器无法打开,而此时Hmaster和HregionServer,QuorumPeerMain已经启动
题目二

解决方法

题目一
在毗连Hbase后使用list下令后发现如下

发现原因是由于我启动Hbase使用的不是hbase自带的zookeeper,而是自己独立安装的,在hbase-env.sh下增加
  1. export HBASE_DISABLE_HADOOP_CLASSPATH_LOOKUP="true"
复制代码
重新启动后

题目解决
题目二
发现是maven导入包出现题目,再将hbase-client包换为2.5.3后题目解决


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4