Advertisement
amitsen01ei

ReadCsv.java

Feb 4th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | Source Code | 0 0
  1. import java.nio.file.Files;
  2. import java.nio.file.Path;
  3. import java.time.Duration;
  4. import java.time.Instant;
  5. import java.util.Arrays;
  6. import java.util.Map;
  7. import java.util.function.Function;
  8. import java.util.stream.Collectors;
  9.  
  10. import static common.IO.println;
  11. import static java.util.Map.Entry;
  12.  
  13. /**
  14.  * A csv file of size 1.85 GB containing 10000000 rows of uuids each row having 5 comma separated uuid
  15.  */
  16. final String inputPath = "/Users/amit.sen/Desktop/uuids-medium.csv";
  17.  
  18. void main() {
  19.     withStream();
  20. }
  21.  
  22. void withStream() {
  23.     try (var inputStream = Files.lines(Path.of(inputPath))) {
  24.  
  25.         var start = Instant.now();
  26.  
  27.         inputStream
  28.                 .parallel()
  29.                 .map(this::rowReduce)
  30.                 .flatMap(charMap -> charMap.entrySet().stream())
  31.                 .collect(Collectors.toMap(Entry::getKey, Entry::getValue, Long::sum))
  32.                 .entrySet()
  33.                 .forEach(System.out::println);
  34.  
  35.         println(STR."Time Taken : \{Duration.between(start, Instant.now()).getSeconds()} seconds");
  36.  
  37.     } catch (Exception ex) {
  38.         System.err.println(ex.getMessage());
  39.     }
  40. }
  41.  
  42. Map<Character, Long> rowReduce(String row) {
  43.     return Arrays.stream(row.split(","))
  44.             .flatMap(uuid -> uuid.chars().mapToObj(i -> (char) i))
  45.             .filter(ch -> ch != '-')
  46.             .collect(Collectors.toMap(Function.identity(), _ -> 1L, Long::sum));
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement