use of com.opencsv.bean.CsvToBeanBuilder in project seed by euu-rocks.
the class CSVProcessor method doImport.
@Override
public TransferResult doImport(ImportOptions options, InputStream inputStream) throws ValidationException {
Assert.notNull(options, C.OPTIONS);
Assert.notNull(inputStream, "inputStream");
final TransferResult result = new TransferResult(options);
try (Reader reader = new InputStreamReader(inputStream, getCharset())) {
final CsvToBeanBuilder<ValueObject> builder = new CsvToBeanBuilder<ValueObject>(reader).withType(getObjectClass()).withMappingStrategy(createMappingStrategy()).withIgnoreLeadingWhiteSpace(true).withIgnoreEmptyLine(true);
if (getTransfer().isHeader()) {
builder.withSkipLines(1);
}
if (getTransfer().getSeparatorChar() != null) {
builder.withSeparator(getTransfer().getSeparatorChar().charAt(0));
}
if (getTransfer().getQuoteChar() != null) {
builder.withQuoteChar(getTransfer().getQuoteChar().charAt(0));
}
if (getTransfer().getEscapeChar() != null) {
builder.withEscapeChar(getTransfer().getEscapeChar().charAt(0));
}
saveObjects(builder.build().parse(), options, result);
} catch (IOException ioe) {
throw new InternalException(ioe);
}
return result;
}
use of com.opencsv.bean.CsvToBeanBuilder in project jsoar by soartech.
the class PerformanceTesting method appendToSummaryFileInternal.
private void appendToSummaryFileInternal(ConfigurationTest test, String soarVariant, Path soarPath, int dcs, String fileSuffix) throws IllegalStateException, IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
File testFile = getTestPath(test.getSettings(), test.getName(), soarVariant, soarPath, dcs, "-raw").toFile();
List<RawResults> rawResults = new CsvToBeanBuilder<RawResults>(new FileReader(testFile)).withType(RawResults.class).withSkipLines(1).build().parse();
RawResults combinedRawResults = rawResults.stream().reduce(new RawResults(), (subtotal, element) -> subtotal.accumulate(element));
Results summary = new Results(test.getName() + "-" + dcs, test.getFile(), soarVariant, soarPath);
summary.updateStats(combinedRawResults);
Path finalPath = test.getSettings().getCsvDirectory().resolve(test.getSettings().getSummaryFile());
boolean newFile = !Files.exists(finalPath);
try (Writer writer = new FileWriter(finalPath.toFile(), true)) {
if (newFile) {
writer.write(String.join(",", Results.header) + "\n");
}
StatefulBeanToCsv<Results> beanToCsv = new StatefulBeanToCsvBuilder<Results>(writer).build();
beanToCsv.write(summary);
}
}
use of com.opencsv.bean.CsvToBeanBuilder in project digitraffic-road by tmfg.
the class SensorDataS3WriterTest method s3Bucket.
@Test
public void s3Bucket() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime to = now.truncatedTo(ChronoUnit.HOURS);
ZonedDateTime from = to.minusHours(1);
initDBContent(now);
sensorDataS3Properties.setRefTime(from);
int origCount = builder.getElementCountAt(1);
int sum = writer.writeSensorData(from, to);
assertEquals(origCount, sum, "element count mismatch");
// repository.findAll().stream().forEach(item -> log.info("item: {}, measured: {}", item.getRoadStationId(), item.getMeasuredTime()));
// Check S3 object
ObjectListing list = amazonS3.listObjects(sensorDataS3Properties.getS3BucketName());
assertFalse(list.getObjectSummaries().isEmpty(), "No elements");
String objectName = list.getObjectSummaries().get(0).getKey();
log.info("Read object {} from {}", objectName, sensorDataS3Properties.getS3BucketName());
S3Object s3Object = amazonS3.getObject(sensorDataS3Properties.getS3BucketName(), objectName);
assertNotNull(s3Object, "S3 object not found");
/**
* try {
* FileUtils.copyInputStreamToFile(new ByteArrayInputStream(s3Object.getObjectContent().readAllBytes()), new File("temppi.zip"));
* } catch (Exception e) {
* e.printStackTrace();
* }
*/
try (ByteArrayInputStream bis = new ByteArrayInputStream(s3Object.getObjectContent().readAllBytes());
ZipInputStream in = new ZipInputStream(bis)) {
// Get entry
ZipEntry entry = in.getNextEntry();
log.info("entry {}", entry.getName());
List<WeatherSensorValueHistoryDto> items = new CsvToBeanBuilder<WeatherSensorValueHistoryDto>(new InputStreamReader(in)).withType(WeatherSensorValueHistoryDto.class).withSeparator(',').build().parse();
in.closeEntry();
log.info("Gotta items {}", items);
} catch (Exception e) {
log.error("zip error:", e);
// Assert.fail("failed to process zip: " + objectName);
}
// TODO! Check object is .zip and document is .csv and actual content is readable
}
Aggregations