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 br.ufrgs.inf.prosoft.cache.EventType;
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(EventType.HIT) || event.getType().equals(EventType.ADDITION))
.collect(Collectors.toList());
Map<String, Long> objectHasHits = hits.stream()
.collect(Collectors.groupingBy(
event -> event.getName() + "," + event.getIdentifier() + "," + event.getType().name().toLowerCase(),
Collectors.counting()));
try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
objectHasHits.entrySet().stream().forEach(entry -> {
try {
fileWriter.write(prefix + entry.getKey() + "," + entry.getValue() + "\n");
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "IOException {0}", ex);
}
});
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "file not found {0}", eventsPath);
}
}
}