zoukankan      html  css  js  c++  java
  • 吴超老师课程--HBASE的Java_API

    
    
    public static void main(String[] args) throws IOException {
        String tableName="hbase_tb";
        String columnFamily="cf";
        
        HBaseTestCase.create(tableName, columnFamily);
        HBaseTestCase.put(tableName, "row1", columnFamily, "cl1", "data");
        HBaseTestCase.get(tableName, "row1");
        HBaseTestCase.scan(tableName);
        HBaseTestCase.delete(tableName);
    }
    
    
    
    1 //hbase操作必备
    2     private static Configuration getConfiguration() {
    3         Configuration conf = HBaseConfiguration.create();
    4         conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
    5         //使用eclipse时必须添加这个,否则无法定位
    6         conf.set("hbase.zookeeper.quorum", "hadoop");
    7         return conf;
    8     }
    
     1 //创建一张表
     2 public static void create(String tableName, String columnFamily) throws IOException{
     3     HBaseAdmin admin = new HBaseAdmin(getConfiguration());
     4     if (admin.tableExists(tableName)) {
     5         System.out.println("table exists!");
     6     }else{
     7         HTableDescriptor tableDesc = new HTableDescriptor(tableName);
     8         tableDesc.addFamily(new HColumnDescriptor(columnFamily));
     9         admin.createTable(tableDesc);
    10         System.out.println("create table success!");
    11     }
    12 }
    1 //添加一条记录
    2 public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException{
    3     HTable table = new HTable(getConfiguration(), tableName);
    4     Put p1 = new Put(Bytes.toBytes(row));
    5     p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),     Bytes.toBytes(data));
    6     table.put(p1);
    7     System.out.println("put'"+row+"',"+columnFamily+":"+column+"','"+data+"'");
    8 }
    1 //读取一条记录
    2 public static void get(String tableName, String row) throws IOException{
    3     HTable table = new HTable(getConfiguration(), tableName);
    4     Get get = new Get(Bytes.toBytes(row));
    5     Result result = table.get(get);
    6     System.out.println("Get: "+result);
    7 }
    1 //显示所有数据
    2 public static void scan(String tableName) throws IOException{
    3     HTable table = new HTable(getConfiguration(), tableName);
    4     Scan scan = new Scan();
    5     ResultScanner scanner = table.getScanner(scan);
    6     for (Result result : scanner) {
    7         System.out.println("Scan: "+result);
    8     }
    9 }
     1 //删除表
     2 public static void delete(String tableName) throws IOException{
     3     HBaseAdmin admin = new HBaseAdmin(getConfiguration());
     4     if(admin.tableExists(tableName)){
     5         try {
     6           admin.disableTable(tableName);
     7           admin.deleteTable(tableName);
     8         } catch (IOException e) {
     9           e.printStackTrace();
    10           System.out.println("Delete "+tableName+" 失败");
    11         }
    12     }
    13     System.out.println("Delete "+tableName+" 成功");
    14 }



  • 相关阅读:
    Java数据处理,Map中数据转double并取小数点后两位
    19年7月份面试7家公司,整理的java面试题(答案自行百度解决,也是个学习的过程)
    2019阿里云面试题,竟然现场敲代码,现在的企业越来越实在了
    解决win10状态栏的搜索框无法搜索本地应用或无反应
    sql,按照时间排序,取前N条
    List数组排序
    阿里云的maven仓库 地址
    hashcode相等两个类一定相等吗?equals呢?相反呢?
    杂记
    java排序
  • 原文地址:https://www.cnblogs.com/hellochennan/p/5365957.html
Copyright © 2011-2022 走看看