使用Java API操作HBase

package hbase;

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 org.junit.Test;

import java.io.IOException;

public class HBaseClient {
//提取方法加static 再写main方法调用
    //创建表
    @Test
    public void createTable() throws IOException {
        //1、创建配置
        Configuration conf = HBaseConfiguration.create();
        //2、配置zookeeper集群和端口
        conf.set("hbase.zookeeper.quorum","hadoop1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //3、创建链接
        Connection conn = ConnectionFactory.createConnection(conf);
        //4、创建admin
        Admin admin = conn.getAdmin();
        //5、创建表的描述信息
        HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student"));
        //6、添加列簇
        student.addFamily(new HColumnDescriptor("info"));
        student.addFamily(new HColumnDescriptor("score"));
        //7、调用API进行建表操作
        admin.createTable(student);

    }


    //判断表是否存在
    @Test
    public void isTableExists() throws IOException {
        //1、创建配置
        Configuration conf = HBaseConfiguration.create();
        //2、配置zookeeper集群和端口
        conf.set("hbase.zookeeper.quorum","hadoop1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //3、创建链接
        Connection conn = ConnectionFactory.createConnection(conf);
        //4、创建admin
        Admin admin = conn.getAdmin();
        //5、调用API判断表是否存在

        System.out.println(admin.tableExists(TableName.valueOf("student")));
    }

    //向表中插入数据(DML)
    @Test
    public void putDataTable() throws IOException {
        //1、创建配置
        Configuration conf = HBaseConfiguration.create();
        //2、配置zookeeper集群和端口
        conf.set("hbase.zookeeper.quorum","hadoop1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //3、创建链接
        Connection conn = ConnectionFactory.createConnection(conf);
        //4、创建Table类
        Table student = conn.getTable(TableName.valueOf("student"));
        //5、创建Put类
        Put put = new Put(Bytes.toBytes("1001"));
        //6、向put中添加列簇 列名 值   主要转化成字节数组
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("female"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("course"),Bytes.toBytes("math"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("score"),Bytes.toBytes("89"));
        //7、调用API插入
        student.put(put);

    }
    //查看数据
    @Test
    public void getDataFormTable() throws IOException {
        //1、创建配置
        Configuration conf = HBaseConfiguration.create();
        //2、配置zookeeper集群和端口
        conf.set("hbase.zookeeper.quorum","hadoop1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //3、创建链接
        Connection conn = ConnectionFactory.createConnection(conf);
        //4、创建Table类
        Table student = conn.getTable(TableName.valueOf("student"));
        //5、创建get类
        Get get = new Get(Bytes.toBytes("1001"));
        //6、调用API
        Result result = student.get(get);
        //7、遍历数据
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("rowkey :"+Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("列族    :"+Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列名    :"+Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值      :"+Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("------------------");
        }
    }


    //删除表
    @Test
    public void dropTable() throws IOException {
        //1、创建配置
        Configuration conf = HBaseConfiguration.create();
        //2、配置zookeeper集群和端口
        conf.set("hbase.zookeeper.quorum","hadoop1");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //3、创建链接
        Connection conn = ConnectionFactory.createConnection(conf);
        //4、创建admin
        Admin admin = conn.getAdmin();
        //5、禁用表
        admin.disableTable(TableName.valueOf("student"));
        //6、删除表
        admin.deleteTable(TableName.valueOf("student"));

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值