HBase Java常用的API

HBase Java常用的API

package com.itheima;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
//import org.junit.jupiter.api.Test;
import org.junit.Before;
import org.junit.After;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }

    public Table table;
    public Admin admin;
    public Connection hBaseConn;


    @Before
    public void before() throws Exception {
        //1. 根据连接工厂构建hbase的连接对象
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "node1:2181,node2:2181,node3:2181");
        hBaseConn = ConnectionFactory.createConnection(conf);
        //2. 根据连接对象, 获取相关的管理对象: admin  table
        admin = hBaseConn.getAdmin();
        String tableName = "WATER_BILL";
        table = hBaseConn.getTable(TableName.valueOf(tableName));
    }

    @After
    public void after() throws Exception {
        //4. 释放资源
        table.close();
        admin.close();
        hBaseConn.close();
    }


    @Test
    public void test02() throws Exception{
        Put put = new Put("4944192".getBytes());
        put.addColumn("C1".getBytes(), "name".getBytes(), "登卫红".getBytes());
        put.addColumn("C1".getBytes(), "address".getBytes(), "贵州省铜仁市德江县7单元267室".getBytes());
        put.addColumn("C2".getBytes(), "sex".getBytes(), "男".getBytes());
        table.put(put);
        table.close();
        hBaseConn.close();
    }

    @Test
    public void test01() throws Exception{
//        Configuration conf = HBaseConfiguration.create();
//        conf.set("hbase.zookeeper.quorum", "node1:2181,node2:2181,node3:2181");
//        Connection hBaseConn = ConnectionFactory.createConnection(conf);
//        Admin admin = hBaseConn.getAdmin();

        TableName tableName = TableName.valueOf("WATER_BILL");
        boolean flag = admin.tableExists(tableName);

        if (!flag) {    // 说明表不存在:
            //3.3: 创建表的构建器
            TableDescriptorBuilder descriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
            List<ColumnFamilyDescriptor> families = new ArrayList<>();
            families.add(ColumnFamilyDescriptorBuilder.newBuilder("C1".getBytes()).build());
            families.add(ColumnFamilyDescriptorBuilder.newBuilder("C2".getBytes()).build());
            //3.4: 在表构建器中添加 表的列族信息
            descriptorBuilder.setColumnFamilies(families);
            //3.5: 通过表构建器, 构建表信息对象
            TableDescriptor desc = descriptorBuilder.build();
            //3.2: 执行创建表的操作
            admin.createTable(desc);
        }
    }

    //6. 查询 rowkey 为 4944192 的数据
    @Test
    public void test06() throws Exception{
        //3. 执行相关的操作
        //3.1: 执行查询
        Get get = new Get("4944192".getBytes());
        Result result = table.get(get);
        //3.2: 处理结果集
        List<Cell> cellList = result.listCells();  // 获取 这一行所有的单元格
        for (Cell cell : cellList) { // 单元格:  rowkey + 列族 + 列名 + 列值
            byte[] rowkeyBytes = CellUtil.cloneRow(cell);
            byte[] familyBytes = CellUtil.cloneFamily(cell);
            byte[] qualifierBytes = CellUtil.cloneQualifier(cell);
            byte[] valueBytes = CellUtil.cloneValue(cell);

            String rowkey = Bytes.toString(rowkeyBytes);
            String family = Bytes.toString(familyBytes);
            String qualifier = Bytes.toString(qualifierBytes);
            String value1 = Bytes.toString(valueBytes);

            System.out.println("rowkey为:"+rowkey +"; family:"+family +"; qualifier:"+qualifier+"; value:"+value1);
        }
    }

    // 删除数据 4944192
    @Test
    public void test03() throws Exception{
        Delete delete = new Delete("4944192".getBytes());
        delete.addFamily("C2".getBytes());
        delete.addColumn("C1".getBytes(), "name".getBytes());
        table.delete(delete);
//        rowkey为:4944192; family:C1; qualifier:name; value:登卫红
//        rowkey为:4944192; family:C2; qualifier:sex; value:男
    }

    // 删除表
    @Test
    public void test04() throws Exception{
        String tableName = "WATER_BILL";
        if (admin.isTableEnabled(TableName.valueOf(tableName))){
            admin.disableTable(TableName.valueOf(tableName));
        }
        admin.deleteTable(TableName.valueOf(tableName));

    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值