Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Writer;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.stream.Collectors;
- public class Ranking {
- class Record {
- private LocalDateTime datetime;
- private int data1, data2;
- public LocalDateTime getDatetime() {
- return datetime;
- }
- public void setDatetime(LocalDateTime datetime) {
- this.datetime = datetime;
- }
- public int getData1() {
- return data1;
- }
- public void setData1(int data1) {
- this.data1 = data1;
- }
- public int getData2() {
- return data2;
- }
- public void setData2(int data2) {
- this.data2 = data2;
- }
- }
- private final static Path DATA_DIR = Paths.get("/home/data");
- private final static String CSV_GLOB = "*.csv";
- private final static Path OUT_PATH = Paths.get("/home/ranking/ranking.csv");
- private final LocalDateTime beg;
- private final LocalDateTime end;
- private Map<String, List<Record>> all;
- private Map<String, Integer> rank;
- public Ranking(LocalDateTime beg, LocalDateTime end) {
- this.beg = beg;
- this.end = end;
- all = new TreeMap<>();
- rank = null;
- }
- private void integrate() {
- if (!all.isEmpty()) {
- return;
- }
- DateTimeFormatter dt = DateTimeFormatter.ofPattern("uuuu'/'MM'/'dd'T'HH':'mm':'ss");
- try {
- for (Path csv: Files.newDirectoryStream(DATA_DIR, CSV_GLOB)) {
- String user = csv.getFileName().toString().replace(".csv", "");
- boolean first = true;
- for (Scanner in = new Scanner(csv); in.hasNextLine();) {
- if (first) {
- in.nextLine(); // for header
- first = false;
- continue;
- }
- String[] line = in.nextLine().split(",");
- Record r = new Record();
- r.setDatetime(LocalDateTime.parse(line[0] + "T" + line[1], dt));
- r.setData1(Integer.parseInt(line[2]));
- r.setData2(Integer.parseInt(line[3]));
- if (!all.containsKey(user)) {
- all.put(user, new ArrayList<>());
- }
- all.get(user).add(r);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- all.forEach((k, v) -> {
- v.sort(new Comparator<Record>() {
- @Override
- public int compare(Record o1, Record o2) {
- return o1.getDatetime().compareTo(o2.getDatetime());
- }
- });
- });
- }
- private void extract() {
- if (rank != null) {
- return;
- }
- rank = new LinkedHashMap<>();
- // filtered with specify time and then summarize(Total)
- all.forEach((k, v) -> {
- rank.put(k,
- v.stream()
- .filter(r -> (r.getDatetime().isAfter(beg) || r.getDatetime().isEqual(beg))
- && (r.getDatetime().isBefore(end) || r.getDatetime().isEqual(end)))
- .mapToInt(r -> r.getData1() + r.getData2()).sum());
- });
- // reverse sort on rank's value(Total)
- rank = rank.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1,
- LinkedHashMap::new));
- }
- public void writeCSV() {
- System.out.println("read data...");
- integrate();
- for (Entry<String, List<Record>> e: all.entrySet()) {
- System.out.println("USER: " + e.getKey() + ", LINES: " + e.getValue().size());
- }
- System.out.println("calc total...");
- extract();
- for (Entry<String, Integer> e: rank.entrySet()) {
- System.out.println(e);
- }
- System.out.println("write csv to " + OUT_PATH);
- try (Writer out = Files.newBufferedWriter(OUT_PATH)) {
- StringBuilder sb = new StringBuilder("RANKING,USER,TOTAL\n");
- int i = 1;
- for (Entry<String, Integer> e: rank.entrySet()) {
- sb.append(i++);
- sb.append(",");
- sb.append(e.getKey());
- sb.append(",");
- sb.append(e.getValue());
- sb.append("\n");
- }
- out.write(sb.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- if (args.length != 3) {
- return;
- }
- DateTimeFormatter dt = DateTimeFormatter.ofPattern("uuuu'/'MM'/'dd'T'HH':'mm");
- new Ranking(LocalDateTime.parse(args[0] + "T" + args[1], dt),
- LocalDateTime.parse(args[0] + "T" + args[2], dt)).writeCSV();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement