java使用Map內存集合實時統計大數據量下的數據量

package com.tbyoung.kafka.utils.count;


import com.tbyoung.kafka.bean.Sbinfo;
import com.tbyoung.kafka.service.TestService;
import com.tbyoung.kafka.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 存儲統計沒有的點每個小時發送3條數據
 */
public class MapCountUtils {
    private static final Logger LOG = LoggerFactory.getLogger(MapCountUtils.class);
    /**
     * map集合存放點編號和點的統計情況
     */
    public static Map<String,String> vmap = new HashMap<>(4000);

    //存放開始計時時間
    public static long startTime = 0;
    //50分鐘的毫秒數
    public static long time = 1000 * 60 * 25;
    //補發數量
    public static int count = 1;

    static {
        //testinit();
        init();
    }

    /**
     * 測試初始化
     */
    public static void testinit(){
        startTime = new Date().getTime();
        vmap.clear();
        vmap.put("1","0-NULL");
        vmap.put("2","0-NULL");
        vmap.put("3","0-NULL");
        vmap.put("4","0-NULL");
        vmap.put("5","0-NULL");
        vmap.put("6","0-NULL");
        vmap.put("7","0-NULL");
    }

    /**
     * 初始化
     */
    public static void init(){
        //清空之間打印map
        log();
        //初始化時間爲當前時間
        startTime = new Date().getTime();
        //初始化map
        vmap.clear();
        TestService testService = (TestService)SpringUtils.getBean("testService");
        List<Sbinfo> list = testService.findAll();

        vmap = list.stream().collect(HashMap::new,(m,v)->m.put(v.getCode(),"0-NULL"),HashMap::putAll);

    }

    //存儲Map點數據的寫入
    public synchronized static String writeMap (String code) throws Exception{
        return add(code);
    }

    /**
     * 新增數據
     * @param code
     */
    public static String add(String code){
        String key = code,status = "NULL";
        //清洗集合數據
        updateMaxKey();

        String [] values = vmap.get(code).split("-");


        //時間大於一小時後查找map中卡口爲0的編號
        if(checkTime()){
            //重發數據小於3條改變code值返回
            String codes = min();
            String [] valuesMin = vmap.get(code).split("-");
            if(!StringUtils.isEmpty(codes)){
                code = codes;
                valuesMin = vmap.get(code).split("-");
            }
            if(Integer.parseInt(valuesMin[0])<=count){
                //code = valuesMin[1];
                status = key;
            }
            values = valuesMin;
        }
        //計算寫入map中
        vmap.put(code, (Long.parseLong(values[0]) + 1) + "-"+status);
        //時間大於一小時驗證是否補發完成,完成後初始化map

        if(checkTime() && checkAll()){
            init();
        }
        return code;
    }

    /**
     * 獲取所有不爲NULL的數據的計數是否變成count 未完成
     * @return true 可以重置靜態變量
     */
    public static boolean checkAll(){
        boolean iS = vmap.entrySet().stream()
                .noneMatch(e-> {
                    String []values = e.getValue().split("-");
                    return (!"NULL".equals(values[1])&&Integer.parseInt(values[0])<3);
        });
        return iS;
    }


    /**
     * 爲0的賦值NULL替換成key
     */
    public static void updateMaxKey(){
        if(checkTime()) {
            vmap.entrySet().stream()
                    .filter(e -> {
                        String[] values = e.getValue().split("-");
                        return "0".equals(values[0]);
                    })
                    .forEach(e -> {
                        e.setValue(e.getValue().replaceAll("NULL", max()));
                    });
        }

    }


    /**
     * 返回最大的key
     * @return
     */
    public static String max(){
        return vmap.entrySet().stream()
                .sorted(Map.Entry.<String,String>comparingByValue().reversed())
                .findFirst()
                .get()
                .getKey();
    }

    /**
     * 獲取沒有發送完成的數據
     * @return
     */
    public static String min(){
        return vmap.entrySet().stream()
                .filter((e)->{
                    String[] values = e.getValue().split("-");
                    return (Integer.parseInt(values[0])<3 && !"NULL".equals(values[1]));
                })
                .findFirst()
                .get()
                .getKey();
    }

    /**
     * 時間比較
     * @return
     */
    public static boolean checkTime(){
        return (new Date().getTime() -startTime) > time;
    }

    //打印方法
    public static void log(){
        vmap.entrySet().stream().forEach(e -> {
            String []values=e.getValue().split("-");
            LOG.info(e.getKey()+":"+values[0]+"-"+values[1]);
        });

    }
    //測試方法
    public static void main(String[] args){
        try {
            while (true) {
                System.out.print("輸入");
                Scanner scan = new Scanner(System.in);
                String read = scan.nextLine();
                writeMap(read);
                log();
            }
        }catch (Exception e){

        }
    }
}

 

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