你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop实验——HBase的安装和实验

[日期:2017-02-27] 来源:简书  作者:Tiny_16 [字体: ]

实验目的

  1. 理解HBase在hadoop体系结构中的角色。
  2. 熟练使用HBase操作常用的Shell命令。
  3. 熟悉HBase操作常用的Java API。

实验平台

  • 操作系统:Ubuntu-16.04
  • Hadoop版本:2.6.0
  • JDK版本:1.8
  • IDE:Eclipse
  • HBase版本:1.2.3

实验内容和要求

HBase的安装:

  1. 把 HBase 压缩包放到 Home 文件夹中 
    Hadoop,HBase
  2. 右键打开终端,解压 HBase 到/usr/local 
    sudo tar zxvf hbase-1.2.3-bin.tar.gz -C /usr/local
  3. 重命名方便后续操作 
    sudo mv /usr/local/hbase-1.2.3/ /usr/local/hbase/
  4. 获取文件夹权限(tiny改为你的主机名) 
    sudo chown -R tiny /usr/local/hbase/
  5. 设置环境变量 
    sudo vim /etc/profile
    • 在最后一行添加内容:
      #set hbase path
      export PATH=$PATH:/usr/local/hbase/bin
  6. 使环境变量生效 
    source /etc/profile
  7. 配置Hbase的配置文件
    • 进入/usr/local/hbase/conf/ 
      cd /usr/local/hbase/conf/
    • 配置hbase-env.sh 
      vim hbase-env.sh
    • 在最后一行添加内容:
      export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_101
      export HBASE_MANAGES_ZK=true
    • 配置hbase-site.xml 
      vim hbase-site.xml
    • 在<configuration>标签中添加内容:
      <property>
              <name>hbase.rootdir</name>
              <value>hdfs://localhost:9000/hbase</value>
      </property>
      <property>
              <name>hbase.cluster.distributed</name>
              <value>true</value>
      </property>
  8. 启动 Hadoop
    • 进入 Hadoop 主文件夹 
      cd /usr/local/hadoop/
    • 开启 Hadoop 相关进程 
      sbin/start-dfs.sh 
      sbin/start-yarn.sh 
  9. 启动 HBase
    • 进入HBase主文件夹 
      cd /usr/local/hbase/
    • 开启HBase相关进程 
      bin/start-hbase.sh 
  10. 查看 jps 
  11. 进入 Hbase Shell 
  12. 创建一个表以便实验使用 
    create 'student','score' 

HBase实验:

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

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

    Shell命令实现:
    • 以列表的形式显示所有数据表 
      list
    • 查看表的结构 
      describe 'student'
    • 查询表是否存在 
      exists 'student'
    • 查询表是否可用 
      is_enabled 'student' 
      Eclipse的使用:
      1. 找到 File 菜单,选择 New -> Project 
      2. 选择 Java Project ,然后Next 
      3. 输入 Project name,然后Finish 
      4. 点开项目,找到 src 文件夹,右键选择 New -> Class 
      5. 输入 Package 和 Name,然后Finish 
      6. 右键工程,选择 Properties ,然后在工程中导入外部jar包 
      7. 写好Java代码,右键选择 Run As -> Java Application,就可以在Console里看到结果了 
        Java实现:
