我正在使用parallelstream()解析csv文件,并注意到一些奇怪的行为。问题代码如下:
public List<AddressRaw> parseAddress(List<CSVRecord> records) {
return records.subList(1, records.size())
.parallelStream()
.flatMap(record -> {
// There should only be 10 columns in the CSV
if (record.size() > 10) {
log.error("Too many columns were detected on row number " + record.getRecordNumber());
throw new IllegalArgumentException("Too many columns were detected on row number " + record.getRecordNumber());
}
我调用上述方法的控制器:
try {
// csv files are identified by either text/csv or application/vnd.ms-excel
if (file.isEmpty() || Objects.isNull(file.getContentType()) || !(file.getContentType().equals("text/csv") || file.getContentType().equals("application/vnd.ms-excel"))) {
log.error("File uploaded is not a csv");
throw new BadRequestException("File uploaded is not a csv");
}
List<CSVRecord> records = CSVHelper.recordsFromBytes(file.getBytes());
// convert the rows into a format mirroring a db transit time
List<AddressRaw> rawAddresss = addressService.parseAddress(records);
if (rawAddresss.isEmpty()) {
throw new BadRequestException("No addresses found");
}
// execute the upsert in batches (Upserts count as two changes, as the insert fails then an update occurs)
Integer addressesInserted = addressService.batchInsertAddresses(rawAddresss, targetUserId);
LocalDateTime end = LocalDateTime.now();
log.info("Upserted {} AddressRaw records in: {}ms", addressesInserted, start.until(end, ChronoUnit.MILLIS));
return ResponseEntity.ok(addressesInserted);
} catch (IOException e) {
log.error("Unable to parse csv file: ", e);
} catch (DataAccessException e) {
log.error("Unable to execute SQL: ", e);
}
有时我在前端遇到的错误是“在第3行上检测到的列太多”,这是我所期望的。有时我得到的错误是“java.lang.illegalargumentexception:在第3行上检测到太多列”,我不太清楚为什么它也会打印出异常名称?有什么我不知道的吗?
暂无答案!
目前还没有任何答案,快来回答吧!