Search in sources :

Example 11 with CsvToBeanBuilder

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;
}
Also used : CsvToBeanBuilder(com.opencsv.bean.CsvToBeanBuilder) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ValueObject(org.seed.core.entity.value.ValueObject) IOException(java.io.IOException) InternalException(org.seed.InternalException)

Example 12 with CsvToBeanBuilder

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);
    }
}
Also used : Path(java.nio.file.Path) CsvToBeanBuilder(com.opencsv.bean.CsvToBeanBuilder) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 13 with CsvToBeanBuilder

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
}
Also used : InputStreamReader(java.io.InputStreamReader) ZipEntry(java.util.zip.ZipEntry) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) ZipInputStream(java.util.zip.ZipInputStream) CsvToBeanBuilder(com.opencsv.bean.CsvToBeanBuilder) ZonedDateTime(java.time.ZonedDateTime) ByteArrayInputStream(java.io.ByteArrayInputStream) WeatherSensorValueHistoryDto(fi.livi.digitraffic.tie.dto.WeatherSensorValueHistoryDto) S3Object(com.amazonaws.services.s3.model.S3Object) Test(org.junit.jupiter.api.Test)

Aggregations

CsvToBeanBuilder (com.opencsv.bean.CsvToBeanBuilder)13 IOException (java.io.IOException)7 Files (java.nio.file.Files)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Collectors (java.util.stream.Collectors)4 File (java.io.File)3 FileReader (java.io.FileReader)3 Reader (java.io.Reader)3 ZERO (java.math.BigDecimal.ZERO)3 Map (java.util.Map)3 Optional (java.util.Optional)3 RequiredArgsConstructor (lombok.RequiredArgsConstructor)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Resource (org.springframework.core.io.Resource)3 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)3