package com.zucc.tiny;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.*;
public class HBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    public static void listTables() throws IOException {
        init();
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            System.out.println("表名:" + hTableDescriptor.getNameAsString());
        }
        close();
    }

    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 (null != connection) {
                connection.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        public static void main(String[] args) {
                try {
                    HBase.listTables();
                } catch (IOException e) {
                    e.printStackTrace();
                }

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

    • 向student表,添加数据

      Shell命令实现:

      put 'student','s001','score:math','100' 

      Java实现:

      package com.zucc.tiny;
      import java.io.IOException;
      import org.apache.hadoop.conf.Configuration;
      import org.apache.hadoop.hbase.HTableDescriptor;
      import org.apache.hadoop.hbase.client.*;
      import org.apache.hadoop.hbase.*;
      public class HBase {
      
      public static Configuration configuration;
      public static Connection connection;
      public static Admin admin;
      
      public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
      init();
      Table table = connection.getTable(TableName.valueOf(tableName));
      Put put = new Put(rowKey.getBytes());
      put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
      table.put(put);
      table.close();
      close();
      }
      
      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 (null != connection) {
              connection.close();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      }
      public static void main(String[] args) {
              try {
                  HBase.insertRow("student","s002","score","math","100");
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
      }
      }
    • 向student表,删除指定列

      Shell命令实现:

      delete 'student','s001','score:math' 

      Java实现:

      package com.zucc.tiny;
      import java.io.IOException;
      import org.apache.hadoop.conf.Configuration;
      import org.apache.hadoop.hbase.client.*;
      import org.apache.hadoop.hbase.util.Bytes;
      import org.apache.hadoop.hbase.*;
      public class HBase {
      
      public static Configuration configuration;
      public static Connection connection;
      public static Admin admin;
      
      public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
      init();
      Table table = connection.getTable(TableName.valueOf(tableName));
      Delete delete = new Delete(rowKey.getBytes());
      //删除指定列族
      delete.addFamily(Bytes.toBytes(colFamily));
      //删除指定列
      delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
      table.delete(delete);
      table.close();
      close();
      }
      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 (null != connection) {
              connection.close();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      }
      public static void main(String[] args) {
              try {
                  HBase.deleteRow("student","s002","score","math");
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
      }
      }
  2. 在终端打印出指定的表的所有记录数据

    Shell命令实现:

    scan 'student' 

    Java实现:

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     public static void getData(String tableName) throws IOException {
         init();
         Table table = connection.getTable(TableName.valueOf(tableName));
         Scan scan = new Scan();
         ResultScanner scanner = table.getScanner(scan);
         for (Result result : scanner) {
             printRecoder(result);
         }
         close();
     }
    
     public  static void printRecoder(Result result)throws IOException{
         for(Cell cell:result.rawCells()){
             System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
             System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
             System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
             System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
             System.out.println("时间戳: "+cell.getTimestamp());
         }
     }
    
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         try {
             HBase.getData("student");
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }
  3. 清空指定的表的所有记录数据

    Shell命令实现:

    truncate 'student' 

    Java实现:

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     public static void clearRows(String tableName) throws IOException {
         init();
         TableName tablename = TableName.valueOf(tableName);
         admin.disableTable(tablename);
         admin.truncateTable(tablename, false);
         close();
     }
    
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         try {
             HBase.clearRows("student");
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }
  4. 统计表的行数

    Shell命令实现:

    count 'student' 

    Java实现:

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     public static void countRows(String tableName) throws IOException {
         init();
         Table table = connection.getTable(TableName.valueOf(tableName));
         Scan scan = new Scan();
         ResultScanner scanner = table.getScanner(scan);
         int num = 0;
         for (Result result = scanner.next(); result != null; result = scanner
                 .next()) {
             num++;
         }
         System.out.println("行数:" + num);
         scanner.close();
         close();
     }
    
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         try {
             HBase.countRows("student");
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }
  5. 删除一个表

    Shell命令实现:

    disable 'student'
    drop 'student'

    Java实现:

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    
    public static void deleteRows(String tableName) throws IOException {
      init();
      TableName tablename = TableName.valueOf(tableName);
      admin.disableTable(tablename);
      admin.deleteTable(tablename);
      close();
    }
    
    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 (null != connection) {
              connection.close();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
    }
    
    public static void main(String[] args) {
      try {
          HBase.deleteRows("student");
      } catch (IOException e) {
          e.printStackTrace();
      }
    
    }
    }

二.现有以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据(行名随意)

学生表(Student)

学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)
2015001 Zhangsan male 23
2015002 Mary female 22
2015003 Lisi male 24

课程表(Course)

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

选课表(SC)

课程号(C_No)课程名(C_Name)学分(C_Credit)
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' 
    插入数据:
    put 'Student','s001','S_No','2015001'
    put 'Student','s001','S_Name','Zhangsan'
    put 'Student','s001','S_Sex','male'
    put 'Student','s001','S_Age','23'
    put 'Student','s002','S_No','2015002'
    put 'Student','s002','S_Name','Mary'
    put 'Student','s002','S_Sex','female'
    put 'Student','s002','S_Age','22'
    put 'Student','s003','S_No','2015003'
    put 'Student','s003','S_Name','Lisi'
    put 'Student','s003','S_Sex','male'
    put 'Student','s003','S_Age','24'
  • 课程表 
    创建表: create 'Course','C_No','C_Name','C_Credit' 
    插入数据:
    put 'Course','c001','C_No','123001'
    put 'Course','c001','C_Name','Math'
    put 'Course','c001','C_Credit','2.0'
    put 'Course','c002','C_No','123002'
    put 'Course','c002','C_Name','Computer'
    put 'Course','c002','C_Credit','5.0'
    put 'Course','c003','C_No','123003'
    put 'Course','c003','C_Name','English'
    put 'Course','c003','C_Credit','3.0'
  • 选课表 
    创建表: create 'SC','SC_Sno','SC_Cno','SC_Score' 
    插入数据:
    put 'SC','sc001','SC_Sno','2015001'
    put 'SC','sc001','SC_Cno','123001'
    put 'SC','sc001','SC_Score','86'
    put 'SC','sc002','SC_Sno','2015001'
    put 'SC','sc002','SC_Cno','123003'
    put 'SC','sc002','SC_Score','69'
    put 'SC','sc003','SC_Sno','2015002'
    put 'SC','sc003','SC_Cno','123002'
    put 'SC','sc003','SC_Score','77'
    put 'SC','sc004','SC_Sno','2015002'
    put 'SC','sc004','SC_Cno','123003'
    put 'SC','sc004','SC_Score','99'
    put 'SC','sc005','SC_Sno','2015003'
    put 'SC','sc005','SC_Cno','123001'
    put 'SC','sc005','SC_Score','98'
    put 'SC','sc006','SC_Sno','2015003'
    put 'SC','sc006','SC_Cno','123002'
    put 'SC','sc006','SC_Score','95'
  1. createTable(String tableName, String[] fields)

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

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
     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);//删除原来的表
         }
         HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
         for(String str:fields){
             HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
             hTableDescriptor.addFamily(hColumnDescriptor);
         }
         admin.createTable(hTableDescriptor);
         close();
     }
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         String[]fields={"Score"};
         try {
             HBase.createTable("person", fields);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    }
  2. addRecord(String tableName, String row, String[] fields, String[] values)

    向表tableName、行row和字符串数组files指定的单元格中添加对应的数据values。其中fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向"Math",“ComputerScience”,"English"三列添加成绩时,字符串数组fields为{"Score:Math","Score:Computer Science","Score:English"},数组values存储这三门课的成绩。

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     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();
     }
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         String[]fields={"Score:Math","Score:Computer Science","Score:English"};
         String[]values={"99","80","100"};
         try {
             HBase.addRecord("person", "Score", fields, values);
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }
  3. scanColumn(String tableName, String column)

    浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据。

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     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(CellUtil.cloneRow(cell))+" ");
             System.out.println("Timetamp:"+cell.getTimestamp()+" ");
             System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
             System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
             System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
         }
     }
    
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         try {
             HBase.scanColumn("person", "Score");
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }
  4. modifyData(String tableName, String row, String column)

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

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static long ts ;
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     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());
         Scan scan = new Scan();
         ResultScanner resultScanner = table.getScanner(scan);
         for (Result r : resultScanner)
         {
             for (Cell cell :r.getColumnCells(row.getBytes(), column.getBytes()))
             {
                  ts=cell.getTimestamp();
             }
         }
         put.addColumn(row.getBytes(),column.getBytes(),ts,val.getBytes());
         table.put(put);
         table.close();
         close();
     }
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         try {
             HBase.modifyData("person", "Score", "Math", "100");
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }
  5. deleteRow(String tableName, String row)

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

    package com.zucc.tiny;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.*;
    public class HBase {
    
     public static Configuration configuration;
     public static Connection connection;
     public static Admin admin;
    
     public static void deleteRow(String tableName,String row)throws IOException{
         init();
         Table table = connection.getTable(TableName.valueOf(tableName));
         Delete delete = new Delete(row.getBytes());
         //删除指定列族
         //delete.addFamily(Bytes.toBytes(colFamily));
         //删除指定列
         //delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
         table.delete(delete);
         table.close();
         close();
     }
    
     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 (null != connection) {
                 connection.close();
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    
     public static void main(String[] args) {
         try {
             HBase.deleteRow("person", "Score");
         } catch (IOException e) {
             e.printStackTrace();
         }
    
     }
    }




收藏 推荐 打印 | 录入:elainebo | 阅读:
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数
点评:
       
评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款