Skip to content

Latest commit

 

History

History
64 lines (51 loc) · 3.62 KB

README.md

File metadata and controls

64 lines (51 loc) · 3.62 KB

DeCS: Diergo Easy CSV Streamable

This Java library supports parsing and generation of comma separated value (csv) data as defined in RFC 4180.

The format is a simple line based text format for table data. Many databases or spreadsheet software like MS-Excel can use this data as an interchange format. Each line is a row of the table, the column values are separated by a comma (or an other character). If the separator or quote character is part of a column value, the value has to be quoted.

Usage

The diergo.csv package contains a tool box to read and write CSV data using Java 8 Streams and functional interfaces. You can easily connect the functionaliy by using map(), filter() and collect() of a Stream. As there are no direct dependencies of the tools you can simply extend it for your needs by creating new lambdas or functional interfaces and inject them to mappings and filters:

  BiConsumer<String, RuntimeException> logger = (line, error) -> LoggerFactory.getLogger("CSV").warn("Cannot read CSV line '{}'", line, error);
  Function<String, List<Row>> parser = CsvParserBuilder.csvParser().separatedBy(',').handlingErrors(ErrorHandler.loggingErrors(logger).build();
  List<Map<String, String>> lines = Readers.asLines(new FileReader("input.csv", StandardCharsets.UTF_8))
      // CSV parser turns each line into a row
      .map(parser).flatMap(Collection::stream)
      // turn each line into a map, the first line is treated as header with column names
      .map(Maps.toMaps()).flatMap(Collection::stream)
      .collect(Collectors.toList());

  lines.stream()
      // create a stream of rows with an initial header row containing the column names
      .map(Maps.toRowsWithHeader()).flatMap(Collection::stream)
      // CSV printer turns each row into one line
      .map(CsvPrinterBuilder.csvPrinter().separatedBy(',').build())
      .collect(Appendables.toAppendable(new FileWriter("output.csv", StandardCharsets.UTF_8), '\n'));

There are more ready to use helper functions to filter and map at Rows, Maps and Values. Handling of comments, headers and separators is configured using the builders for CSV parser and printer. For the release notes, have a look at the change log.

Dependency Release

To integrate the library in your project, add the artifact decs of group de.diergo to your Java dependency management. At JitPack you can find examples for Gradle and Maven. The library has no external dependencies.

License

This library is published under Apache License Version 2.0.