HBase学习笔记(2)—— API使用

对HBase中常用的API操作进行简单的介绍

对应HBase学习笔记(1)------ 知识点总结-CSDN博客中介绍的HBase Shell常用操作

更多用法请参考官网:Apache HBase ™ Reference Guide

依赖导入

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.4.11</version>
            <exclusions>
                <exclusion>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.el</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.1-b06</version>
        </dependency>
    </dependencies>

打包所用的插件:

XML 复制代码
 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

建立连接

java 复制代码
package com.why;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;

public class HBaseConnect2 {
    public static Connection connection = null;

    static {
        try {
            connection = ConnectionFactory.createConnection(); //使用配置文件中的参数hbase.zookeeper.quorum
            System.out.println(connection);
        } catch (IOException e) {
            System.out.println("连接创建失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭连接
     * @throws IOException
     */
    public static void closeConnection() throws IOException {
        if (connection != null)
        {
            connection.close();
        }
    }

}

DDL操作

java 复制代码
package com.why;
import org.apache.hadoop.hbase.HTableDescriptor;
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 {
    //获取连接

    private static Connection connection = HBaseConnect2.connection;

    /**
     * 创建命名空间
     * @param namespace 命名空间名称
     * @throws IOException
     */
    public static void createNamespace(String namespace) throws IOException {

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

        //创建命名空间builder
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);

//        builder.addConfiguration("user","why");

        try{
            admin.createNamespace(builder.build());
        }catch (IOException e)
        {
            System.out.println("命名空间已经存在");
            e.printStackTrace();
        }
        admin.close();

    }

    /**
     * 查看所有命名空间
     * @throws IOException
     */
    public static void listNamespaces() throws IOException {

        Admin admin = connection.getAdmin();

        try {
            String[] strings = admin.listNamespaces();

            System.out.println("命名空间如下:");

            for(String string : strings)
            {
                System.out.println(string);
            }
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        admin.close();

    }


    /**
     * 判断表格是否存在
     * @param namespace 命名空间
     * @param table 表格名称
     * @return
     * @throws IOException
     */
    public static boolean isTableExist(String namespace,String table) throws IOException {
        Admin admin = connection.getAdmin();

        boolean isExist = false;
        try {
            //通过tableExists方法判断表格是否存在
            isExist = admin.tableExists(TableName.valueOf(namespace,table));
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        admin.close();

        return isExist;
    }


    /**
     * 创建表格
     * @param namespace 命名空间
     * @param table 表格名称
     * @param columnFamilies 列族名称
     * @throws IOException
     */
    public static void createTable(String namespace , String table , String... columnFamilies) throws IOException {
        //判断是否至少有一个列族
        if(columnFamilies.length == 0)
        {
            System.out.println("至少要有一个列族");
            return;
        }

        //判断表格是否已经存在
        if(isTableExist(namespace,table))
        {
            System.out.println("表格已经存在");
            return;
        }

        Admin admin = connection.getAdmin();

        //创建表格描述的构建器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, table));

        //添加参数
        for(String columnFamily : columnFamilies)
        {
            //创建列族描述的构建器
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
            columnFamilyDescriptorBuilder.setMaxVersions(5); //添加最大版本数
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); //创建添加完参数的列族描述
        }

        try {
            admin.createTable(tableDescriptorBuilder.build());
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        admin.close();
    }

    /**
     * 查看所有表格
     * @throws IOException
     */
    public static void listTableNames() throws IOException {

        Admin admin = connection.getAdmin();

        try {
            TableName[] tableNames = admin.listTableNames();

            System.out.println("所有表格如下");
            for(TableName tableName : tableNames)
            {
                System.out.println(tableName.getNamespaceAsString() + ":" + tableName.getNameAsString());
            }
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        admin.close();

    }

    /**
     * 修改表格中某一个列族的版本号
     * @param namespace
     * @param table
     * @param columnFamily 列族
     * @param version 版本号
     */
    public static void modifyTable(String namespace , String table , String columnFamily , int version) throws IOException {

        if(!isTableExist(namespace,table))
        {
            System.out.println("表格不存在,无法修改");
            return;
        }
        Admin admin = connection.getAdmin();

        try {
            //获取表格描述
            TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, table));

            //创建新的表格描述构建器
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);

            //获取列族描述
            ColumnFamilyDescriptor columnFamily1 = descriptor.getColumnFamily(Bytes.toBytes(columnFamily));

            //创建新的列族描述构建器
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamily1);

            //设置参数
            columnFamilyDescriptorBuilder.setMaxVersions(version);

            //将新的列族描述添加到表格描述中
            tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());

            admin.modifyTable(tableDescriptorBuilder.build());

        }catch (IOException e)
        {
            e.printStackTrace();
        }
        admin.close();
    }

