分布與并行計算—日志挖掘(Java)

日志挖掘——處理數據、計費統計

1、讀取附件中日志的內容,找出自己學號停車場中對應的進出車次數(in/out配對的記錄數,1條in、1條out,視為一個車次,本日志中in/out為一一對應,不存在缺失某條進或出記錄)

2、統計自己停車場車輛累計停放秒(即某個車牌,車輛駛出時間-車輛駛入時間=本此停放秒數,統計自己車場全部車牌的累計)

在日志挖掘實驗基礎上,針對車輛進出進行計費。

計費規則如下:

1、進場后在30分鐘內出場的車輛,免收費;

2、停放在2小時內(含2小時),收費10元;從第三小時起,每小時2元。

3、不封頂。

public class CountPark {static   Map<String,Integer> profit=new HashMap<>();static   Map<String,String> map=new HashMap<>();static long cnt=0,time=0;static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");public static List<String> readTxt(String fileName){List<String> list=new ArrayList<>();try { // 防止文件建立或讀取失敗,用catch捕捉錯誤并打印,也可以throw/* 讀入TXT文件 */System.out.println(fileName);File filename = new File(fileName); // 要讀取以上路徑的input。txt文件InputStreamReader reader = new InputStreamReader(new FileInputStream(filename),"gbk"); // 建立一個輸入流對象readerBufferedReader br = new BufferedReader(reader); // 建立一個對象,它把文件內容轉成計算機能讀懂的語言String line = "";line = br.readLine();while (line != null) {String[] temp=line.split(",");if(temp[1].equals("2018250")){if(map.containsKey(temp[2])){long cur=sdf.parse(temp[0]).getTime();int cost=0;String month=sdf2.format(sdf.parse(temp[0]));long pre=sdf.parse(map.get(temp[2])).getTime();double curTime=((double) (cur-pre))/1000;//秒if(curTime>1800)//大于半小時{cost+=10;//收10元double hour= Math.ceil(curTime/(60*60));//小時if(hour>2)//兩小時以上cost+=(hour-2)*2;}profit.put(month,profit.getOrDefault(month,0)+cost);//記錄map.remove(temp[2]);}elsemap.put(temp[2],temp[0]);cnt++;}line = br.readLine();}} catch (Exception e) {e.printStackTrace();}return list;}public static void main(String[] args) {long c=System.currentTimeMillis();List<String> list=readTxt("cars2.txt");System.out.println("運行時間(秒):"+(double)(System.currentTimeMillis()-c)/1000+"秒");for(String s:profit.keySet())System.out.println(s+":"+profit.get(s));}
}

日志挖掘——并行改造

1、讀取文件并行化(數據分割)

具體做法:針對之前一個大日志文件,根據時間(例如按照日期)進行切割,切割為多個小文件。讀取時,每天的數據分別讀取

2、處理并行化(任務分割)

具體做法:針對每個日期,分別進行各種掃描和統計;對于跨日期的數據,存放在公用地方,到時再配對處理

