java与HBase的数据增删改查,清空,建表与文件上传下载操作

1.pom

  <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>ch.qos.logback</artifactId>
                    <groupId>logback-classic</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.6.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>ch.qos.logback</artifactId>
                    <groupId>logback-classic</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop-hbase</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

2.配置


hbase:
  conf:
    confMaps:
      'hbase.zookeeper.quorum' : 'xrj:2181'
      'hbase.client.keyvalue.maxsize' : '779606660'
      'zookeeper.session.timeout' : '60000'

3. 相关类


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean
 * @author xrj
 * @date 2020/6/3
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        assertApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertApplicationContext();
        return applicationContext.getBean(requiredType);
    }

    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("applicationContext属性为null,请检查是否注入了SpringContextHolder!");
        }
    }

}

/**
 * 主键生成器
 *
 * @author heou
 */
public class IdentityGenerator {
    private final static long BEGIN_TS = 1483200000000L;

    private long lastTs = 0L;

    /**
     * 设置进程ID
     */
    private long processId = 1025;
    private int processIdBits = 10;

    private long sequence = 0L;
    private int sequenceBits = 12;


    public synchronized long nextId() {
        long ts = System.currentTimeMillis();
        // 刚刚生成的时间戳比上次的时间戳还小,出错
        if (ts < lastTs) {
            throw new RuntimeException("时间戳顺序错误");
        }
        // 刚刚生成的时间戳跟上次的时间戳一样,则需要生成一个sequence序列号
        if (ts == lastTs) {
            // sequence循环自增
            sequence = (sequence + 1) & ((1 << sequenceBits) - 1);
            // 如果sequence=0则需要重新生成时间戳,且必须保证时间戳序列往后
            if (sequence == 0) {
                ts = nextTs(lastTs);
            }
        } else {
            // 如果ts>lastTs,时间戳序列已经不同了,此时可以不必生成sequence了,直接取0
            sequence = 0L;
        }
        // 更新lastTs时间戳
        lastTs = ts;
        return ((ts - BEGIN_TS) << (processIdBits + sequenceBits)) | (processId << sequenceBits) | sequence;
    }

    protected long nextTs(long lastTs) {
        long ts = System.currentTimeMillis();
        while (ts <= lastTs) {
            ts = System.currentTimeMillis();
        }
        return ts;
    }

    private IdentityGenerator() {
    }

    private static volatile IdentityGenerator instance;

    public static IdentityGenerator getInstance() {
        if (instance == null) {
            synchronized (IdentityGenerator.class) {
                if (instance == null) {
                    instance = new IdentityGenerator();
                }
            }
        }
        return instance;
    }
}

import com.alibaba.fastjson.JSONObject;
import com.leishu.model.HBaseFileEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
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.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

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

/**
 * HBaseUtils 工具类
 * DependsOn  控制依赖顺序,保证springContextHolder类在之前已经加载
 * @author xrj
 * @date 2020-7-3
 */
@DependsOn("springContextHolder")
@Component
@Slf4j
public class HBaseUtils {



    public static final String fileTableNameStr="file_table";
    public static final String columnFamilyStr="file";

    /**
     * 手动获取HBaseUtils配置类对象
     */
    private static HbaseConfig hbaseConfig = SpringContextHolder.getBean("hbaseConfig");

    private static Configuration conf = HBaseConfiguration.create();
    /**
     * 设置连接池
     */
    @SuppressWarnings("all")
    private static ExecutorService pool = Executors.newScheduledThreadPool(20);
    private static Connection connection = null;
    private static HBaseUtils instance = null;
    private static Admin admin = null;

    private HBaseUtils(){
        if(connection == null){
            try {
                //将HBase配置类中定义的配置加载到连接池中每个连接里
                Map<String, String> confMap = hbaseConfig.getConfMaps();
                for (Map.Entry<String,String> confEntry : confMap.entrySet()) {
                    conf.set(confEntry.getKey(), confEntry.getValue());
                }
                connection = ConnectionFactory.createConnection(conf, pool);
                admin = connection.getAdmin();
            } catch (IOException e) {
                log.error("HBaseUtils实例初始化失败!错误信息为:" + e.getMessage(), e);
            }
        }
    }

    /**
     * 简单单例方法,如果autowired自动注入就不需要此方法
     */
    public static synchronized HBaseUtils getInstance(){
        if(instance == null){
            instance = new HBaseUtils();
        }
        return instance;
    }

    
  

   

    //*******************************************************************表


    /**
     * 判断表名是否合法
     * @param tableNameStr 表名
     * @return
     */
    private String validateTableName(String tableNameStr){
        if(tableNameStr!=null && StringUtils.isNotEmpty(tableNameStr.trim())){
            return tableNameStr.trim();
        }
        log.error("表名参数错误{}",tableNameStr);
        return null;
    }

    /**
     * 表是否存在
     * @param tableNameStr 表名
     * @return
     */
    public  TableName tableExists(String tableNameStr) throws IOException {
        tableNameStr=validateTableName(tableNameStr);
        if(tableNameStr!=null){
            TableName tableName = TableName.valueOf(tableNameStr);
            //表是否存在
            if (admin.tableExists(tableName)){
                if (admin.isTableDisabled(tableName)){
                    admin.enableTable(tableName);
                }
                return tableName;
            }
            log.error("表不存在{}",tableNameStr);
        }
        return null;
    }

    /**
     * 创建表和列族
     * @param tableNameStr     表名
     * @param columnFamily      列族(数组)
     */
    public boolean createTable(String tableNameStr, String[] columnFamily) throws IOException {
        tableNameStr=validateTableName(tableNameStr);
        if(tableNameStr!=null){
            TableName tableName = TableName.valueOf(tableNameStr);
            //不存在就创建
            if (!admin.tableExists(tableName)) {
                HTableDescriptor desc = new HTableDescriptor(tableName);
                for (String cf : columnFamily) {
                    desc.addFamily(new HColumnDescriptor(cf));
                }
                admin.createTable(desc);
            }else {
                log.error("tableNameStr 已存在");
            }
            return true;
        }
        return false;
    }

    /**
     * 得到所有的表名称
     */
    public List<Map<String,Set>> getListTables() throws IOException {
        List<Map<String,Set>> list=new ArrayList<>();
        Set<String> cloneFamilyList;
        Map<String,Set> map;
        //获取所有的表名
        TableName [] tableNames = admin.listTableNames();
        if (tableNames.length == 0){
            log.info("HBase has no table");
        }else {
            for (TableName tableName:tableNames) {
                map=new HashMap<>();
                HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
                HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
                cloneFamilyList=new HashSet<>();
                for (HColumnDescriptor hColumnDescriptor : columnFamilies) {
                    cloneFamilyList.add(Bytes.toString(hColumnDescriptor.getName())) ;
                }
                map.put(tableName.toString(),cloneFamilyList);
                list.add(map);
            }
        }
        return list;
    }

    

