zoukankan      html  css  js  c++  java
  • 常用的 HBase 操作

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

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

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

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

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

    5统计表的行数。

    (二)HBase 数据库操作

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

    据:

    请编程实现以下功能:

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

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

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

    数据。

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

    数据。

    5deleteRow(String tableName, String row) 删除表 tableName row 指定的行的记录。

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.*;

    import org.apache.hadoop.io.IOUtils;

    import org.apache.hadoop.util.Progressable;

    public class HbaseTest {

         public static Configuration configuration;

         public static Connection connection;

         public static Admin admin;

         public static long ts;

          

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

         }

         }

         //当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表

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

         }

        

    }

  • 相关阅读:
    js选项卡
    js 逻辑运算符
    git 标签管理
    git多人协作
    git 分支强制删除
    git bug修复
    DOS命令编译JAVA程序
    JDK的安装与配置
    我在linux的第一个C程序
    看我如何在控制台一行显示几万字符。
  • 原文地址:https://www.cnblogs.com/aishanyishi/p/10304688.html
Copyright © 2011-2022 走看看