Java连接Hbase实现增删改查

一、导入maven依赖

<dependency>
	<groupId>org.apache.hbase</groupId>
	<artifactId>hbase-server</artifactId>
	<version>2.2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.2.6</version>
</dependency>

二、编写Java代码

package com.qujiuge;

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

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

public class HbaseDemo {
    private HBaseAdmin admin;
    private Connection conn;

    @Before
    public void init() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.159.9");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conn = ConnectionFactory.createConnection(conf);
        admin = (HBaseAdmin) conn.getAdmin();
    }

    @After
    public void destroy() throws IOException {
        admin.close();
        conn.close();
    }

    /**
     * 创建表
     *
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {
        TableName tableName = TableName.valueOf("person");
        // 表的构建类
        TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
        // 列族的描述类
        ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder("info".getBytes()).build();
        builder.setColumnFamily(family);
        // 向表的构建类添加列族后,再创建表
        TableDescriptor table = builder.build();
        admin.createTable(table);
    }

    /**
     * 删除表
     *
     * @throws IOException
     */
    @Test
    public void deleteTable() throws IOException {
        TableName tableName = TableName.valueOf("person");
        // 先使表不可用,再删除表
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }

    /**
     * 列出表
     *
     * @throws IOException
     */
    @Test
    public void list() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
//            System.out.println(tableName.isSystemTable());
        }
    }

    /**
     * 判断表是否存在
     *
     * @throws IOException
     */
    @Test
    public void isTableExists() throws IOException {
        TableName tableName = TableName.valueOf("student");
        System.out.println(admin.tableExists(tableName));
    }

    /**
     * 向表中插入数据
     *
     * @throws IOException
     */
    @Test
    public void insertMany() throws IOException {
        TableName tableName = TableName.valueOf("student");
        Table table = conn.getTable(tableName);
        ArrayList<Put> puts = new ArrayList<>(); // 添加多个put实现批量插入
        Put put = new Put(Bytes.toBytes("1003"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("female"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
        puts.add(put);
        table.put(puts);
        // 提交
        admin.flush(tableName);
    }

    /**
     * 删除表中的记录
     *
     * @throws IOException
     */
    @Test
    public void deleteRecords() throws IOException {
        TableName tableName = TableName.valueOf("student");
        Table table = conn.getTable(TableName.valueOf("student"));
        Delete delete = new Delete(Bytes.toBytes("1003"));
        table.delete(delete); // 可传入多个delete,实现批量删除
        // 提交
        admin.flush(tableName);
    }

    /**
     * 更新记录
     *
     * @throws IOException
     */
    @Test
    public void updateRecord() throws IOException {
        TableName tableName = TableName.valueOf("student");
        Table table = conn.getTable(TableName.valueOf("student"));
        Put put = new Put(Bytes.toBytes("1002"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Smith"));
        table.put(put); // 传入put的列表可达到更新多条数据
        // 提交
        admin.flush(tableName);
    }

    /**
     * 查询表中所有记录
     *
     * @throws IOException
     */
    @Test
    public void findAll() throws IOException {
        Scan scan = new Scan();
        Table table = conn.getTable(TableName.valueOf("student"));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
                System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
                System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
                System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\n");
            }
        }
    }

    /**
     * 查找表中的一条记录
     *
     * @throws IOException
     */
    @Test
    public void findOne() throws IOException {
        Table table = conn.getTable(TableName.valueOf("student"));
        Get get = new Get(Bytes.toBytes("1001"));
        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
            System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
            System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
            System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\t");
            System.out.println("时间戳:" + cell.getTimestamp());
        }
    }

    /**
     * 查找表中某列族下列的信息
     *
     * @throws IOException
     */
    @Test
    public void findOneQualifier() throws IOException {
        Table table = conn.getTable(TableName.valueOf("student"));
        Get get = new Get(Bytes.toBytes("1001"));
        get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
            System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
            System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值