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();
    }
}

上一篇:Runnable,Callable,Future,FutureTask的区别与联系


下一篇:Day13 异常机制.