Coder Social home page Coder Social logo

mklueh / sexy-csv Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 111 KB

Java CSV parser / creator with the goal of providing the leanest API possible - based on Java 8 Streaming API

License: MIT License

Java 100.00%
csv parser java streaming api lombok tabulator skipping sexy

sexy-csv's Introduction

[DEPRECATED] This project was moved to the company's internal repository where I work and is not actively maintained anymore here.

SexyCSV

Java CSV parser / creator with the goal of providing the leanest API possible - based on Java 8 Streaming API

Deals with certain edge cases other parsers aren't capable of and does not require pre-processing for metadata trimming.

Use-Case

You don´t want to rely on rather big libraries such as Apache Commons CSV, dislike their API or lacking features such as line skipping, filling missing cells correctly with null using tabulator delimiter this library might be all you need.

Features

SexyCsv consists of two components:

1. SexyCsv.Parser (Read CSV)

2. SexyCsv.Creator (Write CSV)

and works with Java Annotations to map header columns to object fields

@Data
public class TestEntity {

    @CSVColumn(value = "id", position = 0)
    public String id;

    @CSVColumn(value = "name", position = 1)
    public String name;

    @CSVColumn(value = "age")
    private Integer age;

    @CSVColumn(value = "country")
    private String country;
}

SexyCsv.Parser
  • Automated header detection
    • Based on header row in CSV files
    • Based on Java class entity
  • Parsing CSV files directly to Java Object Streams by using the @CsvColumn annotation on column fields
  • Parsing CSV files to Row Streams
  • Skipping lines above header
  • Duplicated header detection
  • Filtering out invalid data
  • Setting tokenizer for special cases
  • Correctly dealing with missing cells when using tab delimiter
Simple
Path path = Path.of("some-file.csv");

  SexyCSV.Parser.builder()
         .delimiter(",")
         .hasHeaderRow(true)
         .build()
         .parse(path)
         .forEach(row->{
             String value = row.get("columnName");
         });
Extended
Path path = Path.of("some-file.csv");

SexyCSV.Parser parser = SexyCSV.Parser.builder()
                               .delimiter(",")
                               .hasHeaderRow(true) //auto-use of the given header in the file
                               //.header(Arrays.asList("id", "name", "age", "country")) set manual header
                               .skipRows(3)
                               .rowFilter(s->s.matches("^\\d.*")) //we are only interested in rows that start with a number
                               //.tokenizer(s -> s.split(";")) optional custom tokenizer
                               .build();

List<Row> data = parser.parse(path)
                       .collect(Collectors.toList());

Row firstRow = data.get(0);

// Access cells
String a = row.get(1);
String b = row.get("columnName")

//or simply parsing to your Java entity

Stream<TestEntity> entities = parser.parse(path,TestEntity.class);
SexyCsv.Creator
  • Automated header creation
    • Based on Java class entity
  • Streaming API
  • Streaming based prepending of meta information above the header
  • Column order rearranging
  • TypeConverters to deal with custom type serialization
SexyCSV.Creator<TestEntity> creator = SexyCSV.Creator.<TestEntity>builder()
        .withEntity(TestEntity.class)
        .withTypeConverter(LocalDateTime.class, new LocalDateTimeConverter())
        .build();

List<TestEntity> entities = new ArrayList<>();

//... add some entities

String csvString = creator.toString(entities.stream());

Stream<String> csvRowStream = creator.toStream(entities.stream());

Path path = Path.of("test.csv");
creator.toFile(entities.stream(), path);
        

Add custom type converters based on the Java Function interface

public class LocalDateTimeConverter implements Function<Object, String> {

    static DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

    @Override
    public String apply(Object localDateTime) {
        return ((LocalDateTime) localDateTime).format(formatter);
    }

}

Short API Overview

SexyCsv.Parser

Parses CSV files to Stream of Row or Entity

config

  • delimiter
  • hasHeader
  • withEntity
  • charset
  • tokenizer

parser

  • parse(Path) ~ Stream
  • parse(Path,Entity.class) ~ Stream
SexyCsv.Creator

Creates CSV files based on Streams, Strings or Entities

config

  • delimiter
  • prependingHeader(List)
  • withTypeConverter(Function<Object,String>)

creator

  • toString(Stream) ~ String
  • toStream(Stream) ~ Stream
  • toFile(Stream,Path) ~ void

Source

https://github.com/mklueh/sexy-csv

Project Page

https://mklueh.github.io/sexy-csv/

sexy-csv's People

Contributors

mklueh avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

sexy-csv's Issues

Support TypeConverters when parsing CSV to entity via annotation

Currently, only entity fields of type String will be parsed when using a parsing object with annotations. Should be possible to cover default types and add custom type converters based on column name, or specified in the annotation.

Global .withTypeConverter function should remain, but declaring converters on annotation level makes sense if there are different formattings of the same date type in one file for example.

Evaluation if .withTypeConverter makes sense for parsing as well, which should as we can match against the type of the field related to the column name.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.