    /**
     * 禁用表
     * @param tableNameStr 表名
     * @throws IOException
     */
    public void disableTable(String tableNameStr) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            if (!admin.isTableDisabled(tableName)) {
                admin.disableTable(tableName);
            }
        }
    }
    
    
    /**
     * 启用表
     * @param tableNameStr 表名
     * @throws IOException
     */
    public void enableTable(String tableNameStr) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            //判断当前的表是否被禁用了,是就开启
            if (admin.isTableDisabled(tableName)){
                admin.enableTable(tableName);
            }
        }
       
    }

    /**
     * 清空表
     * @param tableNameStr 表名
     */
    public void truncate(String tableNameStr) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            if (!admin.isTableDisabled(tableName)) {
                admin.disableTable(tableName);
            }
            admin.truncateTable(tableName,true);
        }
    }

    
  
    /**
     * 删除表操作
     * @param tableNameStr 表名
     * @throws IOException
     */
    public void deleteTable(String tableNameStr) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
    }


    //**************************************************************插入数据

    /**
     * 插入记录(单行单列族-单列单值)
     *
     * @param tableNameStr     表名
     * @param row               行名
     * @param columnFamily      列族名
     * @param column            列名
     * @param value             值
     */
    public void insertOneRecord(String tableNameStr, String row, String columnFamily, String column, String value) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Put put = new Put(Bytes.toBytes(row));
            put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
            table.put(put);
        }
    }

    /**
     * 插入记录(单行单列族-多列多值)
     * 
     * @param tableNameStr     表名
     * @param row               行名
     * @param columnFamily      列族名
     * @param columns           列名(数组)
     * @param values            值(数组)(且需要和列一一对应)
     */
    public void insertRecords(String tableNameStr, String row, String columnFamily, String[] columns, String[] values) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Put put = new Put(Bytes.toBytes(row));
            for (int i = 0; i < columns.length; i++) {
                put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columns[i]), Bytes.toBytes(values[i]));
                table.put(put);
            }
        }

    }




    //**************************************************************************删除数据



    /**
     * 删除当行记录
     * @param tableNameStr     表名 
     * @param rowKey            行名
     */
    public void deleteRow(String tableNameStr, String rowKey) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Delete d = new Delete(rowKey.getBytes());
            table.delete(d);
        }
        
    }
   


    /**
     * 删除单行单列族记录
     * @param tableNameStr     表名        
     * @param rowKey            行名
     * @param columnFamily      列族名
     */
    public void deleteColumnFamily(String tableNameStr, String rowKey, String columnFamily) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            delete.addFamily(Bytes.toBytes(columnFamily));
            table.delete(delete);
        }
        
    }

    /**
     * 删除单行单列族单列记录
     * @param tableNameStr     表名        
     * @param rowKey            行名
     * @param columnFamily      列族名
     * @param column            列名
     */
    public void deleteColumn(String tableNameStr, String rowKey, String columnFamily, String column) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Delete d = new Delete(rowKey.getBytes()).deleteColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
            table.delete(d);
        }
       
    }



    /**
     * 删除单行单列族多列记录
     * @param tableNameStr 表名
     * @param rowKey        行名
     * @param columnFamily  列族名
     * @param columnList    列名数组
     * @throws IOException
     */
    public void deleteColumns(String tableNameStr,String rowKey,String columnFamily,String ...columnList) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            for (String column : columnList) {
                delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
            }
            table.delete(delete);
        }
        
    }

    /**
     * 删除单行多列族
     * @param tableNameStr
     * @param rKey
     * @param cFamily
     * @return
     * @throws IOException
     */
    public boolean deleteColumnFamilys(String tableNameStr,String rKey,String... cFamily) throws IOException {

        boolean flag_1 = false;
        boolean flag_2 = false;
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            //get table object
            Table table = connection.getTable(tableName);

            ResultScanner resultScanner = table.getScanner(new Scan());
            //判断当前输入的RowKey是否存在
            if ("".equals(rKey) || " ".equals(rKey) || rKey == null){
                log.error("RowKey is null");
                return false;
            }else {
                //判断列簇是否存在
                for (Result result:resultScanner){
                    for (Cell cell:result.listCells()){
                        if (rKey.equals(Bytes.toString(CellUtil.cloneRow(cell)))){
                            flag_1 = false;
                            flag_2 = Arrays.asList(cFamily).contains(Bytes.toString(CellUtil.cloneFamily(cell)));
                            if (flag_2){
                                break;
                            }
                        }
                    }
                }
                if (flag_1){
                    log.error("Table has no specific RowKey");
                    return false;
                }

                if (!flag_2) {
                    log.error("Table has no specific column family");
                    return false;
                }

                if(!flag_1 && flag_2){
                    //存放要被删除的RowKey的对象
                    List<Delete> list = new ArrayList<Delete>();
                    //获取RowKey的删除对象
                    Delete delete = new Delete(Bytes.toBytes(rKey));
                    //逐条添加cFamily的对象
                    for (String cf:cFamily){
                        list.add(delete.addFamily(Bytes.toBytes(cf)));
                    }
                    //批量删除
                    table.delete(list);
                }
            }
        }


        return !flag_1 && flag_2;
    }

    /**
     * 删除多行
     * @param tableNameStr 表名
     * @param rowKeyList
     * @throws IOException
     */
    public void deleteRows(String tableNameStr,String ...rowKeyList) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            ArrayList<Delete> deleteList = new ArrayList<>();
            for (String rowKey : rowKeyList) {
                Delete delete = new Delete(Bytes.toBytes(rowKey));
                deleteList.add(delete);
            }
            table.delete(deleteList);
        }
       
    }



    /**
     * 根据rowKey模糊删除
     * @param subKey
     */
    public boolean deleteByKeys(String tableNameStr,String subKey) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            //批量模糊删除
            RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(subKey));
            Table table = connection.getTable(tableName);
            Scan scan = new Scan();
            scan.setFilter(filter);
            ResultScanner scanner = table.getScanner(scan);
            ArrayList<Delete> deleteList = new ArrayList<>();
            for (Result result : scanner) {
                String rowKey = Bytes.toString(result.getRow());
                Delete delete = new Delete(Bytes.toBytes(rowKey));
                deleteList.add(delete);
            }
            table.delete(deleteList);
            return true;
        }
        return false;
    }


    //**************************************************************************查询数据





    /**
     * 查找一行记录(暂时用作文件查询)
     * @param tableNameStr 表名
     * @param rowKey            行名
     */
    public HashMap selectRow(String tableNameStr, String rowKey) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        HashMap hashMap=new HashMap(8);
        HashMap qualifierHashMap=null;
        String qualifier=null;
        Object value=null;
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Get g = new Get(rowKey.getBytes());
            Result rs = table.get(g);
            String family=null;
            for (Cell cell : rs.rawCells()) {
                family=Bytes.toString(cell.getFamily());
                Object o = hashMap.get(family);
                if(o==null){
                    qualifierHashMap=new HashMap(2);
                }else{
                    qualifierHashMap= (HashMap) o;
                }
                qualifier=Bytes.toString(cell.getQualifier());
                if(columnFamilyStr.equals(qualifier)){
                    value = ByteToFile.getFile(cell.getValue(),"D:\\需要的图片",IdentityGenerator.getInstance().nextId()+"",".jpg");
                }else{
                    value    =Bytes.toString(cell.getValue());
                }
                qualifierHashMap.put(qualifier,value);
                hashMap.put(family,qualifierHashMap);
            }
        }
        return hashMap;
    }

    /**
     * 查找一行记录
     * @param tableNameStr 表名
     * @param rowKey            行名
     */
    public String selectStringByRow(String tableNameStr, String rowKey) throws IOException {
        TableName tableName=tableExists(tableNameStr);
        Table table = connection.getTable(tableName);
        Map<String, Object> resultHashMap=new HashMap<>();
        Map<String, Object> param=null;
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        for (Cell kv : result.rawCells()) {
            String family = Bytes.toString(CellUtil.cloneFamily(kv));
            Object o = resultHashMap.get(family);
            if(o==null){
                param=new HashMap<>();
            }else{
                param= (Map<String, Object>) o;
            }
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(kv));
            String value = Bytes.toString(CellUtil.cloneValue(kv));
            param.put(qualifier,value);
            resultHashMap.put(family,param);
        }
        table.close();
        return new JSONObject(resultHashMap).toJSONString();
    }

    /**
     * 查找单行单列族单列记录
     * @param tableNameStr      表名
     * @param rowKey            行名
     * @param columnFamily      列族名
     * @param column            列名
     * @return
     */
    public String selectValue(String tableNameStr, String rowKey, String columnFamily, String column) throws IOException {
        String record = "";
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Get g = new Get(rowKey.getBytes());
            g.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
            Result rs = table.get(g);
            record=Bytes.toString(rs.value());
        }

        return record;
    }


    /**
     * 根据表名和rowKey前缀模糊查询
     * @param tableNameStr
     * @param prefixRowKey
     * @return
     * @throws Exception
     */
    public Map<String, Object> query(String tableNameStr, String prefixRowKey) throws Exception {

        Map<String, Object> map = new HashMap<>();
        TableName tableName = TableName.valueOf(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Scan scan = new Scan();
            Filter filter = new PrefixFilter(Bytes.toBytes(prefixRowKey));
            scan.setFilter(filter);
            ResultScanner rs = table.getScanner(scan);
            for(Result result : rs) {
                String row = Bytes.toString(result.getRow());
                for (Cell cell : result.rawCells()) {
                    byte[] rowArray = cell.getRowArray();
                    log.info("11*"+Bytes.toString(rowArray));
                    map.put(row, Bytes.toString(rowArray));
                }
            }
            table.close();
        }

        return  map;
    }


    /**
     * 查询表中所有行(Scan方式)
     * @param tableNameStr  表名
     * @return
     */
    public String scanAllRecord(String tableNameStr) throws IOException {
        String record = "";
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Scan scan = new Scan();
            ResultScanner scanner = table.getScanner(scan);
            try {
                for(Result result : scanner){
                    for (Cell cell : result.rawCells()) {
                        byte[] rowArray = cell.getRowArray();
                        log.info("11*"+Bytes.toString(rowArray));
                        record+="11*"+Bytes.toString(rowArray)+"\n\t";
                    }
                }
            }catch (Exception e){

            }finally {
                if (scanner != null) {
                    scanner.close();
                }
            }
        }


        return record;
    }


    /**
     * 根据rowKey关键字和时间戳范围查询报告记录
     * @param tableNameStr 表名
     * @param rowKeyword
     * @return
     */
    public List scanReportDataByRowKeywordTimestamp(String tableNameStr, String rowKeyword, Long minStamp, Long maxStamp) throws IOException {
        ArrayList list = new ArrayList<>();
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);
            Scan scan = new Scan();
            //添加scan的时间范围
            if(minStamp!=null && maxStamp!=null){
                scan.setTimeRange(minStamp, maxStamp);
            }

            FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            if(StringUtils.isNotEmpty(rowKeyword)){
                RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(rowKeyword));
                filterList.addFilter(rowFilter);
            }

            Filter pageFilter = new PageFilter(3);
            filterList.addFilter(pageFilter);
            scan.setFilter(filterList);

            JSONObject resultJson = new JSONObject();
            ResultScanner scanner = table.getScanner(scan);
            try {
                for(Result result : scanner){
                    for (Cell cell : result.rawCells()) {
                        byte[] rowArray = cell.getRowArray();
                        log.info("scanReportDataByRowKeywordTimestamp*"+Bytes.toString(rowArray));
                    }
                }
            }catch (Exception e){

        } finally {
                if (scanner != null) {
                    scanner.close();
                }
            }
        }


        return list;
    }




    /**
     * 利用协处理器进行全表count统计
     *
     * @param tableNameStr 表名
     */
    public Long countRowsWithCoprocessor(String tableNameStr) throws Throwable {
        Long count=0L;
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            HTableDescriptor descriptor = admin.getTableDescriptor(tableName);

            String coprocessorClass = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";
            if (! descriptor.hasCoprocessor(coprocessorClass)) {
                admin.disableTable(tableName);
                descriptor.addCoprocessor(coprocessorClass);
                admin.modifyTable(tableName, descriptor);
                admin.enableTable(tableName);
            }

            //计时
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            Scan scan = new Scan();
            AggregationClient aggregationClient = new AggregationClient(conf);

            count = aggregationClient.rowCount(tableName, new LongColumnInterpreter(), scan);

            stopWatch.stop();
            log.info("RowCount:" + count +  ",全表count统计耗时:" + stopWatch.getTotalTimeMillis());
        }


        return count;
    }


    /**
     * 根据row的时间特性查询
     * @param tableNameStr
     * @param startRowkey
     * @param stopRowkey
     * @param columnFamily
     * @param qualifier
     * @return
     * @throws IOException
     */
    public List<Result> getRowKeyAndColumn(String tableNameStr, String startRowkey, String stopRowkey, String columnFamily, String qualifier) throws IOException {


        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null) {
            Table table = connection.getTable(tableName);
            FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            if (StringUtils.isNotBlank(columnFamily)) {
                log.debug("{}", columnFamily);
                filterList.addFilter(new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(columnFamily))));
            }
            if (StringUtils.isNotBlank(qualifier)) {
                log.debug("{}", qualifier);
                filterList.addFilter(new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(qualifier))));
            }
            Scan scan = new Scan();
            if (filterList.getFilters().size() > 0) {
                scan.setFilter(filterList);
            }
            scan.setStartRow(Bytes.toBytes(startRowkey));
            scan.setStopRow(Bytes.toBytes(stopRowkey));
            ResultScanner result = table.getScanner(scan);
            result.forEach(r -> {
                Map map = r.getFamilyMap(Bytes.toBytes("info"));
                List<Cell> cells = r.listCells();
                cells.forEach(c -> System.out.println("getRowKeyAndColumn*"+Bytes.toString(c.getRowArray())));
            });

        }

        return null;

    }




    /**
     * 查询全部
     * @param tableNameStr
     * @throws IOException
     */
    public void readTable(String tableNameStr) throws IOException {


        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            Table table = connection.getTable(tableName);

            ResultScanner resultScanner = table.getScanner(new Scan());

            for (Result result:resultScanner){

                //取行健
                String rowKey=Bytes.toString(result.getRow());
                for (Cell cell:result.listCells()){
                    //rowKey=cellRowKey
                    //取行健
                    String cellRowKey=Bytes.toString(CellUtil.cloneRow(cell));
                    //取到时间戳
                    long timestamp = cell.getTimestamp();
                    //取到族列
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    //取到修饰名
                    String qualifier  = Bytes.toString(CellUtil.cloneQualifier(cell));
                    //取到值
                    String value = Bytes.toString(CellUtil.cloneValue(cell));

                }
            }
            resultScanner.close();
        }
    }




    /**
     * 功能描述: 获取分页数据
     *
     * @param tableName   表名
     * @param currentPage 当前页码
     * @param pageSize    每页条数
     * @return java.lang.String
     */
    private ResultScanner queryDataByPage(String tableName, int currentPage, int pageSize) {
        // 第一次查询时startRowKey为null
        String startRowKey = null;
        ResultScanner results = null;
        // 从第一页开始查询每页的数据
        for (int i = 0; i < currentPage; i++) {
            // 根据每一次传入的rowkey, 查询出排列顺序小于该 rowkey 的 pageSize 条数据, 则最后一页(currentPage)的数据就是最后一次查询的结果
            results = queryData(tableName, startRowKey, pageSize);
            Iterator<Result> iterator = results.iterator();
            while (iterator.hasNext()) {
                // 将每一页的最后一条数据做为下一页查询的起始行(不包含该条数据)
                startRowKey = Bytes.toString(iterator.next().getRow());
            }
        }
        return results;
    }

    /**
     * 功能描述: 查询数据
     *
     * @param tableName   表名
     * @param startRowKey 每页起始rowkey
     * @param pageSize    每页条数
     * @return org.apache.hadoop.hbase.client.ResultScanner
     */
    public ResultScanner queryData(String tableName, String startRowKey, int pageSize) {
        HTable table = null;
        ResultScanner results = null;

        Scan scan = new Scan();
        // 设置倒序扫描(倒序查询的关键)
        scan.setReversed(true);

        // MUST_PASS_ALL 表示需要满足过滤器集合中的所有的filter
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        // 设置查询条数
        PageFilter pageFilter = new PageFilter(pageSize);
        filterList.addFilter(pageFilter);

        // 如果查询到了 startRowKey, 则过滤比 startRowKey 大的值
        if (StringUtils.isNotBlank(startRowKey)) {
            RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(startRowKey)));
            filterList.addFilter(rowFilter);
        }
        scan.setFilter(filterList);

        try {
            table = (HTable) connection.getTable(TableName.valueOf(tableName));
            results = table.getScanner(scan);
        } catch (IOException e) {
            log.error("IOException ",e);
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    log.error("IOException ",e);
                }
            }
        }
        return results;
    }



    /**
     * @desc hbase简单分页查询
     * @param tableNameStr 表名
     * @param endRow
     * @param pageSize
     */
    public List<Result> pageFilter(String tableNameStr, String  endRow,int pageSize) throws IOException {

        ResultScanner scanner = null;
        Table table = null;
        List<Result> list = new ArrayList<>();
        TableName tableName=tableExists(tableNameStr);
        if(tableName!=null){
            table = connection.getTable(TableName.valueOf(tableNameStr));
            //长度为零的字节数组,0x00十六进制表示0
            byte[] POSTFIX = new byte[ 0x00 ];

            Filter filter = new PageFilter(pageSize);
            Scan scan = new Scan();
            scan.setFilter(filter);

            //倒序
            scan.setReversed(true);
            //设置hbase单次获取到内存的数据条数
            //scan.setCaching(1024);
            //每次查询的最后一条记录endRow作为新的startRow
            if (endRow!=null){
                //这里为啥加POSTFIX不是很明白,好像是为了区分下一页,但是我去掉结果也没影响
                byte[] startRow = Bytes.add(Bytes.toBytes(endRow), POSTFIX);
                scan.setStartRow(startRow);
            }
            scanner = table.getScanner(scan);
            Result result;
            while ((result = scanner.next()) != null) {
                list.add(result);
            }

            for(int i=1;i<list.size();i++){
                Result re=list.get(i);
                for (Cell cell : re.rawCells()) {
                    byte[] rowArray = cell.getRowArray();
                    log.info(i+"*"+Bytes.toString(rowArray));
                }
            }

            table.close();
        }

        return list;
    }




    //********************************************文件上传下载



    /**
     * 插入文件
     * @param hBaseFileEntity      对象
     */
    public boolean insertFile(HBaseFileEntity hBaseFileEntity) throws IOException {
        TableName tableName=tableExists(fileTableNameStr);
        if(tableName!=null && hBaseFileEntity!=null && hBaseFileEntity.getRowKey()!=null){
            Table table = connection.getTable(tableName);
            Put put = new Put(Bytes.toBytes(hBaseFileEntity.getRowKey()));
            put.addColumn(Bytes.toBytes(columnFamilyStr), Bytes.toBytes("day"), Bytes.toBytes(getInt(hBaseFileEntity.getDay())));
            put.addColumn(Bytes.toBytes(columnFamilyStr), Bytes.toBytes("module"), Bytes.toBytes(getString(hBaseFileEntity.getModule())));
            table.put(put);
            put.addColumn(Bytes.toBytes(columnFamilyStr), Bytes.toBytes("month"), Bytes.toBytes(getInt(hBaseFileEntity.getMonth())));
            table.put(put);
            put.addColumn(Bytes.toBytes(columnFamilyStr), Bytes.toBytes("type"), Bytes.toBytes(getString(hBaseFileEntity.getType())));
            table.put(put);
            put.addColumn(Bytes.toBytes(columnFamilyStr), Bytes.toBytes("year"), Bytes.toBytes(getInt(hBaseFileEntity.getYear())));
            table.put(put);
            put.addColumn(Bytes.toBytes(columnFamilyStr), Bytes.toBytes("file"), ByteToFile.getBytes(hBaseFileEntity.getFile()));
            table.put(put);
            return true;
        }else {
            return false;
        }
    }


    public static int getInt(Integer flag){
        if(flag==null){
            return 0;
        }
        return flag;
    }


    public static String getString(String flag){
        if(flag==null){
            return "";
        }
        return flag;
    }










    //*************************************************************

    /**
     * 全表扫描
     * @param tableNameStr 表名
     * @return
     * @throws IOException
     */
    public static ResultScanner scan(String tableNameStr) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableNameStr));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        return scanner;
    }

    /**
     * 全表扫描-列簇
     * @param tableNameStr 表名
     * @param columnFamily
     * @return
     * @throws IOException
     */
    public static ResultScanner scan(String tableNameStr,String columnFamily) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableNameStr));
        Scan scan = new Scan();
        scan.addFamily(Bytes.toBytes(columnFamily));
        ResultScanner scanner = table.getScanner(scan);
        return scanner;

    }

    /**
     * 全表扫描-列
     * @param tableNameStr 表名
     * @param columnFamily
     * @param column
     * @return
     * @throws IOException
     */
    public static ResultScanner scan(String tableNameStr,String columnFamily,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableNameStr));
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
        ResultScanner scanner = table.getScanner(scan);
        return scanner;
    }



    /**
     * 分页全表扫描-过滤器
     * @param tableNameStr 表名
     * @param filter
     * @return
     * @throws IOException
     */
    public static ResultScanner scan(String tableNameStr, Filter filter,String startRowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableNameStr));
        Scan scan = new Scan();
        scan.setFilter(filter);
        scan.setStartRow(Bytes.toBytes(startRowKey));
        ResultScanner scanner = table.getScanner(scan);
        return scanner;
    }

    /**
     * 获取分页过滤器
     * @param size
     * @return
     */
    public static Filter pagetFilter(long size) {
        return new PageFilter(size);
    }


    /**
     * columnPrefixFilter
     * @param prefix
     * @return
     */
    public static Filter columnPrefixFilter(String prefix) {
        return new ColumnPrefixFilter(Bytes.toBytes(prefix));
    }

    /**
     * 过滤器-and
     * @param filterList
     * @return
     */
    public static FilterList filterListPassAll(Filter ...filterList) {
        FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        for (Filter filter : filterList) {
            list.addFilter(filter);
        }
        return list;
    }


    /**
     * 过滤器-or
     * @param
     * @return
     */
    public static FilterList filterListPassOne(Filter ...filterList) {
        FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        for (Filter filter : filterList) {
            list.addFilter(filter);
        }
        return list;
    }



}


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * Hbase-Conf配置
 *
 * @Author: xrj
 * @Date: 2020/7/12 10:49
 */
