HbaseApi

/**

  • @ClassName TestApi
  • 一. DDL
    1. 判断表是否存在
    1. 创建表 Create
    1. 删除表 disable drop
    1. 创建命名空间 Create NameSpace
  • 二. DML
    1. 添加数据 put
    1. 查询(get)
    1. 查询(scan)
    1. 删除数据 delete
  • @Description TODO
  • @Author mufeng_xky
  • @Date 2019/11/9 8:33
  • @Version V1.0
    **/
    public class TestApi {

    // 声明链接
    private static Connection connection = null;
    // 声明管理员对象
    private static Admin admin = null;

    // 初始化
    static {
    try {
    Configuration configuration = HBaseConfiguration.create();
    //configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    connection = ConnectionFactory.createConnection(configuration);
    admin = connection.getAdmin();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    • 释放资源
    • @throws IOException
      */
      private static void close() throws IOException {
      if (admin != null) {
      admin.close();
      }
      if (connection != null) {
      connection.close();
      }
      }
    /**
    • 判断表是否存在
    • @param tableName
    • @return
    • @throws IOException
      */
      public static boolean isTableExsit(String tableName) throws IOException {
      boolean exists = admin.tableExists(TableName.valueOf(tableName));
      return exists;
      }
    /**
    • 创建表
    • @param tableName
    • @param cf
      */
      public static void createTable(String tableName, String... cf) throws IOException {
      if (isTableExsit(tableName)) {
      // new IllegalArgumentException("表已经存在,在点我就报警了!");
      System.out.println("表已经存在,在点我就报警了!");
      return;
      }
      if (cf.length <= 0) {
      System.out.println("参数异常!");
      return;
      }
      HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
      for (String s : cf) {
      HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(s);
      descriptor.addFamily(hColumnDescriptor);
      }
      admin.createTable(descriptor);
      }
    /**
    • 删除表
    • @param tableName
    • @throws IOException
      */
      public static void deleteTable(String tableName) throws IOException {
      if (!isTableExsit(tableName)) {
      System.out.println("表不存在!");
      return;
      }
      admin.disableTable(TableName.valueOf(tableName));
      admin.deleteTable(TableName.valueOf(tableName));
      }
    /**
    • 创建命名空间
    • @param nameSpace
    • @throws IOException
      */
      public static void createNameSpace(String nameSpace) throws IOException {
      try {
      NamespaceDescriptor build = NamespaceDescriptor.create(nameSpace).build();
      admin.createNamespace(build);
      } catch (NamespaceExistException e) {
      System.out.println(nameSpace + "此命名空间已经存在!");
      } catch (IOException e) {
      e.printStackTrace();
      }
      }

    // =====================================>我是华丽分割线===================================>

    /**
    • 添加数据
    • @param tableName
    • @param cf
    • @param cn
    • @param value
      */
      public static void put(String tableName, String rk, String cf, String cn, String value) throws IOException {
      // 创建表对象
      Table table = connection.getTable(TableName.valueOf(tableName));
      // 声明put对象
      Put putObject = new Put(Bytes.toBytes(rk));
      // 向put对象中添加值
      putObject.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
      // putObject.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("female"));
      // 向表中添加数据
      table.put(putObject);
      // 关闭
      table.close();
      }
    /**
    • 查询数据 ---》 get
    • @param tableName
    • @param rk
    • @throws IOException
      */
      public static void get(String tableName, String rk) throws IOException {
      // 1. 获取对象
      Table table = connection.getTable(TableName.valueOf(tableName));
      // 2. 获得get对象
      Get getObject = new Get(Bytes.toBytes(rk));
      // 3. 获取查询结果
      Result result = table.get(getObject);
      // 4. 遍历获取单个cell并打印
      for (Cell cell : result.rawCells()) {
      System.out.println("RK : " + Bytes.toString(CellUtil.cloneRow(cell)) +
      ", CF : " + Bytes.toString(CellUtil.cloneFamily(cell)) +
      ", CN : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
      ", Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
      }
      // 5. 释放资源
      table.close();
      }
    /**
    • 查询数据 ---> scan
    • @param tableName
    • @throws IOException
      */
      public static void scan(String tableName) throws IOException {
      // 1. 获取表对象
      Table table = connection.getTable(TableName.valueOf(tableName));
      // 2. 创建Scan对象
      Scan scanObject = new Scan(Bytes.toBytes("1001"), Bytes.toBytes("1003"));
      // 3. 查询数据并获取结果
      ResultScanner scanner = table.getScanner(scanObject);
      // 4. 遍历
      for (Result result : scanner) {
      for (Cell cell : result.rawCells()) {
      System.out.println("RK : " + Bytes.toString(CellUtil.cloneRow(cell)) +
      ", CF : " + Bytes.toString(CellUtil.cloneFamily(cell)) +
      ", CN : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
      ", Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
      }
      }
      // 5. 释放资源
      table.close();
      }
    /**
    • 删除数据 ---> delete
    • @param tableName
    • @param rk
      */
      public static void delete(String tableName, String rk) throws IOException {
      // 1. 获取表对象
      Table table = connection.getTable(TableName.valueOf(tableName));
      // 2. 执行删除逻辑
      Delete deleteObject = new Delete(Bytes.toBytes(rk));
      table.delete(deleteObject);
      // 2. 释放资源
      table.close();
      }
    /**
    • 测试Api
    • @param args
      */
      public static void main(String[] args) throws IOException {
      // 1. 测试表是否存在
      // System.out.println(isTableExsit("employee"));
      // 2. 创建表
      // createTable("user", "info", "name");
      // 3. 删除表
      // deleteTable("user");
      // 4. 创建命名空间
      // createNameSpace("1703");
      // 5. 插入数据
      // put("user", "1003", "info", "name", "qiaoyuan2");
      // 6. 查询数据 get
      // get("user", "1001");
      // 7. 查询数据 scan
      // scan("user");
      // 8. 删除数据 delete
      delete("user", "1003");
      // 释放资源
      close();
      }
      }

HbaseApi

上一篇:11-Mybatis中使用PageHelper分页插件


下一篇:Oracle一些数据类型的区别