zoukankan      html  css  js  c++  java
  • (二)hbase API的代码

    import java.io.IOException;
    //import java.util.Map; 
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.HBaseAdmin;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import org.apache.hadoop.hbase.util.Bytes;
    public class Hbasecreate {
             static Configuration cfg = HBaseConfiguration.create();
             public static void create(String tablename, String columnFamily) throws Exception {
                      HBaseAdmin admin = new HBaseAdmin(cfg);
                      if (admin.tableExists(tablename)) {
                              System.out.println("table Exists!");
                                       admin.disableTable(tablename);
                                       admin.deleteTable(tablename);
                      } else {
                              @SuppressWarnings("deprecation")
                              HTableDescriptor tableDesc = new HTableDescriptor(tablename);
                              tableDesc.addFamily(new HColumnDescriptor(columnFamily));
                              admin.createTable(tableDesc);
                              System.out.println("create table success!");
                      }
             }
             public static void put(String tablename, String row, String columnFamily, String column, String data)
                              throws IOException {
                      HTable table = new HTable(cfg, tablename);
                      Put p1 = new Put(Bytes.toBytes(row));
                      p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
                      table.put(p1);
                      System.out.println("put'" + row + "','" + columnFamily + ":" + column + "','" + data + "'");
             }
             public static void get(String tablename, String row) throws IOException {
                      HTable table = new HTable(cfg, tablename);
                      Get g = new Get(Bytes.toBytes(row));
                      Result result = table.get(g);
                      System.out.println("Get:" + result);
             }
             public static void scan(String tablename) throws Exception {
                      HTable table = new HTable(cfg, tablename);
                      Scan s = new Scan();
                      ResultScanner rs = table.getScanner(s);
                      for (Result r : rs) {
                              System.out.println("Scan:" + r);
                      }
             }
             public static boolean delete(String tablename) throws IOException {
                      HBaseAdmin admin = new HBaseAdmin(cfg);
                      if (admin.tableExists(tablename)) {
                              try {
                                       admin.disableTable(tablename);
                                       admin.deleteTable(tablename);
                              } catch (Exception ex) {
                                       ex.printStackTrace();
                                       return false;
                              }
                      }
                      return true;
             }
             public static void main(String[] args) {
                      String tablename = "hbase_tb";
                      String columnFamily = "cf";
                      try {
                              Hbasecreate.create(tablename, columnFamily);
                              Hbasecreate.put(tablename, "row1", columnFamily, "cl1", "data");
                              Hbasecreate.get(tablename, "row1");
                              Hbasecreate.scan(tablename);
                              /*
                               * if(true==Hbasecreate.delete(tablename)){
                               * System.out.println("Delete table:"+tablename+"success!"); }
                               */
                      } catch (Exception e) {
                              e.printStackTrace();
                      }
             }
    }
    

  • 相关阅读:
    数据库事务
    什么场景应该用 MongoDB ?
    ES6 箭头函数 =>
    HTML5 <template>标签元素简介
    ES6新特性:使用新方法定义javascript的Class
    Windows平台下Git(gitblit)服务器搭建
    利用WiFi Pineapple Nano渗透客户端获取SHELL
    出现 “未能创建此平台,因为已存在同名的解决方案平台”提示,是什么原因?
    VS2010 常用快捷键
    C# WINFORM 捕获全局异常
  • 原文地址:https://www.cnblogs.com/apppointint/p/8885313.html
Copyright © 2011-2022 走看看