|
| 1 | +package fun.seabird; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.Reader; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.time.LocalDate; |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.time.LocalTime; |
| 10 | +import java.time.format.DateTimeFormatter; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Comparator; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.concurrent.atomic.AtomicInteger; |
| 16 | +import java.util.function.Consumer; |
| 17 | + |
| 18 | +import org.apache.commons.csv.CSVFormat; |
| 19 | +import org.apache.commons.csv.CSVParser; |
| 20 | +import org.apache.commons.csv.CSVRecord; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.apache.commons.lang3.time.StopWatch; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import reactor.core.publisher.Flux; |
| 27 | +import reactor.core.scheduler.Schedulers; |
| 28 | + |
| 29 | +public class EbirdCsvParser |
| 30 | +{ |
| 31 | + private static final Logger logger = LoggerFactory.getLogger(EbirdCsvParser.class); |
| 32 | + |
| 33 | + public enum ParseMode {single,parallel} |
| 34 | + |
| 35 | + public enum PreSort {none,obsDt} |
| 36 | + |
| 37 | + private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a"); |
| 38 | + |
| 39 | + private static final DateTimeFormatter csvDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm a"); |
| 40 | + |
| 41 | + private static final AtomicInteger linesProcessed = new AtomicInteger(0); |
| 42 | + |
| 43 | + static LocalDateTime parseSubDate(CSVRecord record) |
| 44 | + { |
| 45 | + if (record.getRecordNumber() == 1l) |
| 46 | + return LocalDateTime.MIN; |
| 47 | + |
| 48 | + String obsTimeStr = record.get(12); |
| 49 | + if (StringUtils.isBlank(obsTimeStr)) |
| 50 | + obsTimeStr = "12:00 AM"; |
| 51 | + |
| 52 | + return LocalDateTime.parse(record.get(11) + " " + obsTimeStr, csvDtf); |
| 53 | + } |
| 54 | + |
| 55 | + private static EbirdCsvRow parseCsvLine(CSVRecord record) |
| 56 | + { |
| 57 | + if (record.getRecordNumber() == 1l) |
| 58 | + return null; // skip the header |
| 59 | + |
| 60 | + EbirdCsvRow row = new EbirdCsvRow(); |
| 61 | + |
| 62 | + row.setSubId(record.get(0)); |
| 63 | + row.setCommonName(record.get(1)); |
| 64 | + row.setSciName(record.get(2)); |
| 65 | + row.setTaxonOrder(Double.parseDouble(record.get(3))); |
| 66 | + row.setCount(record.get(4)); |
| 67 | + row.setSubnat1Code(record.get(5)); |
| 68 | + row.setSubnat2Name(record.get(6)); |
| 69 | + row.setLocId(record.get(7)); |
| 70 | + row.setLocName(record.get(8)); |
| 71 | + row.setLat(Double.parseDouble(record.get(9))); |
| 72 | + row.setLng(Double.parseDouble(record.get(10))); |
| 73 | + row.setDate(LocalDate.parse(record.get(11))); // Assuming the date format is ISO-8601 (yyyy-MM-dd) |
| 74 | + |
| 75 | + String timeStr = record.get(12); |
| 76 | + if (!StringUtils.isEmpty(timeStr)) |
| 77 | + row.setTime(LocalTime.parse(timeStr,timeFormatter)); // Assuming the time format is ISO-8601 (HH:mm:ss) |
| 78 | + |
| 79 | + row.setProtocol(record.get(13)); |
| 80 | + |
| 81 | + if (!record.get(14).isEmpty()) |
| 82 | + row.setDuration(Integer.parseInt(record.get(14))); |
| 83 | + else |
| 84 | + row.setDuration(0); |
| 85 | + |
| 86 | + row.setCompleteChecklist(record.get(15).equals("1")); |
| 87 | + |
| 88 | + if (record.size() > 16 && !record.get(16).isEmpty()) { |
| 89 | + row.setDistanceKm(Double.parseDouble(record.get(16))); |
| 90 | + } |
| 91 | + |
| 92 | + if (record.size() > 17 && !record.get(17).isEmpty()) { |
| 93 | + row.setAreaHa(Double.parseDouble(record.get(17))); |
| 94 | + } |
| 95 | + |
| 96 | + if (record.size() > 18 && !record.get(18).isEmpty()) |
| 97 | + row.setPartySize(Integer.parseInt(record.get(18))); |
| 98 | + |
| 99 | + if (record.size() > 19) |
| 100 | + row.setBreedingCode(record.get(19)); |
| 101 | + |
| 102 | + // Parsing the space-separated String into a List of Long values |
| 103 | + if (record.size() > 22) { |
| 104 | + String assetIdsString = record.get(22); |
| 105 | + List<Long> assetIds = Arrays.stream(assetIdsString.split(" ")) |
| 106 | + .map(Long::parseLong) |
| 107 | + .toList(); |
| 108 | + row.setAssetIds(assetIds); |
| 109 | + } |
| 110 | + |
| 111 | + return row; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Parses eBird CSV file using Apache Commons CSV library, and processes each line in parallel. |
| 116 | + * |
| 117 | + * @param csvFile The path to the CSV file to be parsed. |
| 118 | + * @throws IOException If an I/O error occurs while reading the CSV file. |
| 119 | + */ |
| 120 | + public static void parseCsv(Path csvFile,ParseMode mode,PreSort preSort,Consumer<EbirdCsvRow> rowProcessor) throws IOException |
| 121 | + { |
| 122 | + logger.info("Parsing " + csvFile + "..."); |
| 123 | + |
| 124 | + linesProcessed.set(0); |
| 125 | + |
| 126 | + try (Reader fileReader = Files.newBufferedReader(csvFile); |
| 127 | + CSVParser csvParser = new CSVParser(fileReader, |
| 128 | + CSVFormat.DEFAULT.builder().setSkipHeaderRecord(true).build())) { |
| 129 | + |
| 130 | + StopWatch stopwatch = StopWatch.createStarted(); |
| 131 | + |
| 132 | + Iterable<CSVRecord> records; |
| 133 | + if (PreSort.obsDt == preSort) |
| 134 | + { |
| 135 | + // Read all lines and sort by date and time columns |
| 136 | + List<CSVRecord> recordsList = csvParser.getRecords(); |
| 137 | + recordsList.sort(Comparator.comparing(EbirdCsvParser::parseSubDate)); |
| 138 | + logger.info("Read " + (recordsList.size()-1) + " and sorted eBird observations in " + stopwatch.getTime(TimeUnit.SECONDS) + " seconds"); |
| 139 | + records = recordsList; |
| 140 | + } |
| 141 | + else |
| 142 | + records = csvParser; |
| 143 | + |
| 144 | + Consumer<CSVRecord> csvRecordConsumer = new Consumer<CSVRecord>() { |
| 145 | + @Override |
| 146 | + public void accept(CSVRecord record) |
| 147 | + { |
| 148 | + EbirdCsvRow row = parseCsvLine(record); |
| 149 | + if (row == null) //header row |
| 150 | + return; |
| 151 | + |
| 152 | + rowProcessor.accept(row); |
| 153 | + linesProcessed.getAndIncrement(); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + switch (mode) |
| 158 | + { |
| 159 | + case parallel: |
| 160 | + Flux.fromIterable(records).parallel().runOn(Schedulers.parallel()).sequential(25000).doOnNext(csvRecordConsumer).then().block(); |
| 161 | + break; |
| 162 | + case single: |
| 163 | + Flux.fromIterable(records).doOnNext(csvRecordConsumer).then().block(); |
| 164 | + break; |
| 165 | + } |
| 166 | + |
| 167 | + stopwatch.stop(); |
| 168 | + logger.info("Processed " + linesProcessed.get() + " eBird observations in " + stopwatch.getTime(TimeUnit.SECONDS) + " seconds"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments