hbase api常用操作

环境信息

  • hbase版本:hbase-2.2.4-bin.tar.gz

  • api版本 :

            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>2.2.4</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-common</artifactId>
                <version>2.2.4</version>
            </dependency>
    

API常用操作

  1. org.apache.hadoop.hbase.client.Admin

    Admin主要用来做DDL操作,可以执行创建、修改、删除表等操作

  2. org.apache.hadoop.hbase.client.Table

    Table主要用来对表执行DML操作,包括查询、新增、修改、删除数据等操作

  3. Put、Get、Delete、Scan等

    org.apache.hadoop.hbase.client.Table的参数,用来设置DML操作的相关信息

代码示例

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* hbase api的常用操作示例
*/
public class TestHbase {

    private Connection connection;
    private Admin admin;

    @Before
    public void init() throws Exception{
        //hbase连接配置,主要是zk地址
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("hbase.zookeeper.quorum", "t-d4-p-sszb,t-d5-p-sszb,t-d6-p-sszb");
        //创建hbase连接
        this.connection = ConnectionFactory.createConnection(configuration);
        //admin 主要用作DDL操作
        this.admin = connection.getAdmin();
    }

    /**
     * 创建了一个student表,
     * 包含两个列族(info:学生姓名、年纪等信息,score:学生各学科成绩)
     */
    @Test
    public void testCreateTable() throws Exception{
        //列族 info
        ColumnFamilyDescriptor infoColumnFamily = ColumnFamilyDescriptorBuilder
                .newBuilder("info".getBytes())
                .build();
        //列族 score
        ColumnFamilyDescriptor scoreColumnFamily = ColumnFamilyDescriptorBuilder
                .newBuilder("score".getBytes())
                //最大版本号
                .setMaxVersions(3)
                .build();
        List<ColumnFamilyDescriptor> columnsFamilies = new ArrayList(2);
        columnsFamilies.add(infoColumnFamily);
        columnsFamilies.add(scoreColumnFamily);

        //设置表名、列族信息、是否只读
        TableDescriptor studentTableDescriptor = TableDescriptorBuilder
                .newBuilder(TableName.valueOf("student"))
                .setColumnFamilies(columnsFamilies)
                .setReadOnly(false)
                .build();

        admin.createTable(studentTableDescriptor);
    }

    /**
     * hbase删除表之前需要先disable表
     */
    @Test
    public void testDropTable() throws Exception{
        TableName table = TableName.valueOf("student");
        if(admin.tableExists(table)){
            if(admin.isTableEnabled(table)){
                admin.disableTable(table);
            }
            admin.deleteTable(table);
        }
    }

    /**
     * 通过Put插入或者更新数据,需要指定rowkey和column的值
     */
    @Test
    public void testAddRow() throws Exception{
        TableName tableName = TableName.valueOf("student");
        //Table: 表的DML操作
        Table table = connection.getTable(tableName);

        Put put = new Put("stu_1".getBytes());
        put.addColumn("info".getBytes(), "name".getBytes(), "Lisa".getBytes());
        put.addColumn("info".getBytes(), "sex".getBytes(), "M".getBytes());
        put.addColumn("score".getBytes(), "Chinese".getBytes(), "130".getBytes());
        put.addColumn("score".getBytes(), "Math".getBytes(), "90".getBytes());

        table.put(put);
        table.close();
    }

    /**
     * 根据rowkey查询数据
     */
    @Test
    public void testGet() throws Exception{
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        //指定rowkey
        Get get = new Get("stu_1".getBytes());
        //查询指定列族
        //get.addFamily("score".getBytes());
        //查询指定列
        //get.addColumn("score".getBytes(), "Chinese".getBytes());

        Result result = table.get(get);
        result.listCells().forEach(cell -> {
            String format = "rowkey=%s,column=%s:%s,value=%s";
            String columnInfo = String.format(format,  new String(CellUtil.cloneRow(cell)), new String(CellUtil.cloneFamily(cell)),
                    new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell)));
            System.out.println(columnInfo);
        });
        table.close();
    }

    /**
     * 通过scan扫描范围数据,可通过filter来过滤数据
     */
    @Test
    public void testScan() throws Exception{
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        Scan scan = new Scan()
                //设置范围查询 >=
                //.withStartRow("stu_1".getBytes())
                //允许部分返回,可避免客户端OOM
                .setAllowPartialResults(true)
                //查询指定列族
                //.addFamily("score".getBytes())
                //查询指定列
                //.addColumn("score".getBytes(), "Chinese".getBytes())
                //设置一次RPC请求批量读取的Results数量
                .setCaching(2)
                .setFilter(new SingleColumnValueFilter("score".getBytes(), "Chinese".getBytes(), CompareOperator.GREATER_OR_EQUAL, "100".getBytes()));

        ResultScanner resultScanner = table.getScanner(scan);
        Result tmp;
        while ((tmp = resultScanner.next()) != null){
            tmp.listCells().stream().forEach(cell -> {
                String format = "rowkey=%s,column=%s:%s,value=%s";
                String columnInfo = String.format(format,  new String(CellUtil.cloneRow(cell)), new String(CellUtil.cloneFamily(cell)),
                        new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell)));
                System.out.println(columnInfo);
            });
        }

        resultScanner.close();
        table.close();
    }

    /**
     * 根据rowkey删除数据,可指定需要删除的列族或者列
     */
    @Test
    public void testDelete() throws Exception{
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        Delete delete = new Delete("stu_1".getBytes())
                //指定要删除的列族
                //.addFamily("score".getBytes())
                //指定要删除的列
                //.addColumn("score".getBytes(), "Chinese".getBytes())
                ;

        table.delete(delete);
        table.close();
    }

    @After
    public void after() throws Exception{
        if (connection != null){
            connection.close();
        }
        if (admin != null){
            admin.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值