@Configuration
@ConfigurationProperties(prefix = HbaseConfig.CONF_PREFIX)
public class HbaseConfig {

    public static final String CONF_PREFIX = "hbase.conf";

    private Map<String,String> confMaps;

    public Map<String, String> getConfMaps() {
        return confMaps;
    }

    public void setConfMaps(Map<String, String> confMaps) {
        this.confMaps = confMaps;
    }
}
/**
 * 文件
 * @author xrj
 * @date 2020/6/4
 */
public class FileConstant {


    //存的方式      配置路径+文件类型+module+timePath+fileName
    //数据库只存    文件类型+module+timePath+fileName


    public static int MAX = 15;
    public static int MIN = 1;

    /**
     * id分隔符
     */
    public static String LINE="-";


    /**
     * 文件分隔符
     */
    public static String SEPARATE="/";

    /**
     * 文件类型
     */
    public static String IMAGE="image";
    public static String FILE="file";





}

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @Description: 文件下载
 * @Author xrj
 * @Date 2020/4/10
 **/
@Slf4j
public class DownLoadUtil {


    /**
     * 视频播放
     */
    public static final String VIDEO_TYPE="video/mp4";
    public static final String FILE_TYPE="application/force-download";
    public static final String IMAGE_TYPE="image/jpg";

