Hbase工具类 V2.0.5

Posted 徐一闪_BigData

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hbase工具类 V2.0.5相关的知识,希望对你有一定的参考价值。

提供了

1.创建链接

2.创建命名空间

3.判断表存在与否

4.创建表

5.修改表

6.删除表

7.插入数据

8.查询数据

9.扫描数据

10.删除数据 

Maven添加如下依赖

<dependencies>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-server</artifactId>
                <version>2.0.5</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.glassfish</groupId>
                        <artifactId>javax.el</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>2.0.5</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish</groupId>
                <artifactId>javax.el</artifactId>
                <version>3.0.1-b06</version>
            </dependency>
    </dependencies>

 HbaseUtils.java

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseUtils {
    /*
    * @author 70493
    * @Description  使用前必看!!!!!
    * 修改下方value为自己的zookeeper组,也可以只写一台,但是容错率低
    * @Date 19:34 2021/10/29
    **/
    private static String value="hadoop102,hadoop103,hadoop104";
    private static Connection connection;
    static{
        try {
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", value);
            connection = ConnectionFactory.createConnection(conf);
            System.out.println("连接成功");
        }catch (Exception e) {
            System.out.println(e);
        }
    }
    /*
    * @author 70493
    * @Description 创建命名空间
    * @Date 19:34 2021/10/29
    * @Param namespace:命名空间名
    * @return boolean
    **/
    public static Boolean createNameSpace(String namespace){
        boolean flag = false;
        try {
            Admin admin = connection.getAdmin();
            NamespaceDescriptor db = NamespaceDescriptor.create(namespace).build();
            admin.createNamespace(db);
            flag = true;
        }catch (Exception e) {
            System.out.println(e);
        }
        return flag;
    }
    /*
    * @author 70493
    * @Description 检查表是否存在
    * @Date 19:34 2021/10/29
    * @Param namespace:命名空间名
    * @return boolean
    **/
    public static boolean isTableExist(String namespace,String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        boolean flag = admin.tableExists(TableName.valueOf(namespace, tableName));
        admin.close();
        return flag;
    }
    /*
    * @author 70493
    * @Description 创建表
    * @Date 19:34 2021/10/29
    * @Param namespace:命名空间名 tableName:表名 familyNames:列族名 默认维护版本为2
    * @return
    **/
    public static boolean createTable(String namespace,String tableName,String... familyNames) throws IOException {
        if (familyNames.length <= 0) {
            System.out.println("请输入列族数");
            return false;
        }
        if (isTableExist(namespace,tableName)) {
            System.out.println("表格已存在");
            return false;
        }
        Admin admin = connection.getAdmin();
        TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName));
        for (String familyName : familyNames) {
            ColumnFamilyDescriptorBuilder builder1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName));
            builder1.setMaxVersions(2);
            builder.setColumnFamily(builder1.build());
        }
        admin.createTable(builder.build());
        admin.close();
        return true;
    }
    /*
    * @author 70493
    * @Description 修改表
    * @Date 20:00 2021/10/29
    * @Param
    * @return
    **/
    public static boolean modifyTable(String nameSpace,String tableName,String familyName,int version) throws IOException {
        Admin admin = connection.getAdmin();
        TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(nameSpace, tableName));
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName)).setMaxVersions(version);
        tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
        admin.modifyTable(tableDescriptorBuilder.build());
        admin.close();
        return true;
    }
    /*
    * @author 70493
    * @Description 删除表
    * @Date 20:02 2021/10/29
    * @Param
    * @return
    **/
    public static boolean deleteTable(String nameSpace,String tableName) throws IOException {
        if (!isTableExist(nameSpace, tableName)){
            System.out.println("表格不存在");
            return false;
        }
        Admin admin = connection.getAdmin();
        admin.disableTable(TableName.valueOf(nameSpace, tableName));
        admin.deleteTable(TableName.valueOf(nameSpace, tableName));
        admin.close();
        return true;
    }
    /*
    * @author 70493
    * @Description 插入数据
    * @Date 20:02 2021/10/29
    * @Param
    * @return
    **/
    public static void putCell(String nameSpace,String tableName,String rowKey,String family,String column,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        table.put(put);
        table.close();
    }
    /*
    * @author 70493
    * @Description 获取数据
    * @Date 20:03 2021/10/29
    * @Param
    * @return
    **/
    public static String getCell(String nameSpace,String tableName,String rowKey,String family,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        String value = "";
        for (Cell cell : cells) {
            value += Bytes.toString(CellUtil.cloneValue(cell)) + "-";
        }
        table.close();
        return value;
    }
    /*
    * @author 70493
    * @Description 扫描行
    * @Date 20:04 2021/10/29
    * @Param
    * @return
    **/
    public static List<String> scanRows(String nameSpace, String tableName, String startRow, String stopRow) throws IOException {
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
        Scan scan = new Scan().withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));
        ResultScanner scanner = table.getScanner(scan);
        ArrayList<String> arrayList = new ArrayList<>();
        for (Result result : scanner) {
            arrayList.add(Bytes.toString(result.value()));
        }
        // 5.关闭资源
        scanner.close();
        table.close();
        return arrayList;
    }
    /*
    * @author 70493
    * @Description 删除数据
    * @Date 20:05 2021/10/29
    * @Param
    * @return
    **/
    public static void deleteColumn(String nameSpace, String tableName, String rowKey, String family, String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumns(Bytes.toBytes(family), Bytes.toBytes(column));
        table.delete(delete);
        table.close();
    }

    public static void close(){
        try {
            connection.close();
        }catch (Exception e) {
            System.out.println("关闭连接失败");
        }
    }
}

以上是关于Hbase工具类 V2.0.5的主要内容,如果未能解决你的问题,请参考以下文章

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

SpringBoot 封装 HBase 操作工具类

hbase持有者工具类

HBase编程 API入门之工具Bytes类

成人高考宝典 v2.0.5下载入口http://www.pc6.com/az/996616.html

hbase构建二级索引的实现方式都有哪些