HBase API快速入门-DDL操作

2021/7/19 23:36:23

本文主要是介绍HBase API快速入门-DDL操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

所需pom文件

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
	<version>1.3.3</version>
</dependency>
<dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.13.2</version>
</dependency>

初始化配置

	private static Configuration conf = null;
    private static Connection conn = null;
    private static Admin admin = null;
 	/**
     * 初始化配置
     *
     * @throws IOException
     */
    @Before
    public void init() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hdp1:2181,hdp2:2181,hdp3:2181");
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
    }

判断是否有指定表

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

创建表

    /**
     * 创建表
     *
     * @throws IOException
     */
    @Test
    public void create_table() throws IOException {
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("emp_table"));
        hTableDescriptor.addFamily(new HColumnDescriptor("info"));
        hTableDescriptor.addFamily(new HColumnDescriptor("exp"));
        admin.createTable(hTableDescriptor);
    }

删除表

    /**
     * 删除表
     *
     * @throws IOException
     */
    @Test
    public void delete_table() throws IOException {
        TableName tn = TableName.valueOf("student");
        //下线表
        admin.disableTable(tn);
        //删除表
        admin.deleteTable(tn);
    }

插入数据

    /**
     * 插入数据
     *
     * @throws IOException
     */
    @Test
    public void put_data() throws IOException {
        Table table = conn.getTable(TableName.valueOf("emp_table"));
        Put put = new Put(Bytes.toBytes("10002"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
        table.put(put);

        table.close();
    }

获取数据

    /**
     * 获取数据
     *
     * @throws IOException
     */
    @Test
    public void get_data() throws IOException {
        Table tab = conn.getTable(TableName.valueOf("emp_table"));
        Get get = new Get(Bytes.toBytes("10001"));
        get.addFamily(Bytes.toBytes("info"));
        get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        Result result = tab.get(get);
        for (Cell cell : result.rawCells()) {
            String family = Bytes.toString(CellUtil.cloneFamily(cell));
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(family + "," + qualifier + "," + value);
        }
        tab.close();
    }

创建命名空间

    /**
     * 创建命名空间
     */
    @Test
    public void create_namespace() {
        try {
            NamespaceDescriptor nsd = NamespaceDescriptor.create("senior6").build();
            admin.createNamespace(nsd);
        } catch (NamespaceExistException e) {
            System.out.println("已存在");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

释放资源

    /**
     * 释放资源
     *
     * @throws IOException
     */
    @After
    public void disabled() throws IOException {
        if (admin != null)
            admin.close();
        if (conn != null)
            conn.close();
    }

完整代码如下

package com.guantengyun.day0719;

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;

/**
 * HBase API
 * DDL操作
 */
public class HBaseClient {

    private static Configuration conf = null;
    private static Connection conn = null;
    private static Admin admin = null;

    /**
     * 初始化配置
     *
     * @throws IOException
     */
    @Before
    public void init() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hdp1:2181,hdp2:2181,hdp3:2181");
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
    }

    /**
     * 判断表是否存在
     *
     * @throws IOException
     */
    @Test
    public void exist_table() throws IOException {

        boolean b = admin.tableExists(TableName.valueOf("emp_table"));
        System.out.println(b);
    }

    /**
     * 创建表
     *
     * @throws IOException
     */
    @Test
    public void create_table() throws IOException {
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("emp_table"));
        hTableDescriptor.addFamily(new HColumnDescriptor("info"));
        hTableDescriptor.addFamily(new HColumnDescriptor("exp"));
        admin.createTable(hTableDescriptor);
    }

    /**
     * 删除表
     *
     * @throws IOException
     */
    @Test
    public void delete_table() throws IOException {
        TableName tn = TableName.valueOf("student");
        //下线表
        admin.disableTable(tn);
        //删除表
        admin.deleteTable(tn);
    }

    /**
     * 插入数据
     *
     * @throws IOException
     */
    @Test
    public void put_data() throws IOException {
        Table table = conn.getTable(TableName.valueOf("emp_table"));
        Put put = new Put(Bytes.toBytes("10002"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
        table.put(put);

        table.close();
    }

    /**
     * 获取数据
     *
     * @throws IOException
     */
    @Test
    public void get_data() throws IOException {
        Table tab = conn.getTable(TableName.valueOf("emp_table"));
        Get get = new Get(Bytes.toBytes("10001"));
        get.addFamily(Bytes.toBytes("info"));
        get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        Result result = tab.get(get);
        for (Cell cell : result.rawCells()) {
            String family = Bytes.toString(CellUtil.cloneFamily(cell));
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(family + "," + qualifier + "," + value);
        }
        tab.close();
    }

    /**
     * 创建命名空间
     */
    @Test
    public void create_namespace() {
        try {
            NamespaceDescriptor nsd = NamespaceDescriptor.create("senior6").build();
            admin.createNamespace(nsd);
        } catch (NamespaceExistException e) {
            System.out.println("已存在");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放资源
     *
     * @throws IOException
     */
    @After
    public void disabled() throws IOException {
        if (admin != null)
            admin.close();
        if (conn != null)
            conn.close();
    }
}



这篇关于HBase API快速入门-DDL操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程