    /**
     * 文件下载方法/在线播放
     * @param response 响应
     */
    public static void download(HttpServletRequest request, HttpServletResponse response, File file, String contentType) {

        OutputStream outputStream = null;
        if (file.exists()) {
            byte[] buffer = new byte[1024];
            try {
                //设置内容类型
                response.setContentType(contentType);
                RandomAccessFile targetFile = new RandomAccessFile(file, "r");
                if(contentType.equals(VIDEO_TYPE)){
                    //在线观看视频
                    long fileLength = targetFile.length();
                    response.reset();
                    //获取从那个字节开始读取文件
                    String rangeString = request.getHeader("Range");
                    //播放
                    if(rangeString != null) {
                        long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
                        //设置此次相应返回的数据长度
                        response.setHeader("Content-Length", String.valueOf(fileLength - range));
                        //设置此次相应返回的数据范围
                        response.setHeader("Content-Range", "bytes " + range + "-" + (fileLength - 1) + "/" + fileLength);
                        //返回码需要为206,而不是200
                        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                        //设定文件读取开始位置(以字节为单位)
                        targetFile.seek(range);
                    }
                }
                response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file.getName() , "UTF-8"));
                outputStream = response.getOutputStream();
                int flag;
                while ((flag = targetFile.read(buffer))!=-1){
                    outputStream.write(buffer, 0, flag);
                }
            } catch (Exception e) {
               log.error("download   Exception:",e);
            } finally {
                if(outputStream!=null){
                    try {
                        outputStream.flush();
                        outputStream.close();
                    } catch (IOException e) {
                        log.error("download outputStream  Exception:",e);
                    }

                }

            }
        }
    }

    /**
     * 图片在线浏览
     * @param response 响应
     */
    public static void downloadImage(HttpServletResponse response,  File file) throws Exception {
        if (file.exists()) {
            try {
                FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
                response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file.getName() , "UTF-8"));
                response.setContentType(IMAGE_TYPE);

                response.setHeader("Connection", "keep-alive");
                response.setHeader("Keep-Alive", "timeout=60");
                return;
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
        log.error("资源不存在:"+file.getAbsolutePath());
        //这里不抛异常,放过请求
    }
}

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * @author xrj
 * @date 2020/3/27
 */
