Hbase工具类

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>2.4.6</version>
    </dependency>
</dependencies>

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseUtils {

    private static final String HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum";
    private static final String HBASE_ZOOKEEPER_PROPERTY_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
    private static final String TABLE_NAME = "table_name";

    private static Configuration configuration;
    private static Connection connection;

    static {
        configuration = HBaseConfiguration.create();
        configuration.set(HBASE_ZOOKEEPER_QUORUM, "localhost"); // 设置ZooKeeper地址
        configuration.set(HBASE_ZOOKEEPER_PROPERTY_CLIENT_PORT, "2181"); // 设置ZooKeeper端口号
        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Table对象
     * 
     * @param tableName
     * @return
     * @throws IOException
     */
    public static Table getTable(String tableName) throws IOException {
        return connection.getTable(TableName.valueOf(tableName));
    }

    /**
     * 新增一行数据
     * 
     * @param tableName
     * @param rowKey
     * @param columnFamily
     * @param column
     * @param value
     */
    public static void put(String tableName, String rowKey, String columnFamily, String column, String value)
            throws IOException {
        Table table = getTable(tableName);
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);
        table.close();
    }

    /**
     * 删除一行数据
     * 
     * @param tableName
     * @param rowKey
     */
    public static void delete(String tableName, String rowKey) throws IOException {
        Table table = getTable(tableName);
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        table.close();
    }

    /**
     * 获取一行数据
     * 
     * @param tableName
     * @param rowKey
     * @return
     * @throws IOException
     */
    public static Result get(String tableName, String rowKey) throws IOException {
        Table table = getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        table.close();
        return result;
    }

    /**
     * 获取多行数据
     * 
     * @param tableName
     * @param startRowKey
     * @param endRowKey
     * @return
     * @throws IOException
     */
    public static List<Result> scan(String tableName, String startRowKey, String endRowKey) throws IOException {
        Table table = getTable(tableName);
        Scan scan = new Scan(Bytes.toBytes(startRowKey), Bytes.toBytes(endRowKey));
        ResultScanner scanner = table.getScanner(scan);
        List<Result> resultList = new ArrayList<>();
        for (Result result : scanner) {
            resultList.add(result);
        }
        table.close();
        return resultList;
    }

    /**
     * 获取指定列族和列的单元格数据
     * 
     * @param result
     * @param columnFamily
     * @param column
     * @return
     */
    public static String getValue(Result result, String columnFamily, String column) {
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            if (Bytes.toString(CellUtil.cloneFamily(cell)).equals(columnFamily)
                    && Bytes.toString(CellUtil.cloneQualifier(cell)).equals(column)) {
                return Bytes.toString(CellUtil.cloneValue(cell));
            }
        }
        return null;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值