Hbase操作工具類-maxVersion設置爲1,那麼相同數據在時間戳大於等於原數據時間戳時,數據會被覆蓋

Hbase依賴

<!-- 包含HbaseClient和聚合工具類 -->
 <dependency>
     <groupId>org.apache.hbase</groupId>
     <artifactId>hbase-endpoint</artifactId>
     <version>2.2.2</version>
</dependency>

配置文件

hbase:
  config:
    properties:
      hbase.zookeeper.quorum : hadoop-master:2181,hadoop-slave01:2181,hadoop-slave02:2181

配置Bean

package com.aimsphm.nuclear.hbase.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Package: com.aimsphm.nuclear.hbase.config
 * @Description: <hbase配置信息>
 * @Author: MILLA
 * @CreateDate: 2020/3/5 13:16
 * @UpdateUser: MILLA
 * @UpdateDate: 2020/3/5 13:16
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Configuration
@ConfigurationProperties(prefix = HbaseConfig.CONF_PREFIX)
@Slf4j
public class HbaseConfig {
    public static final String CONF_PREFIX = "hbase.config";

    private Map<String, String> properties;

    public Map<String, String> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }

    @Bean
    public Connection connection() throws IOException {
        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        Assert.isTrue(properties != null && !properties.isEmpty(), "Hbase config can not be null");

        for (Map.Entry<String, String> confEntry : properties.entrySet()) {
            conf.set(confEntry.getKey(), confEntry.getValue());
        }
        System.setProperty("zookeeper.sasl.client", "false");//消除sasl認證
        ExecutorService executor = Executors.newScheduledThreadPool(20);
        Connection connection = ConnectionFactory.createConnection(conf, executor);
        return connection;
    }
}

HbaseUtils工具類

package com.aimsphm.nuclear.hbase.utill;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.*;

import static org.apache.hadoop.hbase.CompareOperator.EQUAL;