@Slf4j
public class DateUtil {

    /**
     * 日期模板
     */
    public static final String DATEUTIL_YMDHMS="yyyy-MM-dd HH:mm:ss";
    public static final String DATEUTILYMDHMS="yyyyMMddHHmmss";
    public static final String DATEUTIL_YMD="yyyy-MM-dd";
    public static final String DATEUTIL_Y="yyyy";
    public static final String DATEUTIL_HMS="HH:mm:ss";


    /**
     * 两个时间相隔类型  get
     */
    public static final int YEARTYPE=0;
    public static final int DAYTYPE=1;
    public static final int HOURTYPE=2;
    public static final int MINTYPE=3;
    public static final int SECTYPE=4;



    /**
     * 字符串转日期
     * @param dateFormat   日期格式
     * @param date
     * @return
     */
    public static Date parseDate(String dateFormat, String date) {
        if(StringUtils.isEmpty(date)){
            return null;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            return sdf.parse(date);
        } catch (Exception e) {
            log.error("字符串转日期发生异常!原因是:",e);
            return null;
        }
    }

    /**
     * 日期转字符串
     * @param dateFormat
     * @param date
     * @return
     */
    public static String parseDate(String dateFormat, Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            return sdf.format(date);
        } catch (Exception e) {
            log.error("日期转字符串发生异常!原因是:",e);
            return null;
        }
    }



    /**
     * 两个时间间隔了多少年
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getYearSubByStr(String beginDate,String endDate) {
        return getDaySubByStr(beginDate, endDate)/365;
    }


    /**
     * 两个时间间隔了多少年
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getYearSub(Date beginDate,Date endDate) {
        return getSubTimes(beginDate,endDate,YEARTYPE);
    }

    /**
     * 时间相减得到天数  字符串
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long
     * @author Administrator
     */
    public static long getDaySubByStr(String beginDateStr,String endDateStr){
        long day=0;
        if(beginDateStr==null || endDateStr==null
                || StringUtils.isEmpty(beginDateStr.trim())
                || StringUtils.isEmpty(endDateStr.trim())){
            //错误日期
            return day;
        }
        Date beginDate = parseDate(DATEUTIL_YMD,beginDateStr);
        Date endDate = parseDate(DATEUTIL_YMD,endDateStr);
        return getDaySub(beginDate,endDate);
    }

    /**
     * 时间相减得到天数   格式年月日
     * @param beginDate  DATEUTIL_YMD
     * @param endDate    DATEUTIL_YMD
     * @return
     */
    public static long getDaySub(Date beginDate,Date endDate){
        return getSubTimes(beginDate,endDate,DAYTYPE);
    }


    /**
     * 把时间根据时、分、秒转换为时间段
     * @param  beginDate
     * @param  endDate
     * @param  type  DateUtil.YEARTYPE
     *               DateUtil.DAYTYPE
     *               DateUtil.HOURTYPE
     *               DateUtil.MINTYPE
     *               DateUtil.SECTYPE
     *
     */
    public static long getSubTimes(Date beginDate,Date endDate,int type){
        long returnNum=0;
        if(beginDate==null || endDate==null){
            //日期不能为空
            return returnNum;
        }
        long times = endDate.getTime() - beginDate.getTime();
        long day = times / (24 * 60 * 60 * 1000);
        long hour = (times / (60 * 60 * 1000) - day * 24);
        long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if(type==YEARTYPE){
            return day/365;
        }else if(type==DAYTYPE){
           return day;
        }else if(type==HOURTYPE){
           return hour;
        }else if(type==MINTYPE){
           return min;
        }else if(type==SECTYPE){
           return sec;
        }else{
           return returnNum;
        }
    }





    /**
     * 日期比较
     * @Description: 如果start>=end 返回true 否则返回false
     * @param dateFormat  DateUtil.DATEUTIL_YMDHMS
     * @param start
     * @param end
     * @return boolean
     */
    public static boolean compareDate(String dateFormat,String start, String end) {
        Date startDate=parseDate(dateFormat,start);
        Date endDate=parseDate(dateFormat,end);
        if(startDate==null||endDate==null){
            return false;
        }
        return startDate.getTime() >= endDate.getTime();
    }



    /**
     * 校验日期是否合法
     * @return
     */
    public static boolean isValidDate(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            fmt.parse(date);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            return false;
        }
    }



    /**
     * 返回年与周
     * @param date
     * @param flag   flag ? year + "年第" + week + "周" : year + week
     * @return
     */
    public static String getYearAndWeek(Date date, boolean flag) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATEUTIL_Y);
        String year ;
        int week ;
        try {
            year = simpleDateFormat.format(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(2);
            calendar.setTime(date);
            week= calendar.get(3);
        } catch (Exception e) {
            log.error("返回年与周!原因是:",e);
            return "";
        }
        return flag ? year + "年第" + week + "周" : year + week;
    }


    /**
     * 当前日期 加上天数后 的日期
     * @param num  当前日期+天数
     * @param day  日期
     * @return  返回当前日期加上天数后的日期
     */
    public static Date plusDay(int num,Date day){
        if(day==null){
            return day;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(day);
        // num为增加的天数,可以改变的
        c.add(Calendar.DATE, num);
        return c.getTime();
    }

    /**
     * 根据当前日期返回本周一的日期
     * @param day
     * @return
     */
    public static String getWeekStart(Date day){
        //获得日历
        Calendar cal = DateUtil.calendarUtil(day);
        //获得SimpleDateFormat对象
        SimpleDateFormat dateFormat = DateUtil.getSimpleDateFormat();
        // index中存的就是星期几了,其范围 1~7;1=星期日 7=星期六,其他类推
        //获取当前是在本周的第几天
        int index=cal.get(Calendar.DAY_OF_WEEK);
        //本周开始日期
        String weekStart = null;
        switch (index) {
            case 2:
                //周一
                weekStart=dateFormat.format(day);
                break;
            case 3:
                //周二,获取当前日期的前一天.
                cal.add(Calendar.DAY_OF_MONTH, -1);
                weekStart=dateFormat.format(cal.getTime());
                break;
            case 4:
                //周三,取当前日期的前两天.
                cal.add(Calendar.DAY_OF_MONTH, -2);
                weekStart = dateFormat.format(cal.getTime());
                break;
            case 5:
                //周四,获取取当前日期的前三天.
                cal.add(Calendar.DAY_OF_MONTH, -3);
                weekStart=dateFormat.format(cal.getTime());
                break;
            case 6:
                //周五,获取取当前日期的前四天.
                cal.add(Calendar.DAY_OF_MONTH, -4);
                weekStart=dateFormat.format(cal.getTime());
                break;
            case 7:
                //周六,获取取当前日期的前五天.
                cal.add(Calendar.DAY_OF_MONTH, -5);
                weekStart=dateFormat.format(cal.getTime());
                break;
            default:
                //周日,取当前日期的前六天.
                cal.add(Calendar.DAY_OF_MONTH, -6);
                weekStart=dateFormat.format(cal.getTime());
                break;
        }
        return weekStart;
    }

    /**
     * 根据当前日期返回本周日的日期
     * @param day
     * @return
     */
    public static String getWeekEnd(Date day){
        //获得日历
        Calendar cal = DateUtil.calendarUtil(day);
        //获得SimpleDateFormat对象
        SimpleDateFormat dateFormat = DateUtil.getSimpleDateFormat();
        // index中存的就是星期几了,其范围 1~7;1=星期日 7=星期六,其他类推
        //获取当前是在本周的第几天
        int index=cal.get(Calendar.DAY_OF_WEEK);
        //本周开始日期
        String weekEnd = null;
        switch (index) {
            case 2:
                //周一,+6天
                cal.add(Calendar.DAY_OF_MONTH, +6);
                weekEnd = dateFormat.format(cal.getTime());
                break;
            case 3:
                //周二,+5天
                cal.add(Calendar.DAY_OF_MONTH, +5);
                weekEnd=dateFormat.format(cal.getTime());
                break;
            case 4:
                //周三,+4天
                cal.add(Calendar.DAY_OF_MONTH, +4);
                weekEnd = dateFormat.format(cal.getTime());
                break;
            case 5:
                //周四,+3天
                cal.add(Calendar.DAY_OF_MONTH, +3);
                weekEnd=dateFormat.format(cal.getTime());
                break;
            case 6:
                //周五,+2天
                cal.add(Calendar.DAY_OF_MONTH, +2);
                weekEnd=dateFormat.format(cal.getTime());
                break;
            case 7:
                //周六,+1天
                cal.add(Calendar.DAY_OF_MONTH, +1);
                weekEnd=dateFormat.format(cal.getTime());
                break;
            default:
                weekEnd=dateFormat.format(day);
                break;
        }
        return weekEnd;
    }

    /**
     * 返回当前日期的当月第一天日期
     * @param day
     * @return
     */
    public static String getMouthStart(Date day){
        //用于返回的本月第一天日期的字符串
        String mouthStart = null;
        //获得日历
        Calendar cal = DateUtil.calendarUtil(day);
        //获得SimpleDateFormat对象
        SimpleDateFormat dateFormat = DateUtil.getSimpleDateFormat();
        //设置本月第一天日期
        cal.set(Calendar.DAY_OF_MONTH, 1);
        mouthStart = dateFormat.format(cal.getTime());
        return mouthStart;
    }

    /**
     * 返回本月最后一天的日期
     * @param day
     * @return
     */
    public static String getMouthEnd(Date day){
        //用于返回的当月日期String;
        String mouthEnd = null;
        //获得日历
        Calendar cal = DateUtil.calendarUtil(day);
        //获得SimpleDateFormat对象
        SimpleDateFormat dateFormat = DateUtil.getSimpleDateFormat();
        //返回本月最大日期的int类型
        int index = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置Calendar日期
        cal.set(Calendar.DAY_OF_MONTH, index);
        mouthEnd = dateFormat.format(cal.getTime());
        return mouthEnd;
    }

    /**
     * 返回当前日期的日历
     * @param day
     * @return
     */
    public static Calendar calendarUtil(Date day){
        SimpleDateFormat dateFormat = DateUtil.getSimpleDateFormat();
        //格式化日期,得到日期的字符串
        String MTime = dateFormat.format(day);
        //用于保存当前日期(格式化之后的)
        Date currentDate=null;
        try {
            currentDate = dateFormat.parse(MTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //使用默认时区和区域设置获取日历。
        Calendar cal = Calendar.getInstance();
        //使用给定的 Date设置此日历的时间。
        cal.setTime(currentDate);
        return cal;
    }

    /**
     * 返回格式化为DATEUTIL_YMD的SimpleDateFormat对象
     * @return
     */
    public static SimpleDateFormat getSimpleDateFormat(){
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATEUTIL_YMD);
        return dateFormat;
    }

}

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author xrj
 * @date 2020/7/7
 */
@Slf4j
public class ByteToFile {



    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws IOException {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据byte数组,生成文件
     */
    public static File getFile(byte[] bfile, String filePath,String fileName,String suffix) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if(!dir.exists()&&dir.isDirectory()){
                //判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath+"/"+fileName+suffix);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
           log.error("ByteToFile Exception",e);
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    log.error("ByteToFile finally bos Exception",e1);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    log.error("ByteToFile finally fos Exception",e1);
                }
            }
        }
        return file;
    }



    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(MultipartFile file){
        byte[] buffer = null;
        try {
            if(file!=null){
                log.info("大小:"+file.getSize());
                FileInputStream fis = new FileInputStream(multipartFileToFile(file));
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
                byte[] b = new byte[1024];
                int n;
                while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
                }
                fis.close();
                bos.close();
                buffer = bos.toByteArray();
            }
        } catch (FileNotFoundException e) {
            log.error("ByteToFile getBytes FileNotFoundException",e);
        } catch (IOException e) {
            log.error("ByteToFile getBytes IOException",e);
        }
        return buffer;
    }


}

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

