public static void main(String[] args) throws FileNotFoundException, IOException { CitiBikeReader<TripAnnotated> reader = new CitiBikeReader<>("/tmp", str -> new TripAnnotatedParser(str)); CsvConfiguration<TripAnnotated> config = new CsvConfiguration.Builder<>(TripAnnotated.class).build(); CsvWriter<TripAnnotated> csvWriter = new CsvWriter<>(config); try (FileOutputStream fos = new FileOutputStream(new File("/tmp/trips.csv"))) { reader.forEachCsvInZip(trips -> { try { csvWriter.writeAll(fos, trips); } catch (IOException e) { e.printStackTrace(); } }); } }
public static void main(String[] args) throws FileNotFoundException, IOException { CitiBikeReader<Trip> reader = new CitiBikeReader<>("/tmp", str -> new TripParser(str)); CsvConfiguration<Trip> csvConfiguration = new Builder<Trip>(configureEntityMapping()).build(); CsvWriter<Trip> csvWriter = new CsvWriter<>(csvConfiguration); try (FileOutputStream fos = new FileOutputStream(new File("/tmp/trips.csv"))) { reader.forEachCsvInZip(trips -> { try { csvWriter.writeAll(fos, trips); } catch (IOException e) { e.printStackTrace(); } }); } }
public static void main(String[] args) throws FileNotFoundException, IOException { CitiBikeReader<Trip> reader = new CitiBikeReader<>("/tmp", str -> new TripParser(str)); EntityInfoBuilder<Trip> entityInfo = new EntityInfoBuilder<>(Trip.class); entityInfo.addFields("bikeId", "tripduration", "starttime", "startStationId", "stoptime", "endStationId", "userType", "birthYear", "gender"); CsvConfiguration<Trip> csvConfiguration = new Builder<Trip>(entityInfo.build()).build(); CsvWriter<Trip> csvWriter = new CsvWriter<>(csvConfiguration); try (FileOutputStream fos = new FileOutputStream(new File("/tmp/trips.csv"))) { reader.forEachCsvInZip(trips -> { try { csvWriter.writeAll(fos, trips); } catch (IOException e) { e.printStackTrace(); } }); } }
public static void main(String[] args) throws FileNotFoundException, IOException { CitiBikeReader<Trip> reader = new CitiBikeReader<>("/tmp", str -> new TripParser(str)); EntityInfoBuilder<Trip> entityInfo = new EntityInfoBuilder<>(Trip.class); entityInfo.addFields("bikeId", "starttime", "startStationId", "stoptime", "endStationId", "userType"); CsvConfiguration<Trip> csvConfiguration = new Builder<>(Trip.class) .entityInfo(entityInfo.build()) .alwaysDelimitText(true) .fieldSeparator(';') .lineDelimiter("\n") .textDelimiter('\'') .header(false) .build(); CsvWriter<Trip> csvWriter = new CsvWriter<>(csvConfiguration); try (FileOutputStream fos = new FileOutputStream(new File("/tmp/trips.csv"))) { reader.forEachCsvInZip(trips -> { try { csvWriter.writeAll(fos, trips); } catch (IOException e) { e.printStackTrace(); } }); } }
@Test public void escapeLineDelimiterTest() throws IOException { config.textDelimiter('\''); String result = writeCsvToString(config.build(), new SomeEntity("John \nSmith", 10)); assertEquals("name,age" + ls + "'John \nSmith',10" + ls, result); }
@Test public void optionalTextDelimiterUsedTest() throws IOException { config.textDelimiter('"'); String result = writeCsvToString(config.build(), new SomeEntity("John \"Smith\"", 10)); assertEquals("name,age" + ls + "\"John \"\"Smith\"\"\",10" + ls, result); }
@Test public void accessorCanChangeType() throws IOException { EntityInfoBuilder<SomeEntity> entityBuilder = new EntityInfoBuilder<>(SomeEntity.class); entityBuilder.addField("name"); entityBuilder.addColumn("age", FieldTypeEnum.STRING, e -> e.getAge() + " years"); entityBuilder.addColumn("adult", FieldTypeEnum.STRING, e -> e.getAge() >= 18 ? "Si" : "No"); Builder<SomeEntity> config = new Builder<>(entityBuilder.build()); String result = writeCsvToString(config.build(), new SomeEntity("John", 10), new SomeEntity("Amanda", 34)); assertEquals("name,age,adult" + ls + "John,10 years,No" + ls + "Amanda,34 years,Si" + ls, result); }
@Test public void emptyCollectionWithoutHeaderIsEmpty() throws IOException { Builder<SomeEntity> config = new Builder<>(createBuilderForSomeEntity().build()); config.header(false); String result = writeCsvToString(config.build(), Collections.emptyList()); assertEquals("", result); }
@Test public void optionalTextDelimiterNotUsedTest() throws IOException { config.textDelimiter('$'); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("name,age" + ls + "John,10" + ls, result); }
@Test public void escapeFieldSeparatorTest() throws IOException { config.fieldSeparator(';').textDelimiter('\''); String result = writeCsvToString(config.build(), new SomeEntity("John;Smith", 10)); assertEquals("name;age" + ls + "'John;Smith';10" + ls, result); }
@Test public void fieldSeparatorTest() throws IOException { config.fieldSeparator(';'); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("name;age" + ls + "John;10" + ls, result); }
@Test public void canChangeDefaultSerializer() throws IOException { EntityInfoBuilder<SomeEntity> entityBuilder = new EntityInfoBuilder<>(SomeEntity.class); entityBuilder.addFields("name", "age"); entityBuilder.addColumn("adult", FieldTypeEnum.BOOLEAN, e -> e.getAge() >= 18); BaseTypeSerializer serializer = new BaseTypeSerializer(); serializer.add(FieldTypeEnum.BOOLEAN, (obj) -> ((Boolean) obj).booleanValue() ? "1" : "0"); Builder<SomeEntity> config = new Builder<>(entityBuilder.build()); config.typeSerializer(serializer); String result = writeCsvToString(config.build(), new SomeEntity("John", 10), new SomeEntity("Amanda", 34)); assertEquals("name,age,adult" + ls + "John,10,0" + ls + "Amanda,34,1" + ls, result); }
@Test public void elementWithoutHeaderIsAlone() throws IOException { Builder<SomeEntity> config = new Builder<>(createBuilderForSomeEntity().build()); config.header(false); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("John,10" + ls, result); }
@Test public void mandatoryTextDelimiterUsedTest() throws IOException { config.textDelimiter('\'').alwaysDelimitText(true); String result = writeCsvToString(config.build(), new SomeEntity("John 'Smith'", 10), new SomeEntity("Amanda", 20)); assertEquals("'name','age'" + ls + "'John ''Smith''','10'" + ls + "'Amanda','20'" + ls, result); }