Hbase中API的DML和DDL操作


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AdminTest {
    public static void main(String[] args) throws IOException {

//        System.out.println(AdminTest.TableisExist("stu"));
//        AdminTest.addTable("test1","cf1","cf2");
//        AdminTest.deleteTable("test2");
//        AdminTest.addValue("stu");
//        AdminTest.addValues("stu");
          AdminTest.getData("stu");
    }

    //判断表是否存在
  public static boolean TableisExist(String tablename) throws IOException {
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
      conf.set("hbase.zookeeper.property.clientPort","2181");
      Connection conn = ConnectionFactory.createConnection(conf);
      HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
      admin.close();
     return  admin.tableExists(tablename);

  }


  //添加一个表
  public static void addTable(String tablename,String... cfs) throws IOException {
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
      conf.set("hbase.zookeeper.property.clientPort","2181");
      Connection conn = ConnectionFactory.createConnection(conf);
      HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();//获取客户端对象
      //创建表描述器
      HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename));

      //循环添加列族信息
      for (String cf : cfs) {

          //添加列族描述器
          HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
          hTableDescriptor.addFamily(hColumnDescriptor);
      }
      admin.createTable(hTableDescriptor);
      admin.close();
  }


  //删除一个表
  public static void deleteTable(String tablename) throws IOException {
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
      conf.set("hbase.zookeeper.property.clientPort","2181");
      Connection conn = ConnectionFactory.createConnection(conf);
      HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();//获取客户端对象
      //删除表之前必选先让表下线
      admin.disableTable(TableName.valueOf(tablename));
      //删除表
      admin.deleteTable(tablename);
      admin.close();
  }


  //在表中插入一条数据
  public static void addValue(String tablename) throws IOException {
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
      conf.set("hbase.zookeeper.property.clientPort","2181");
      Connection conn = ConnectionFactory.createConnection(conf);
      Table table = conn.getTable(TableName.valueOf(tablename));
      //插入数据之前指定更具体的RowKey
      Put put = new Put(Bytes.toBytes("1001"));
      //调用addColumn方法指定所要插入的列族,列修饰符
      put.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("phone"),Bytes.toBytes("123"));
      table.put(put);
      table.close();
      conn.close();
  }

  //在表中插入多条数据
  public static void addValues(String tablename) throws IOException {
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
      conf.set("hbase.zookeeper.property.clientPort","2181");
      Connection conn = ConnectionFactory.createConnection(conf);
      Table table = conn.getTable(TableName.valueOf(tablename));
      List <Put>  puts = new ArrayList<Put>();//利用集合向表中插入多条数据
      Put put1 = new Put(Bytes.toBytes("1003"));
      //调用addColumn方法指定所要插入的列族,列修饰符
      put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("addr"),Bytes.toBytes("beijing"));
      Put put2 = new Put(Bytes.toBytes("1003"));
      put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("age"),Bytes.toBytes("18"));
      puts.add(put1);
      puts.add(put2);
      table.put(puts);
      table.close();
      conn.close();
  }

  //查询现有表中的一行数据
  public static void getData(String tablename) throws IOException {
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
      conf.set("hbase.zookeeper.property.clientPort","2181");
      Connection conn = ConnectionFactory.createConnection(conf);
      Table table = conn.getTable(TableName.valueOf(tablename));
      Get get = new Get(Bytes.toBytes("1003"));//获取RowKey为1003这一行的数据
      get.setMaxVersions(2);//设置最大版本为2
      get.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("num"));
      Result result = table.get(get);
      for(Cell cell : result.rawCells()){
          //分别获取键值,列族,列,值
          System.out.println("RowKey: "+new String(CellUtil.cloneRow(cell)));
          System.out.println("CF: "+new String(CellUtil.cloneFamily(cell)));
          System.out.println("CN: "+new String(CellUtil.cloneQualifier(cell)));
          System.out.println("Value: "+new String(CellUtil.cloneValue(cell)));
      }
      table.close();
      conn.close();
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值