/**
 * @author xrj
 * @date 2020/7/7
 */
@Data
public class HBaseFileEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    private String rowKey;

    /**
     * 图片类型
     */
    private String type;
    /**
     * 业务模块
     */
    private String module;
    /**
     * 年
     */
    private Integer year;
    /**
     * 月
     */
    private Integer month;
    /**
     * 日
     */
    private Integer day;
    /**
     * 上传文件
     */
    private MultipartFile file;

}

4.controller接口


import com.leishu.utils.HBaseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * http://localhost:8081/hbase/test/table
 * 访问地址
 * @author xrj
 */
@RequestMapping("/hbase/test/table")
@RestController
@Slf4j
public class TableController {


        @Autowired
        private HBaseUtils hBaseUtils;


        /**
         * tableExists
         * @param tableName
         */
        @GetMapping("/tableExists")
        public boolean tableExists(String tableName) throws IOException {
            log.info("tableExists:"+tableName);
            TableName table = hBaseUtils.tableExists(tableName);
            if(table==null){
                return false;
            }else {
                return true;
            }
        }

        /**
         * createTable
         * @param tableName
         */
        @GetMapping("/createTable")
        public void createTable(String tableName,String[] columnFamily) throws IOException {
            log.info("createTable:"+tableName);
            hBaseUtils.createTable(tableName,columnFamily);
        }



