Advertisement
cd62131

Read Write CSV with LocalDateTime Data

Jun 30th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.73 KB | None | 0 0
  1. import java.io.Writer;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.time.LocalDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.LinkedHashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Map.Entry;
  14. import java.util.Scanner;
  15. import java.util.TreeMap;
  16. import java.util.stream.Collectors;
  17.  
  18. public class Ranking {
  19.   class Record {
  20.     private LocalDateTime datetime;
  21.     private int data1, data2;
  22.  
  23.     public LocalDateTime getDatetime() {
  24.       return datetime;
  25.     }
  26.  
  27.     public void setDatetime(LocalDateTime datetime) {
  28.       this.datetime = datetime;
  29.     }
  30.  
  31.     public int getData1() {
  32.       return data1;
  33.     }
  34.  
  35.     public void setData1(int data1) {
  36.       this.data1 = data1;
  37.     }
  38.  
  39.     public int getData2() {
  40.       return data2;
  41.     }
  42.  
  43.     public void setData2(int data2) {
  44.       this.data2 = data2;
  45.     }
  46.   }
  47.  
  48.   private final static Path DATA_DIR = Paths.get("/home/data");
  49.   private final static String CSV_GLOB = "*.csv";
  50.   private final static Path OUT_PATH = Paths.get("/home/ranking/ranking.csv");
  51.   private final LocalDateTime beg;
  52.   private final LocalDateTime end;
  53.   private Map<String, List<Record>> all;
  54.   private Map<String, Integer> rank;
  55.  
  56.   public Ranking(LocalDateTime beg, LocalDateTime end) {
  57.     this.beg = beg;
  58.     this.end = end;
  59.     all = new TreeMap<>();
  60.     rank = null;
  61.   }
  62.  
  63.   private void integrate() {
  64.     if (!all.isEmpty()) {
  65.       return;
  66.     }
  67.     DateTimeFormatter dt = DateTimeFormatter.ofPattern("uuuu'/'MM'/'dd'T'HH':'mm':'ss");
  68.     try {
  69.       for (Path csv: Files.newDirectoryStream(DATA_DIR, CSV_GLOB)) {
  70.         String user = csv.getFileName().toString().replace(".csv", "");
  71.         boolean first = true;
  72.         for (Scanner in = new Scanner(csv); in.hasNextLine();) {
  73.           if (first) {
  74.             in.nextLine(); // for header
  75.             first = false;
  76.             continue;
  77.           }
  78.           String[] line = in.nextLine().split(",");
  79.           Record r = new Record();
  80.           r.setDatetime(LocalDateTime.parse(line[0] + "T" + line[1], dt));
  81.           r.setData1(Integer.parseInt(line[2]));
  82.           r.setData2(Integer.parseInt(line[3]));
  83.           if (!all.containsKey(user)) {
  84.             all.put(user, new ArrayList<>());
  85.           }
  86.           all.get(user).add(r);
  87.         }
  88.       }
  89.     } catch (Exception e) {
  90.       e.printStackTrace();
  91.     }
  92.     all.forEach((k, v) -> {
  93.       v.sort(new Comparator<Record>() {
  94.         @Override
  95.         public int compare(Record o1, Record o2) {
  96.           return o1.getDatetime().compareTo(o2.getDatetime());
  97.         }
  98.       });
  99.     });
  100.   }
  101.  
  102.   private void extract() {
  103.     if (rank != null) {
  104.       return;
  105.     }
  106.     rank = new LinkedHashMap<>();
  107.     // filtered with specify time and then summarize(Total)
  108.     all.forEach((k, v) -> {
  109.       rank.put(k,
  110.           v.stream()
  111.               .filter(r -> (r.getDatetime().isAfter(beg) || r.getDatetime().isEqual(beg))
  112.                   && (r.getDatetime().isBefore(end) || r.getDatetime().isEqual(end)))
  113.               .mapToInt(r -> r.getData1() + r.getData2()).sum());
  114.     });
  115.     // reverse sort on rank's value(Total)
  116.     rank = rank.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
  117.         .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1,
  118.             LinkedHashMap::new));
  119.   }
  120.  
  121.   public void writeCSV() {
  122.     System.out.println("read data...");
  123.     integrate();
  124.     for (Entry<String, List<Record>> e: all.entrySet()) {
  125.       System.out.println("USER: " + e.getKey() + ", LINES: " + e.getValue().size());
  126.     }
  127.     System.out.println("calc total...");
  128.     extract();
  129.     for (Entry<String, Integer> e: rank.entrySet()) {
  130.       System.out.println(e);
  131.     }
  132.     System.out.println("write csv to " + OUT_PATH);
  133.     try (Writer out = Files.newBufferedWriter(OUT_PATH)) {
  134.       StringBuilder sb = new StringBuilder("RANKING,USER,TOTAL\n");
  135.       int i = 1;
  136.       for (Entry<String, Integer> e: rank.entrySet()) {
  137.         sb.append(i++);
  138.         sb.append(",");
  139.         sb.append(e.getKey());
  140.         sb.append(",");
  141.         sb.append(e.getValue());
  142.         sb.append("\n");
  143.       }
  144.       out.write(sb.toString());
  145.     } catch (Exception e) {
  146.       e.printStackTrace();
  147.     }
  148.   }
  149.  
  150.   public static void main(String[] args) {
  151.     if (args.length != 3) {
  152.       return;
  153.     }
  154.     DateTimeFormatter dt = DateTimeFormatter.ofPattern("uuuu'/'MM'/'dd'T'HH':'mm");
  155.     new Ranking(LocalDateTime.parse(args[0] + "T" + args[1], dt),
  156.         LocalDateTime.parse(args[0] + "T" + args[2], dt)).writeCSV();
  157.   }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement