【HBase】——Java API

1 环境

1.1 依赖

<dependencies>
    <dependency>
        <groupId>org.apache.hbasegroupId>
        <artifactId>hbase-clientartifactId>
        <version>2.4.11version>
    dependency>
dependencies>

1.2 配置文件hbase-site.xml



<configuration>
  
  <property>
    <name>hbase.zookeeper.quorumname>
    <value>hadoop102:2181,hadoop103:2181,hadoop104:2181value>
  property>

configuration>

2 创建连接

package com.hbase;

import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class HBaseThreadConnection {

    public static Connection connection = null;

    static {
        try {
            //1. 创建连接,默认使用同步链接,采用的读取本地hbase-site.xml文件形式读取数据
            connection = ConnectionFactory.createConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void closeConnection() throws IOException {
        //判断连接是否为空
        if (connection != null) {
            connection.close();
        }
    }

    public static void main(String[] args) throws IOException {
        Connection connection1 = HBaseThreadConnection.connection;
        System.out.println(connection1);
        HBaseThreadConnection.closeConnection();
    }

}

3 DDL API

package com.hbase;

import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseDDL {
    public static Connection connection = HBaseThreadConnection.connection;


    /**
     * 创建命名空间
     *
     * @param namespace 命名空间
     */
    public static void createNamespace(String namespace) throws IOException {
        //1. 获取admin
        // admin 的连接是轻量级的 不是线程安全的 不推荐池化或者缓存这个连接
        Admin admin = connection.getAdmin();

        //2. 调用方法创建命名空间
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
        //添加命名空间描述
        builder.addConfiguration("user", "gaohechao");
        try {
            admin.createNamespace(builder.build());
        } catch (IOException e) {
            System.out.println("Namespace already exists");
            e.printStackTrace();
        }
        //3. 关闭admin
        admin.close();

    }

    /**
     * 判断表是否存在
     *
     * @param namespace 命名空间
     * @param tableName 表名
     * @return true : 存在 , false: 不存在
     */
    public static boolean isTableExists(String namespace, String tableName) throws IOException {
        //1. 获取admin
        // admin 的连接是轻量级的 不是线程安全的 不推荐池化或者缓存这个连接
        Admin admin = connection.getAdmin();
        boolean b = false;
        try {
            b = admin.tableExists(TableName.valueOf(namespace, tableName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3. 关闭admin
        admin.close();
        return b;
    }


    /**
     * 创建表格
     *
     * @param namespace      命名空间
     * @param tableName      表名
     * @param columnFamilies 列族名称,可以多个
     */
    public static void createTable(String namespace, String tableName, String... columnFamilies) throws IOException {


        if (columnFamilies.length == 0) {
            System.out.println("列族数量不能为0");
            return;
        }

        if (isTableExists(namespace, tableName)) {
            System.out.println("表已存在");
            return;
        }
        //1. 获取admin
        // admin 的连接是轻量级的 不是线程安全的 不推荐池化或者缓存这个连接
        Admin admin = connection.getAdmin();

        //2. 创建表格描述的builder
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName));
        //2.1 添加参数
        for (String columnFamily : columnFamilies) {
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
            //2.2 添加当前列族的参数
            columnFamilyDescriptorBuilder.setMaxVersions(6);
            //2.3 添加列族
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        }
        try {
            admin.createTable(tableDescriptorBuilder.build());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3. 关闭admin
        admin.close();
    }


    /**
     * 修改表格
     *
     * @param namespace    命名空间
     * @param tableName    表名
     * @param columnFamily 列族名称
     */
    public static void modifyTable(String namespace, String tableName, String columnFamily, int version) throws IOException {


        if (!isTableExists(namespace, tableName)) {
            System.out.println("表不存在");
            return;
        }

        //1. 获取admin
        // admin 的连接是轻量级的 不是线程安全的 不推荐池化或者缓存这个连接
        Admin admin = connection.getAdmin();

        //2. 获取之前的表格描述
        TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, tableName));
        //2.1创建之前的表格描述的builder
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
        //2.2 创建之前的列族描述的builde,如果不存在,则需要创建
        ColumnFamilyDescriptor columnFamilyOld = descriptor.getColumnFamily(Bytes.toBytes(columnFamily));
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyOld);
        //2.3 添加参数
        columnFamilyDescriptorBuilder.setMaxVersions(version);
        //2.4 添加列族
        tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
        try {
            admin.modifyTable(tableDescriptorBuilder.build());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3. 关闭admin
        admin.close();


    }

    /**
     * 删除表
     *
     * @param namespace 命名空间
     * @param tableName 表名
     * @return true : 成功 , false: 失败
     */
    public static boolean deleteTable(String namespace, String tableName) throws IOException {

        if (!isTableExists(namespace, tableName)) {
            System.out.println("表不存在");
            return false;
        }


        //1. 获取admin
        // admin 的连接是轻量级的 不是线程安全的 不推荐池化或者缓存这个连接
        Admin admin = connection.getAdmin();

        // 3. 调用相关的方法删除表格
        try {
            // HBase 删除表格之前 一定要先标记表格为不可以
            TableName tableName1 = TableName.valueOf(namespace, tableName);
            admin.disableTable(tableName1);
            admin.deleteTable(tableName1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3. 关闭admin
        admin.close();

        return true;
    }

    public static void main(String[] args) throws IOException {
        //createNamespace("test1_namespace");
        //System.out.println(isTableExists("test1_namespace","test_tableName"));
        //System.out.println(isTableExists("test_namespace","student"));
        //createTable("test_namespace","student1", "info","msg");
        //modifyTable("test_namespace", "student1", "msg", 7);
        //deleteTable("test_namespace","student1");

        connection.close();
    }
}

4 DML API

package com.hbase;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


public class HBaseDML {

    public static Connection connection = HBaseThreadConnection.connection;

    /**
     * 写入数据
     *
     * @param namespace    命名空间
     * @param tableName    表名
     * @param rowKey       主键
     * @param columnFamily 列族名称
     * @param columnName   列名
     * @param value        值
     */
    public static void putCell(String namespace, String tableName, String rowKey, String columnFamily, String columnName, String value) throws IOException {

        //1. 获取table
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        //2. 创建put对象
        Put put = new Put(Bytes.toBytes(rowKey));

        //3. 添加put参数
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes(value));

        //4. 调用方法插入数据
        try {
            table.put(put);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //5. 关闭table
        table.close();

    }

    /**
     * 读取数据
     *
     * @param namespace    命名空间
     * @param tableName    表名
     * @param rowKey       主键
     * @param columnFamily 列族名称
     * @param columnName   列名
     */
    public static void getCells(String namespace, String tableName, String rowKey, String columnFamily, String columnName) throws IOException {


        //1. 获取table
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        //2. 创建get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        //3. 添加get参数,如果不添加,则是读取一整行的数据
        get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));

        //4. 设置读取数据的版本
        //最大版本
        get.readAllVersions();

        //指定版本
        //get.readVersions(1);
        //5. 调用方法读取数据
        try {
            Result result = table.get(get);

            //6. 处理数据
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {

                byte[] bytes = CellUtil.cloneValue(cell);
                String s = new String(bytes);
                System.out.println(s);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //6. 关闭table
        table.close();
    }

    /**
     * 扫描数据
     *
     * @param namespace 命名空间
     * @param tableName 表名
     * @param startRow  开始行
     * @param stopRow   结束行
     */
    public static void scanRows(String namespace, String tableName, String startRow, String stopRow) throws IOException {
        //1. 获取table
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        //2. 创建scan对象,
        Scan scan = new Scan();

        //3. 添加scan 开始结束行
        // 设置开始行,结束行,如果不设置,则扫描全表
        //包含startRow行
        scan.withStartRow(Bytes.toBytes(startRow));
        //默认不包含stopRow行
        scan.withStopRow(Bytes.toBytes(stopRow));

        //4. 调用方法读取数据
        try {
            ResultScanner resultScanner = table.getScanner(scan);
            //6. 处理数据
            for (Result result : resultScanner) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    System.out.println("row:" + new String(CellUtil.cloneRow(cell)) + "," +
                            "family:" + new String(CellUtil.cloneFamily(cell)) + "," +
                            "qualifier:" + new String(CellUtil.cloneQualifier(cell)) + "," +
                            "value:" + new String(CellUtil.cloneValue(cell)) + "\t");

                }
                System.out.println("---------------------");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //5. 关闭table
        table.close();

    }

    /**
     * 过滤扫描
     *
     * @param namespace    命名空间
     * @param tableName    表名
     * @param startRow     开始行
     * @param stopRow      结束行
     * @param columnFamily 列族
     * @param columnName   列名
     * @param value        值
     */
    public static void filterScan(String namespace, String tableName, String startRow, String stopRow, String columnFamily, String columnName, String value) throws IOException {

        //1. 获取table
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        //2. 创建scan对象,
        Scan scan = new Scan();

        //3. 添加scan 开始结束行
        // 设置开始行,结束行,如果不设置,则扫描全表
        //包含startRow行
        scan.withStartRow(Bytes.toBytes(startRow));
        //默认不包含stopRow行
        scan.withStopRow(Bytes.toBytes(stopRow));

        //4. 添加过滤条件
        FilterList filterList = new FilterList();

        //只保留当前列的数据
        //ColumnValueFilter columnValueFilter = new ColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), CompareOperator.EQUAL, Bytes.toBytes(value));
        //scan.setFilter(columnValueFilter);

        //结果包含整行数据,会保留没有当前列的数据
        //SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), CompareOperator.EQUAL, Bytes.toBytes(value));
        //scan.setFilter(singleColumnValueFilter);

        //5. 调用方法读取数据
        try {
            ResultScanner resultScanner = table.getScanner(scan);
            //6. 处理数据
            for (Result result : resultScanner) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    System.out.println("row:" + new String(CellUtil.cloneRow(cell)) + "," +
                            "family:" + new String(CellUtil.cloneFamily(cell)) + "," +
                            "qualifier:" + new String(CellUtil.cloneQualifier(cell)) + "," +
                            "value:" + new String(CellUtil.cloneValue(cell)) + "\t");

                }
                System.out.println("---------------------");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //6. 关闭table
        table.close();
    }

    /**
     * @param namespace    命名空间
     * @param tableName    表名
     * @param rowKey       主键
     * @param columnFamily 列族
     * @param columnName   列名
     */
    public static void deleteColumn(String namespace, String tableName, String rowKey, String columnFamily, String columnName) throws IOException {

        //1. 获取table
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        //2. 创建delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));

        //3. 添加参数,
        // 删除一个版本
        //delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));

        // 删除全部版本
        delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));

        //5. 调用方法删除数据
        try {
            table.delete(delete);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //6. 关闭table
        table.close();

    }

    public static void main(String[] args) throws IOException {
        //putCell("test_namespace", "student", "1001", "info", "data", "zhagnsan");
        //getCells("test_namespace", "student", "1001", "info", "name");
        //scanRows("test_namespace", "student", "1001", "1006");
        //filterScan("test_namespace", "student", "1001", "1009", "info", "name", "zhagnsan");
        //deleteColumn("test_namespace", "student", "1001", "info", "name");
        connection.close();
    }
}

你可能感兴趣的:(hbase,java,数据库)