/**
 * @Package: com.aimsphm.nuclear.hbase.utill
 * @Description: <Hbase操作工具類>
 * @Author: MILLA
 * @CreateDate: 2020/3/5 13:40
 * @UpdateUser: MILLA
 * @UpdateDate: 2020/3/5 13:40
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Slf4j
@Component
public class HbaseUtils {
    @Autowired
    private Connection connection;

    /**
     * 創建表
     *
     * @param tableName 表名
     * @param families  列族
     */
    public void createTable(String tableName, List<String> families, Compression.Algorithm type) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Admin admin = connection.getAdmin()) {
            Assert.isTrue(!admin.tableExists(name), "This table already exists");
            TableDescriptorBuilder desc = TableDescriptorBuilder.newBuilder(name);
            for (String cf : families) {
                ColumnFamilyDescriptorBuilder builder = ColumnFamilyDescriptorBuilder.newBuilder(cf.getBytes());
                if (type != null) {
                    builder.setCompressionType(type);
                }
                builder.setMaxVersions(1);//指定最大版本1,值會被覆蓋
                desc.setColumnFamily(builder.build());
            }
            admin.createTable(desc.build());
        } catch (IOException e) {
            throw e;
        }
    }

    public void addFamily2Table(String tableName, List<String> families, Compression.Algorithm type) throws IOException {
        TableName table = TableName.valueOf(tableName);
        try (Admin admin = connection.getAdmin()) {
            Assert.isTrue(admin.tableExists(table), "This table not exists");
            for (String cf : families) {
                ColumnFamilyDescriptorBuilder builder = ColumnFamilyDescriptorBuilder.newBuilder(cf.getBytes());
                if (type != null) {
                    builder.setCompressionType(type);
                }
                builder.setMaxVersions(1);//指定最大版本1,值會被覆蓋(當時間戳大於等於已存數據的時間戳時會覆蓋原來的值)
                admin.addColumnFamily(table, builder.build());
            }
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 刪除表操作
     *
     * @param tableName
     */
    public void deleteTable(String tableName) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Admin admin = connection.getAdmin()) {
            Assert.isTrue(admin.tableExists(name), "This table not exists");
            admin.disableTable(name);
            admin.deleteTable(name);
        } catch (IOException e) {
            throw e;
        }

    }

    /**
     * 插入記錄(單行單列族-單列單值-指定時間戳)
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     * @param column    列名
     * @param value     值
     * @param timestamp 值對應的時間戳
     */
    public void insertDouble(String tableName, String rowKey, String family, String column, Double value, Long timestamp) throws IOException {
        insertObject(tableName, rowKey, family, column, value, timestamp);
    }

    /**
     * 插入記錄(單行單列族-單列單值-不指定時間戳)
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     * @param column    列名
     * @param value     值
     */
    public void insertDouble(String tableName, String rowKey, String family, String column, Double value) throws IOException {
        insertObject(tableName, rowKey, family, column, value, null);
    }

    /**
     * 插入記錄(單行單列族-單列單值)
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     * @param column    列名
     * @param value     值
     * @param timestamp 值對應的時間戳
     */
    public void insertObject(String tableName, String rowKey, String family, String column, Object value, Long timestamp) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Put put = new Put(Bytes.toBytes(rowKey));
            if (timestamp != null) {
                put.setTimestamp(timestamp);
            }
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), getBytes(value));
            table.put(put);
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 插入記錄(單行單列族-多列多值)
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     * @param columns   列名(數組)
     * @param values    值(數組)(且需要和列一一對應)
     */
    public void insertDoubles(String tableName, String rowKey, String family, List<String> columns, List<Double> values) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Put put = new Put(Bytes.toBytes(rowKey));
            for (int i = 0; i < columns.size(); i++) {
                put.addColumn(Bytes.toBytes(family), Bytes.toBytes(columns.get(i)), Bytes.toBytes(values.get(i)));
                table.put(put);
            }
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 查找一行記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     */
    public List<Map<String, Object>> selectData(String tableName, String rowKey) throws IOException {
        return this.selectData(tableName, rowKey, null);
    }

    /**
     * 查找一行記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族
     */
    public List<Map<String, Object>> selectData(String tableName, String rowKey, String family) throws IOException {
        return this.selectData(tableName, rowKey, family, null);
    }

    /**
     * 查找一行記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族
     * @param qualifier 列名
     */
    public List<Map<String, Object>> selectData(String tableName, String rowKey, String family, String qualifier) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Get g = new Get(rowKey.getBytes());
            if (!StringUtils.isEmpty(family)) {
                g.addFamily(Bytes.toBytes(family));
                if (!StringUtils.isEmpty(qualifier)) {
                    g.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
                }
            }
            Result rs = table.get(g);
            Map<String, Set<String>> familyMap = Maps.newHashMap();
            Map<String, List<Object>> qualifierMap = Maps.newHashMap();
            assembleCellData(familyMap, qualifierMap, rs);
            return assembleReturnData(qualifierMap, familyMap.entrySet());
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * @param tableName   表格名稱
     * @param rowKeyStart 開始rowKey
     * @param rowKeyEnd   結束rowKey
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> selectDataList(String tableName, String rowKeyStart, String rowKeyEnd) throws IOException {
        return this.selectDataList(tableName, rowKeyStart, rowKeyEnd, null);
    }

    /**
     * @param tableName   表格名稱
     * @param rowKeyStart 開始rowKey
     * @param rowKeyEnd   結束rowKey
     * @param family      列族
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> selectDataList(String tableName, String rowKeyStart, String rowKeyEnd, String family) throws IOException {
        return this.selectDataList(tableName, rowKeyStart, rowKeyEnd, family, null);
    }

    /**
     * @param tableName   表格名稱
     * @param rowKeyStart 開始rowKey
     * @param rowKeyEnd   結束rowKey
     * @param family      列族
     * @param qualifier   列
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> selectDataList(String tableName, String rowKeyStart, String rowKeyEnd, String family, String qualifier) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Scan scan = new Scan();
            boolean isHasFamily = StringUtils.isEmpty(family);
            boolean isHasQualifier = StringUtils.isEmpty(qualifier);
            if (!isHasFamily) {
                scan.addFamily(Bytes.toBytes(family));
                if (!isHasQualifier) {
                    scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
                }
            }
            scan.withStartRow(Bytes.toBytes(rowKeyStart));
            scan.withStopRow(Bytes.toBytes(rowKeyEnd));
            ResultScanner scanner = table.getScanner(scan);
            Map<String, Set<String>> familyMap = Maps.newHashMap();
            Map<String, List<Object>> qualifierMap = Maps.newHashMap();
            for (Result rs : scanner) {
                assembleCellData(familyMap, qualifierMap, rs);
            }
            return assembleReturnData(qualifierMap, familyMap.entrySet());
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * @param qualifierMap 列集合
     * @param entries      列族集合
     * @return 經過轉換的返回值
     */
    private List<Map<String, Object>> assembleReturnData(Map<String, List<Object>> qualifierMap, Set<Map.Entry<String, Set<String>>> entries) {
        List<Map<String, Object>> families = Lists.newArrayList();
        for (Iterator<Map.Entry<String, Set<String>>> it = entries.iterator(); it.hasNext(); ) {
            Map.Entry<String, Set<String>> next = it.next();
            String key = next.getKey();
            Set<String> value = next.getValue();
            Map<String, Object> familyData = Maps.newHashMap();
            List<Map<String, Object>> qualifiers = Lists.newArrayList();
            familyData.put("family", key);
            familyData.put("qualifiers", qualifiers);
            families.add(familyData);
            for (String cell : value) {
                Map<String, Object> qualifierData = Maps.newHashMap();
                qualifierData.put("qualifier", cell);
                qualifierData.put("times", qualifierMap.get(cell + "Times"));
                qualifierData.put("values", qualifierMap.get(cell + "Values"));
                qualifiers.add(qualifierData);
            }
        }
        return families;
    }

    /**
     * 組裝數據
     *
     * @param familyMap    列族集合
     * @param qualifierMap 列集合
     * @param rs
     */
    private void assembleCellData(Map<String, Set<String>> familyMap, Map<String, List<Object>> qualifierMap, Result rs) {
        if (rs.size() == 0) {
            return;
        }
        for (Cell cell : rs.listCells()) {
            String family = Bytes.toString(CellUtil.cloneFamily(cell));
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
            double value = Bytes.toDouble(CellUtil.cloneValue(cell));
            long timestamp = cell.getTimestamp();

            if (!qualifierMap.containsKey(qualifier + "Times")) {
                qualifierMap.put(qualifier + "Times", Lists.newArrayList());
                qualifierMap.put(qualifier + "Values", Lists.newArrayList());
            }
            if (!familyMap.containsKey(family)) {
                familyMap.put(family, Sets.newHashSet());
            }
            familyMap.get(family).add(qualifier);
            qualifierMap.get(qualifier + "Times").add(timestamp);
            qualifierMap.get(qualifier + "Values").add(value);
        }
    }

    /**
     * 刪除一行記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     */
    public void deleteRow(String tableName, String rowKey) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Delete d = new Delete(rowKey.getBytes());
            table.delete(d);
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 刪除單行單列族記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     */
    public void deleteFamily(String tableName, String rowKey, String family) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Delete d = new Delete(rowKey.getBytes()).addFamily(Bytes.toBytes(family));
            table.delete(d);
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 刪除單行單列族單列記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     * @param qualifier 列名
     */
    public void deleteColumn(String tableName, String rowKey, String family, String qualifier) throws IOException {
        TableName name = TableName.valueOf(tableName);
        try (Table table = connection.getTable(name)) {
            Delete d = new Delete(rowKey.getBytes()).addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
            table.delete(d);
        } catch (IOException e) {
            throw e;
        }
    }


    /**
     * 查找單行單列族單列記錄
     *
     * @param tableName 表名
     * @param rowKey    行名
     * @param family    列族名
     * @param column    列名
     * @return
     */
    public Object selectValue(String tableName, String rowKey, String family, String column) throws IOException {
        TableName name = TableName.valueOf(tableName);
        Table table = connection.getTable(name);
        Get g = new Get(rowKey.getBytes());
        g.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
        Result rs = table.get(g);
        return getObject(rs.value(), Double.class);
    }

    /**
     * 根據rowKey關鍵字查詢報告記錄
     *
     * @param tableName
     * @param rowKeyword
     * @return
     */
    public List scanReportDataByRowKeyword(String tableName, String rowKeyword) throws IOException {
        List list = new ArrayList<>();

        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();

        //添加行鍵過濾器,根據關鍵字匹配
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(rowKeyword));
        scan.setFilter(rowFilter);

        ResultScanner scanner = table.getScanner(scan);
        try {
            for (Result result : scanner) {
                //TODO 此處根據業務來自定義實現
                list.add(null);
            }
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return list;
    }

    /**
     * 根據rowKey關鍵字和時間戳範圍查詢報告記錄
     *
     * @param tableName
     * @param rowKeyword
     * @return
     */
    public List scanReportDataByRowKeywordTimestamp(String tableName, String rowKeyword, Long minStamp, Long maxStamp) throws IOException {
        ArrayList list = new ArrayList<>();

        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        //添加scan的時間範圍
        scan.setTimeRange(minStamp, maxStamp);

        RowFilter rowFilter = new RowFilter(EQUAL, new SubstringComparator(rowKeyword));
        scan.setFilter(rowFilter);

        ResultScanner scanner = table.getScanner(scan);
        try {
            for (Result result : scanner) {
                //TODO 此處根據業務來自定義實現
                list.add(null);
            }
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return list;
    }


    /**
     * 利用協處理器進行全表count統計
     *
     * @param tableName
     */
    public Long countRowsWithCoprocessor(String tableName) throws Throwable {
        TableName name = TableName.valueOf(tableName);
        Admin admin = connection.getAdmin();
        HTableDescriptor descriptor = admin.getTableDescriptor(name);
        String coprocessorClass = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";
        if (!descriptor.hasCoprocessor(coprocessorClass)) {
            admin.disableTable(name);
            descriptor.addCoprocessor(coprocessorClass);
            admin.modifyTable(name, descriptor);
            admin.enableTable(name);
        }
        //計時
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        Scan scan = new Scan();
        //Hbase服務端需要開啓該服務
        AggregationClient aggregationClient = new AggregationClient(connection.getConfiguration());
        Long count = aggregationClient.rowCount(name, new LongColumnInterpreter(), scan);
        stopWatch.stop();
        log.debug("RowCount:{} ,全表count統計耗時:{}", count, stopWatch.getTotalTimeMillis());
        return count;
    }

    /**
     * 根據不同的類型獲取不同的byte數組
     *
     * @param object
     * @return
     */
    private static byte[] getBytes(Object object) {
        Class className = object.getClass();
        if (className.equals(java.lang.Boolean.class)) {
            return Bytes.toBytes((Boolean) object);
        }
        if (className.equals(java.lang.Byte.class)) {
            return Bytes.toBytes((Byte) object);
        }
        if (className.equals(java.lang.Short.class)) {
            return Bytes.toBytes((Short) object);
        }
        if (className.equals(java.lang.Character.class)) {
            return Bytes.toBytes((Character) object);
        }
        if (className.equals(java.lang.Integer.class)) {
            return Bytes.toBytes((Integer) object);
        }
        if (className.equals(java.lang.Long.class)) {
            return Bytes.toBytes((Long) object);
        }
        if (className.equals(java.lang.Float.class)) {
            return Bytes.toBytes((Float) object);
        }
        if (className.equals(java.lang.Double.class)) {
            return Bytes.toBytes((Double) object);
        }
        if (className.equals(java.lang.String.class)) {
            return Bytes.toBytes((String) object);
        }
        if (className.equals(java.nio.ByteBuffer.class)) {
            return Bytes.toBytes((ByteBuffer) object);
        }
        if (className.equals(java.math.BigDecimal.class)) {
            return Bytes.toBytes((BigDecimal) object);
        }
        return ByteUtil.toBytes(object);
    }

    /**
     * 根據不同的byte數組獲取不同的對象
     *
     * @param bytes
     * @return
     */
    private static Object getObject(byte[] bytes, Class className) {
        if (className.equals(java.lang.Boolean.class)) {
            return Bytes.toBoolean(bytes);
        }
        if (className.equals(java.lang.Byte.class)) {
            return Bytes.toShort(bytes);
        }
        if (className.equals(java.lang.Short.class)) {
            return Bytes.toShort(bytes);
        }
        if (className.equals(java.lang.Character.class)) {
            return Bytes.toString(bytes);
        }
        if (className.equals(java.lang.Integer.class)) {
            return Bytes.toInt(bytes);
        }
        if (className.equals(java.lang.Long.class)) {
            return Bytes.toLong(bytes);
        }
        if (className.equals(java.lang.Float.class)) {
            return Bytes.toFloat(bytes);
        }
        if (className.equals(java.lang.Double.class)) {
            return Bytes.toDouble(bytes);
        }
        if (className.equals(java.lang.String.class)) {
            return Bytes.toString(bytes);
        }
        if (className.equals(java.nio.ByteBuffer.class)) {
            return ByteBuffer.wrap(bytes);
        }
        if (className.equals(java.math.BigDecimal.class)) {
            return Bytes.toBigDecimal(bytes);
        }
        return ByteUtil.toObject(bytes);
    }
}

ByteUtil工具類
package com.aimsphm.nuclear.hbase.utill;

import java.io.*;

/**
 * @Package: com.aimsphm.nuclear.hbase.utill
 * @Description: <>
 * @Author: MILLA
 * @CreateDate: 2020/3/6 11:53
 * @UpdateUser: MILLA
 * @UpdateDate: 2020/3/6 11:53
 * @UpdateRemark: <>
 * @Version: 1.0
 */
public final class ByteUtil {

    /**
     * 對象轉數組
     *
     * @param obj 對象
     * @return
     */
    public static byte[] toBytes(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
            oos.close();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return bytes;
    }

    /**
     * 數組轉對象
     *
     * @param bytes 字節數據
     * @return
     */
    public static Object toObject(byte[] bytes) {
        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
            ois.close();
            bis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return obj;
    }
}

備註

壓縮算法、聚合需要在服務端進行插件的配置 Collection對象是單例的,但是在admin中獲取對象的時候使用的是連接池技術
PS:如果將maxVersion設置爲1,那麼相同數據在時間戳大於等於原數據時間戳時,數據會被覆蓋

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章