public class CountPark2 {static Map<String,ArrayList<String>> profit=new HashMap<>();static   Map<String,String> map=new HashMap<>();static long cnt=0,time=0;static   SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");public static void readTxt(String fileName){List<String> list=new ArrayList<>();try { // 防止文件建立或讀取失敗,用catch捕捉錯誤并打印,也可以throw/* 讀入TXT文件 */File filename = new File(fileName); // 要讀取以上路徑的input。txt文件InputStreamReader reader = new InputStreamReader(new FileInputStream(filename),"gbk"); // 建立一個輸入流對象readerBufferedReader br = new BufferedReader(reader); // 建立一個對象,它把文件內容轉成計算機能讀懂的語言String line = "";line = br.readLine();while (line != null) {String[] temp=line.split(",");if(temp[1].equals("2018250")){long cur=sdf.parse(temp[0]).getTime();      String month=sdf2.format(sdf.parse(temp[0]));if(!profit.containsKey(month)) profit.put(month,new ArrayList<>());profit.get(month).add(line);}line = br.readLine();} }catch (Exception e) {e.printStackTrace();}}public static void writeTxt(String content,String filename){try {File writename = new File("src/logminning/reso/"+filename); // 相對路徑,如果沒有則要建立一個新的output.txt文件writename.createNewFile(); // 創建新文件BufferedWriter out = new BufferedWriter(new FileWriter(writename));out.write(content); // \r\n即為換行out.flush(); // 把緩存區內容壓入文件out.close(); // 最后記得關閉文件} catch (Exception e) {e.printStackTrace();}}public void handle(){readTxt("cars2.txt");for(String c:profit.keySet()){StringBuilder stringBuilder=new StringBuilder();for(String t:profit.get(c))stringBuilder.append(t).append("\r\n");writeTxt(stringBuilder.toString(),c);}}public static void main(String[] args) {ConcurrentHashMap<String, PriorityBlockingQueue<Long>> concurrentHashMap=new ConcurrentHashMap<>();BlockingQueue<String> blockingQueue=new LinkedBlockingQueue<>();ExecutorService executorService= Executors.newFixedThreadPool(8);CountDownLatch countDownLatch=new CountDownLatch(6);System.out.println("Start ReadFile:"+formatter.format(new Date()));for(int i=1;i<=6;i++){executorService.execute(new ReadFileThread(concurrentHashMap,"src/logminning/reso/2020-0"+String.valueOf(i)+".txt",countDownLatch));}try {countDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("End ReadFile:"+formatter.format(new Date()));
/*        int used=0;for(String c:concurrentHashMap.keySet()){if(concurrentHashMap.get(c).size()==2) continue;System.out.println(c+':'+concurrentHashMap.get(c).size());used+=concurrentHashMap.get(c).size();}System.out.println(used);*/Res res=new Res();countDownLatch=new CountDownLatch(6);for(int i=0;i<26;i+=5){executorService.execute(new handler(concurrentHashMap,countDownLatch,i,res));}try {countDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("End Process:"+formatter.format(new Date()));System.out.println(res.getTar()+":"+res.getUsed());executorService.shutdown();}
}
public class handler implements Runnable{CountDownLatch countDownLatch;ConcurrentHashMap<String, PriorityBlockingQueue<Long>> concurrentHashMap;int s;Res res;public handler(ConcurrentHashMap<String, PriorityBlockingQueue<Long>> concurrentHashMap,CountDownLatch countDownLatch,int s,Res res) {this.s=s;this.res=res;this.countDownLatch=countDownLatch;this.concurrentHashMap = concurrentHashMap;}@Overridepublic void run() {try {for(String c:concurrentHashMap.keySet() ){int cur=c.charAt(1)-'A';if(cur>=s&&cur<s+5){PriorityBlockingQueue<Long> priorityQueue=concurrentHashMap.get(c);while (priorityQueue.size()>0){long c1=priorityQueue.take(),c2=priorityQueue.take();res.add((c2-c1)/1000);}}}} catch (Exception e) {e.printStackTrace();}countDownLatch.countDown();}
}
public class ReadFileThread implements Runnable {private ConcurrentHashMap<String, PriorityBlockingQueue<Long>> concurrentHashMap;private String fileName;CountDownLatch countDownLatch;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");public ReadFileThread(ConcurrentHashMap<String, PriorityBlockingQueue<Long>> concurrentHashMap, String filename, CountDownLatch countDownLatch) {this.concurrentHashMap = concurrentHashMap;this.fileName = filename;this.countDownLatch=countDownLatch;}@Overridepublic void run() {try { // 防止文件建立或讀取失敗,用catch捕捉錯誤并打印,也可以throw/* 讀入TXT文件 */File filename = new File(fileName); // 要讀取以上路徑的input。txt文件InputStreamReader reader = new InputStreamReader(new FileInputStream(filename),"UTF-8"); // 建立一個輸入流對象readerBufferedReader br = new BufferedReader(reader); // 建立一個對象,它把文件內容轉成計算機能讀懂的語言String line = "";line = br.readLine();while (line != null) {String[] temp=line.split(",");if(!concurrentHashMap.containsKey(temp[2]))concurrentHashMap.put(temp[2],new PriorityBlockingQueue<>());synchronized (this){long cur=sdf.parse(temp[0]).getTime(); concurrentHashMap.get(temp[2]).add(cur);}line = br.readLine();}} catch (Exception e) {e.printStackTrace();}countDownLatch.countDown();}
}
public class Res {private   long tar = 0;private  int used=0;public long getTar() {return tar;}public int getUsed() {return used;}public synchronized void add(long c){tar+=c;used++;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392184.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392184.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392184.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《人人都該買保險》讀書筆記

內容目錄&#xff1a; 1.你必須知道的保險知識 2.家庭理財的必需品 3.保障型保險產品 4.儲蓄型保險產品 5.投資型保險產品 6.明明白白買保險 現在我所在的公司Manulife是一家金融保險公司&#xff0c;主打業務就是保險&#xff0c;因此我需要熟悉一下保險的基礎知識&#xff0c…

Linux下查看txt文檔

當我們在使用Window操作系統的時候&#xff0c;可能使用最多的文本格式就是txt了&#xff0c;可是當我們將Window平臺下的txt文本文檔復制到Linux平臺下查看時&#xff0c;發現原來的中文所有變成了亂碼。沒錯&#xff0c; 引起這個結果的原因就是兩個平臺下&#xff0c;編輯器…

如何擊敗騰訊_擊敗股市

如何擊敗騰訊個人項目 (Personal Proyects) Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an…

滑塊 組件_組件制作:如何使用鏈接的輸入創建滑塊

滑塊 組件by Robin Sandborg羅賓桑德伯格(Robin Sandborg) 組件制作&#xff1a;如何使用鏈接的輸入創建滑塊 (Component crafting: how to create a slider with a linked input) Here at Stacc, we’re huge fans of React and the render-props pattern. When it came time…

配置靜態IPV6 NAT-PT

一.概述&#xff1a; IPV6 NAT-PT( Network Address Translation - Port Translation)應用與ipv4和ipv6網絡互訪的情況&#xff0c;根據參考鏈接配置時出現一些問題&#xff0c;所以記錄下來。參考鏈接&#xff1a;http://www.cisco.com/en/US/tech/tk648/tk361/technologies_c…

linux 線程與進程 pid,linux下線程所屬進程號問題

這一段看《unix環境高級編程》&#xff0c;一個關于線程的小例子。#include#include#includepthread_t ntid;void printids(const char *s){pid_t pid;pthread_t tid;pidgetpid();tidpthread_self();printf("%s pid %u tid %u (0x%x)n",s,(unsigned int)pid,(unsigne…

python3虛擬環境中解決 ModuleNotFoundError: No module named '_ssl'

前提是已經安裝了openssl 問題 當我在python3虛擬環境中導入ssl模塊時報錯&#xff0c;報錯如下&#xff1a; (py3) [rootlocalhost Python-3.6.3]# python3 Python 3.6.3 (default, Nov 19 2018, 14:18:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type "help…

python 使用c模塊_您可能沒有使用(但應該使用)的很棒的Python模塊

python 使用c模塊by Adam Goldschmidt亞當戈德施密特(Adam Goldschmidt) 您可能沒有使用(但應該使用)的很棒的Python模塊 (Awesome Python modules you probably aren’t using (but should be)) Python is a beautiful language, and it contains many built-in modules that…

分布與并行計算—生產者消費者模型實現(Java)

在實際的軟件開發過程中&#xff0c;經常會碰到如下場景&#xff1a;某個模塊負責產生數據&#xff0c;這些數據由另一個模塊來負責處理&#xff08;此處的模塊是廣義的&#xff0c;可以是類、函數、線程、進程等&#xff09;。產生數據的模塊&#xff0c;就形象地稱為生產者&a…

通過Xshell登錄遠程服務器實時查看log日志

主要想總結以下幾點&#xff1a; 1.如何使用生成密鑰的方式來登錄Xshell連接遠端服務器 2.在遠程服務器上如何上傳和下載文件&#xff08;下載log文件到本地&#xff09; 3.如何實時查看log&#xff0c;提取錯誤信息 一. 使用生成密鑰的方式來登錄Xshell連接遠端服務器 ssh登錄…

如何將Jupyter Notebook連接到遠程Spark集群并每天運行Spark作業?

As a data scientist, you are developing notebooks that process large data that does not fit in your laptop using Spark. What would you do? This is not a trivial problem.作為數據科學家&#xff0c;您正在開發使用Spark處理筆記本電腦無法容納的大數據的筆記本電腦…

是銀彈嗎?業務基線方法論

Fred.Brooks在1987年就提出&#xff1a;沒有銀彈。沒有任何一項技術或方法可以能讓軟件工程的生產力在十年內提高十倍。 我無意挑戰這個理論&#xff0c;只想討論一個方案&#xff0c;一個可能大幅提高業務系統開發效率的方案。 方案描述 我管這個方案叫做“由基線擴展…

linux core無權限,linux – 為什么編輯core_pattern受限制?

當我試圖為故意崩潰的程序生成核心文件時,最初的核心文件生成似乎被abrt-ccpp阻礙了.所以我嘗試用vim手動編輯/ proc / sys / kernel / core_pattern&#xff1a;> sudo vim /proc/sys/kernel/core_pattern當我試圖保存文件時,vim報告了這個錯誤&#xff1a;"/proc/sys…

nsa構架_我如何使用NSA的Ghidra解決了一個簡單的CrackMe挑戰

nsa構架by Denis Nu?iu丹尼斯努尤(Denis Nu?iu) 我如何使用NSA的Ghidra解決了一個簡單的CrackMe挑戰 (How I solved a simple CrackMe challenge with the NSA’s Ghidra) Hello!你好&#xff01; I’ve been playing recently a bit with Ghidra, which is a reverse engi…

分布與并行計算—生產者消費者模型隊列(Java)

在生產者-消費者模型中&#xff0c;在原有代碼基礎上&#xff0c;把隊列獨立為1個類實現&#xff0c;通過公布接口&#xff0c;由生產者和消費者調用。 public class Consumer implements Runnable {int n;CountDownLatch countDownLatch;public Consumer(BlockingQueue<In…

python 日志內容提取

問題&#xff1a;如下&#xff0c;一個很大的日志文件&#xff0c;提取 start: 到 end: 標志中間的內容 日志文件a.log xxxxx yyyyy start: start: hahahaha end: start: hahahahha end: ccccccc kkkkkkk cdcdcdcd start: hahahaha end: code import reisfindFalse with open(&…

同一服務器部署多個tomcat時的端口號修改詳情

2019獨角獸企業重金招聘Python工程師標準>>> 同一服務器部署多個tomcat時&#xff0c;存在端口號沖突的問題&#xff0c;所以需要修改tomcat配置文件server.xml&#xff0c;以tomcat7為例。 首先了解下tomcat的幾個主要端口&#xff1a;<Connector port"808…

linux優盤驅動目錄,Linux U盤加載陣列卡驅動步驟(.dd或img).doc

Linux U盤加載陣列卡驅動步驟(.dd或img)如果沒有Linux的機器,可以使用安裝光盤的Linux環境&#xff1a;將?U?盤完全慢速格式化&#xff0c;將驅動拷貝到U盤&#xff0c;將U盤插在服務器上&#xff0c;用Linux安裝光盤第一張啟動到圖形安裝界面&#xff0c;按Ctrl&#xff0b;…

第一章-從雙向鏈表學習設計

鏈表學習鏈表是一種動態的數據結構使用節點作為鏈表的基本單位存儲在節點包括數據元素和節點指針一個完整的數據鏈表應包括轉載于:https://www.cnblogs.com/cjxltd/p/7125747.html

twitter 數據集處理_Twitter數據清理和數據科學預處理

twitter 數據集處理In the past decade, new forms of communication, such as microblogging and text messaging have emerged and become ubiquitous. While there is no limit to the range of information conveyed by tweets and texts, often these short messages are …