        /**
         * getListTables
         */
        @GetMapping("/listTables")
        public List<Map<String, Set>> getListTables() throws IOException {
            return hBaseUtils.getListTables();
        }



        /**
         * disableTable
         */
        @GetMapping("/disableTable")
        public void disableTable(String tableName) throws IOException {
             hBaseUtils.disableTable(tableName);
        }




        /**
         * enableTable
         */
        @GetMapping("/enableTable")
        public void enableTable(String tableName) throws IOException {
            hBaseUtils.enableTable(tableName);
        }



        /**
         * deleteTable
         */
        @GetMapping("/deleteTable")
        public void deleteTable(String tableName) throws IOException {
            hBaseUtils.deleteTable(tableName);
        }



    /**
     * truncateTable
     */
    @GetMapping("/truncateTable")
    public void truncateTable(String tableName) throws IOException {
        hBaseUtils.truncate(tableName);
    }
}

import com.leishu.utils.HBaseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * http://localhost:8081/hbase/test/select
 * 访问地址
 * @author xrj
 */
@RequestMapping("/hbase/test/select")
@RestController
@Slf4j
public class SelectController {


        @Autowired
        private HBaseUtils hBaseUtils;


        /**
         * 查询row
         * @return
         */
        @GetMapping("/selectRow")
        public HashMap selectRow(String tableNameStr, String rowKey) throws IOException {
            HashMap hashMap = hBaseUtils.selectRow(tableNameStr, rowKey);
            log.info(tableNameStr+"查询row:"+hashMap);
            return hashMap;
        }

        /**
         * 查询row
         * @return
         */
        @GetMapping("/selectRow/String")
        public String selectStringByRow(String tableNameStr, String rowKey) throws IOException {
            String str = hBaseUtils.selectStringByRow(tableNameStr, rowKey);
            log.info(tableNameStr+"查询row:"+str);
            return str;
        }


        /**
         * 查询所有
         * @return
         */
        @GetMapping("/all")
        public String getResultModel1(String tableNameStr) throws IOException {
            String jier = hBaseUtils.scanAllRecord(tableNameStr);
            log.info(tableNameStr+"查询所有:"+jier);
            return jier;
        }

        /**
         * 查询所有num
         * @return
         */
        @GetMapping("/all/num")
        public Long getResultModel12(String tableNameStr) throws Throwable {
            Long num = hBaseUtils.countRowsWithCoprocessor(tableNameStr);
            log.info(tableNameStr+"查询所有num:"+num);
            return num;
        }


