Reducer.java
Home
/
src /
main /
java /
br /
ufrgs /
inf /
prosoft /
cache /
tools /
Reducer.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.ufrgs.inf.prosoft.cache.tools;
import br.ufrgs.inf.prosoft.cache.CacheEvent;
import com.google.gson.Gson;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
*
* @author romulo
*/
public class Reducer {
private static final Logger LOGGER = Logger.getLogger(Reducer.class.getName());
public static void reduce(String eventsPath, String reducePath, String prefix) {
try (Stream<String> lines = Files.lines(Paths.get(eventsPath))) {
Gson gson = new Gson();
List<CacheEvent> hits = lines.map(line -> gson.fromJson(line, CacheEvent.class))
.filter(event -> event.getType().equals("HIT"))
.collect(Collectors.toList());
Map<String, Long> objectHasHits = hits.stream()
.collect(Collectors.groupingBy(
event -> event.getIdentifier() + "," + event.getType().toLowerCase(),
Collectors.counting()));
try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
objectHasHits.values().stream().forEach(value -> {
try {
fileWriter.write(prefix + value + "\n");
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "IOException {0}", ex);
}
});
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "file not found {0}", eventsPath);
}
}
}