    /**
     *
     * @param namespace
     * @param table
     * @return true表示删除成功
     */
    public static boolean deleteTable(String namespace , String table) throws IOException {
        if(!isTableExist(namespace,table))
        {
            System.out.println("表格不存在,无法删除");
            return false;
        }

        Admin admin = connection.getAdmin();

        try {
            TableName tableName = TableName.valueOf(namespace, table);
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        admin.close();
        return true;
    }


}

DML操作

java 复制代码
package com.why;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnValueFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class HBaseDML {

    private static Connection connection = HBaseConnect2.connection;

    /**
     * 插入数据
     *
     * @param namespace
     * @param table
     * @param rowKey
     * @param columnFamily
     * @param column
     * @param value
     * @throws IOException
     */
    public static void insert(String namespace, String table, String rowKey, String columnFamily, String column, String value) throws IOException {

        //首先获取表格table
        Table table1 = connection.getTable(TableName.valueOf(namespace, table));
        System.out.println("表格创建成功");
        //创建put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        System.out.println("put对象创建成功");
        //向put对象中添加数据
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        try {
            table1.put(put);
            System.out.println("数据插入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

        table1.close();
    }


    /**
     * 读取数据
     *
     * @param namespace
     * @param table
     * @param rowKey
     * @param columnFamily
     * @param column
     * @throws IOException
     */
    public static void get(String namespace, String table, String rowKey, String columnFamily, String column) throws IOException {
        //获取table
        Table table1 = connection.getTable(TableName.valueOf(namespace, table));

        //获取get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        //设置读取某一列的数据(如果不设置的话则读取所有数据)
        get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));

        try {
            //调用get方法读取数据
            Result result = table1.get(get);
            //获取到所有的cell
            Cell[] cells = result.rawCells();
            //打印value
            for (Cell cell : cells) {
                String value = new String(CellUtil.cloneValue(cell));
                System.out.println(value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        table1.close();
    }


    /**
     * 扫描表
     *
     * @param namespace
     * @param table
     * @param startRow
     * @param stopRow
     * @throws IOException
     */
    public static void scan(String namespace, String table, String startRow, String stopRow) throws IOException {
        //获取table
        Table table1 = connection.getTable(TableName.valueOf(namespace, table));

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

        //添加扫描的起止rowKey
        scan.withStartRow(Bytes.toBytes(startRow));
        scan.withStopRow(Bytes.toBytes(stopRow));

        try {
            ResultScanner scanner = table1.getScanner(scan);

            for (Result result : scanner) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    System.out.print(new
                            String(CellUtil.cloneRow(cell)) + "-" + new
                            String(CellUtil.cloneFamily(cell)) + "-" + new
                            String(CellUtil.cloneQualifier(cell)) + "-" + new
                            String(CellUtil.cloneValue(cell)) + "\t");
                }
                System.out.println();
                ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        table1.close();
    }


    /**
     * 扫描表(有过滤器)
     *
     * @param namespace
     * @param table
     * @param startRow
     * @param stopRow
     * @param columnFamily
     * @param column
     * @param value
     */
    public static void scanWithFilter(String namespace, String table, String startRow, String stopRow, String columnFamily, String column, String value) throws IOException {

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

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

        //添加扫描的起止rowKey
        scan.withStartRow(Bytes.toBytes(startRow));
        scan.withStopRow(Bytes.toBytes(stopRow));

        //创建过滤器列表
        FilterList filterList = new FilterList();

        //创建列过滤器,作用:只保留当前列的数据
        ColumnValueFilter columnValueFilter = new ColumnValueFilter(
                Bytes.toBytes(columnFamily),
                Bytes.toBytes(column),
                CompareOperator.EQUAL,
                Bytes.toBytes(value));

//        // (2) 结果保留整行数据
//        // 结果同时会保留没有当前列的数据
//        SingleColumnValueFilter singleColumnValueFilter = new
//                SingleColumnValueFilter(
//                // 列族名称
//                Bytes.toBytes(columnFamily),
//                // 列名
//                Bytes.toBytes(column),
//                // 比较关系
//                CompareOperator.EQUAL,
//                // 值
//                Bytes.toBytes(value)
//        );

        filterList.addFilter(columnValueFilter);
        scan.setFilter(filterList);

        try {
            ResultScanner scanner = table1.getScanner(scan);

            for (Result result : scanner) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    System.out.print(new
                            String(CellUtil.cloneRow(cell)) + "-" + new
                            String(CellUtil.cloneFamily(cell)) + "-" + new
                            String(CellUtil.cloneQualifier(cell)) + "-" + new
                            String(CellUtil.cloneValue(cell)) + "\t");
                }
                System.out.println();
                ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        table1.close();
    }

    /**
     * 删除数据
     * @param nameSpace
     * @param table
     * @param rowKey
     * @param columnFamily
     * @param column
     * @throws IOException
     */
    public static void delete(String nameSpace, String table, String rowKey, String columnFamily, String column) throws IOException {
        //获取 table
        Table table1 = connection.getTable(TableName.valueOf(nameSpace, table));

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

        //添加删除信息
        //删除单个版本(默认最新)
        delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));

//        //删除所有版本
//        delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(column));

        //删除列族
        delete.addFamily(Bytes.toBytes(columnFamily));

        try {
            table1.delete(delete);
        }catch (IOException e)
        {
            e.printStackTrace();
        }

        table1.close();
    }
}

说明:本学习笔记根据基于尚硅谷课程进行整理,课程链接:hbase

未完待续~

相关推荐
青云交18 小时前
Java 大视界 -- 基于 Java 的大数据分布式存储在智慧城市时空大数据管理与应用中的创新实践(408)
java·hdfs·flink·智慧城市·hbase·java 分布式存储·时空大数据
攻城狮7号19 小时前
2025时序数据库选型,以IoTDB为主从架构基因到AI赋能来解析
大数据·物联网·时序数据库·apache iotdb·时序大模型·sql mcp·ainode
paid槮2 天前
图像形态学
时序数据库
TDengine (老段)2 天前
TDengine 时间函数 WEEKDAY() 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
TDengine (老段)2 天前
从 ETL 到 Agentic AI:工业数据管理变革与 TDengine IDMP 的治理之道
数据库·数据仓库·人工智能·物联网·时序数据库·etl·tdengine
时序数据说3 天前
时序数据库IoTDB的核心优势
大数据·数据库·物联网·开源·时序数据库·iotdb
TDengine (老段)3 天前
中国时序数据库行业市场概览、投资热点及发展趋势预测报告
数据库·物联网·时序数据库·iot·tdengine
TDengine (老段)3 天前
TDengine 时间函数 TIMETRUNCATE 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
TDengine (老段)3 天前
TDengine 时间函数 TODAY() 用户手册
大数据·数据库·物联网·oracle·时序数据库·tdengine·涛思数据
只因在人海中多看了你一眼4 天前
B.50.10.06-NoSQL数据库与电商应用
数据库·nosql