        /**
         * 通过关键字查询所有list
         * @return
         */
        @GetMapping("/all/key")
        public List getResultModel13(String tableNameStr,String rowKeyword, Long minStamp, Long maxStamp) throws Throwable {
            List list = hBaseUtils.scanReportDataByRowKeywordTimestamp(tableNameStr,rowKeyword,minStamp,maxStamp);
            log.info(tableNameStr+"通过关键字"+rowKeyword+"查询所有list:"+list);
            return list;
        }






        /**
         * 分页查询
         * @return
         */
        @GetMapping("/all/page")
        public List pageFilter(String tableNameStr,String endRow,int pageSize) throws Throwable {
            //需要上一条数据的最后,如果没有表示查所有(逻辑操作)包含endRow
            List list = hBaseUtils.pageFilter(tableNameStr,endRow,pageSize);
            log.info(tableNameStr+"分页查询:"+list);
            return list;
        }




    /**
     * 根据时间row查询
     * 根据列名
     * @return
     */
    @GetMapping("/time/page")
    public List timePageFilter(String tableNameStr,String startRowkey,String stopRowkey, String qualifier) throws Throwable {
        //Rowkey=时间+唯一uuid,避免不同数据的覆盖
        //查询qualifier的过滤
        List list = hBaseUtils.getRowKeyAndColumn(tableNameStr,startRowkey,stopRowkey,null,qualifier);
        log.info(tableNameStr+"根据时间row查询:"+list);
        return list;
    }





    /**
     * 根据表名和rowKey前缀模糊查询
     * @return
     */
    @GetMapping("/all/query/page")
    public Map query(String tableNameStr,String prefixRowKey) throws Throwable {
        Map<String, Object> map = hBaseUtils.query(tableNameStr, prefixRowKey);
        log.info(tableNameStr+"根据表名和输入条件获取HBase的记录数:"+map);
        return map;
    }






    /**
     * rowkey模糊删除
     * @return
     */
    @GetMapping("/delete")
    public Boolean deleteByKeys(String tableNameStr,String likeRowKey) throws Throwable {
        Boolean bool = hBaseUtils.deleteByKeys(tableNameStr, likeRowKey);
        log.info(tableNameStr+"rowkey模糊删除:"+bool);
        return bool;
    }
}

import com.leishu.model.HBaseFileEntity;
import com.leishu.utils.DownLoadUtil;
import com.leishu.utils.HBaseUtils;
import com.leishu.utils.IdentityGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * http://localhost:8081/hbase/test/file
 * 访问地址
 * @author xrj
 */
@RequestMapping("/hbase/test/file")
@RestController
@Slf4j
public class FileController {


        @Autowired
        private HBaseUtils hBaseUtils;


    /**
     * 上传
     * @param files
     * @param module
     * @return
     * @throws IOException
     */
        @PostMapping(value = "/files/{module}", headers = "content-type=multipart/form-data")
        public  List<String> uploadFiles(@RequestBody MultipartFile[] files, @PathVariable String module) throws IOException {
            List<String> list=new ArrayList<>();
            String rowKey=null;
                for (MultipartFile file:files
                     ) {
                        HBaseFileEntity hBaseFileEntity=new HBaseFileEntity();
                        hBaseFileEntity.setFile(file);
                        hBaseFileEntity.setModule(module);
                        hBaseFileEntity.setYear(2020);
                        hBaseFileEntity.setDay(7);
                        rowKey=IdentityGenerator.getInstance().nextId()+"";
                        hBaseFileEntity.setRowKey(rowKey);
                        hBaseUtils.insertFile(hBaseFileEntity);
                        list.add(rowKey);
                }
                return list;

        }


    /**
     * 图片在线浏览
     * @param response
     * @param rowKey
     */
    @GetMapping("/image/{rowKey}")
    public void image(HttpServletResponse response, @PathVariable String rowKey) throws Exception {
        if(!StringUtils.isEmpty(rowKey)){
            HashMap hashMap = hBaseUtils.selectRow(HBaseUtils.fileTableNameStr, rowKey);
            hashMap = (HashMap) hashMap.get(HBaseUtils.columnFamilyStr);
            DownLoadUtil.downloadImage(response, (File) hashMap.get("file"));
        }
    }


    /**
     * 下载接口
     * @param response
     * @param rowKey
     */
    @GetMapping("/download/{rowKey}")
    public void download(HttpServletResponse response, @PathVariable String rowKey) throws IOException {
        HashMap hashMap = hBaseUtils.selectRow(HBaseUtils.fileTableNameStr, rowKey);
        hashMap = (HashMap) hashMap.get(HBaseUtils.columnFamilyStr);

        DownLoadUtil.download(null,response, (File) hashMap.get("file"), DownLoadUtil.FILE_TYPE);
    }


    /**
     * 播放视频接口
     * @param request
     * @param response
     * @param rowKey
     */
    @GetMapping("/video/play/{rowKey}")
    public void play(HttpServletRequest request, HttpServletResponse response, @PathVariable String rowKey) throws IOException {
        HashMap hashMap = hBaseUtils.selectRow(HBaseUtils.fileTableNameStr, rowKey);
        hashMap = (HashMap) hashMap.get(HBaseUtils.columnFamilyStr);

        DownLoadUtil.download(request,response, (File) hashMap.get("file"),DownLoadUtil.VIDEO_TYPE);
    }


}

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.leishu.model.LocationInfoUploadMsgDto;
import com.leishu.utils.DateUtil;
import com.leishu.utils.HBaseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * http://localhost:8081/hbase/test/create
 * 访问地址
 * @author xrj
 */
@RequestMapping("/hbase/test/create")
@RestController
@Slf4j
public class CreateController {


        @Autowired
        private HBaseUtils hBaseUtils;



        /**
         * 添加
         *
         * 写入记录,不存在就是写入,已存在就更新添加的部分
         * @return
         */
        @GetMapping("/columns/values")
        public String getResultModel(String tableName,String rowKey,String columnFamily,String[] columns,String[] values) throws IOException {
                log.info("写入记录,不存在就是写入,已存在就更新添加的部分");
                //hBaseUtils.insertRecords(tableName,rowKey,columnFamily,columns,values);
                SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyyMMddHHmmss");
                LocationInfoUploadMsgDto locationInfoUploadMsgDto = new LocationInfoUploadMsgDto();
                for (int i=1;i<50;i++){
                        locationInfoUploadMsgDto.setDirection(i);
                        hBaseUtils.insertRecords("gps",
                                DateUtil.parseDate(DateUtil.DATEUTILYMDHMS,new Date())+"川A55f85",
                                "info",
                                new String[]{"key"+i},
                                new String[]{JSONObject.toJSONString(locationInfoUploadMsgDto)});



                        hBaseUtils.insertRecords("gps",
                                DateUtil.parseDate(DateUtil.DATEUTILYMDHMS,new Date())+"川A9765t",
                                "info",
                                new String[]{"key"+i*i},
                                new String[]{JSONObject.toJSONString(locationInfoUploadMsgDto)});
                 }
                return "succece";

        }



}

 

博主强烈推荐:https://blog.csdn.net/persistencegoing/article/details/84376427

希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职  

群号:721 515 304
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值