Search in sources :

Example 6 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project goci by EBISPOT.

the class FileHandler method getStudyPatchRequests.

public static List<StudyPatchRequest> getStudyPatchRequests(FileUploadRequest fileUploadRequest) {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = getSchemaFromMultiPartFile(fileUploadRequest.getMultipartFile());
    List<StudyPatchRequest> studyPatchRequests;
    try {
        InputStream inputStream = fileUploadRequest.getMultipartFile().getInputStream();
        MappingIterator<StudyPatchRequest> iterator = mapper.readerFor(StudyPatchRequest.class).with(schema).readValues(inputStream);
        studyPatchRequests = iterator.readAll();
    } catch (IOException e) {
        throw new FileUploadException("Could not read the file");
    }
    return studyPatchRequests;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) InputStream(java.io.InputStream) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) IOException(java.io.IOException) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException) StudyPatchRequest(uk.ac.ebi.spot.goci.curation.dto.StudyPatchRequest)

Example 7 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project goci by EBISPOT.

the class FileHandler method serializePojoToTsv.

public static String serializePojoToTsv(List<?> pojoList) {
    CsvMapper csvMapper = new CsvMapper();
    List<Map<String, Object>> dataList = csvMapper.convertValue(pojoList, new TypeReference<Object>() {
    });
    List<List<String>> csvData = new ArrayList<>();
    List<String> csvHead = new ArrayList<>();
    AtomicInteger counter = new AtomicInteger();
    dataList.forEach(row -> {
        List<String> rowData = new ArrayList<>();
        row.forEach((key, value) -> {
            rowData.add(String.valueOf(value));
            if (counter.get() == 0) {
                csvHead.add(key);
            }
        });
        csvData.add(rowData);
        counter.getAndIncrement();
    });
    CsvSchema.Builder builder = CsvSchema.builder();
    csvHead.forEach(builder::addColumn);
    CsvSchema schema = builder.build().withHeader().withLineSeparator("\n").withColumnSeparator('\t');
    String result = "";
    try {
        result = csvMapper.writer(schema).writeValueAsString(csvData);
    } catch (IOException e) {
        throw new FileUploadException("Could not read the file");
    }
    return result;
}
Also used : CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException)

Example 8 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project goci by EBISPOT.

the class FileHandler method getSchemaFromMultiPartFile.

public static CsvSchema getSchemaFromMultiPartFile(MultipartFile multipartFile) {
    CsvSchema.Builder builder = CsvSchema.builder();
    CsvSchema schema = builder.build().withHeader();
    if (FilenameUtils.getExtension(multipartFile.getOriginalFilename()).equals("tsv")) {
        schema = schema.withColumnSeparator('\t');
    }
    return schema;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema)

Example 9 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project registry by hortonworks.

the class TruckEventsCsvConverter method readTruckEventsFromCsv.

private MappingIterator<TruckEvent> readTruckEventsFromCsv(InputStream csvStream) throws IOException {
    CsvSchema bootstrap = CsvSchema.builder().addColumn("driverId", CsvSchema.ColumnType.NUMBER).addColumn("truckId", CsvSchema.ColumnType.NUMBER).addColumn("eventTime", CsvSchema.ColumnType.STRING).addColumn("eventType", CsvSchema.ColumnType.STRING).addColumn("longitude", CsvSchema.ColumnType.NUMBER).addColumn("latitude", CsvSchema.ColumnType.NUMBER).addColumn("eventKey", CsvSchema.ColumnType.STRING).addColumn("correlationId", CsvSchema.ColumnType.NUMBER).addColumn("driverName", CsvSchema.ColumnType.STRING).addColumn("routeId", CsvSchema.ColumnType.NUMBER).addColumn("routeName", CsvSchema.ColumnType.STRING).addColumn("eventDate", CsvSchema.ColumnType.STRING).build().withHeader();
    CsvMapper csvMapper = new CsvMapper();
    return csvMapper.readerFor(TruckEvent.class).with(bootstrap).readValues(csvStream);
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper)

Example 10 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project mapping-benchmark by arnaudroger.

the class JacksonCsvParserBenchmark method main.

public static void main(String[] args) throws IOException {
    CsvParam csvParam = new CsvParam();
    csvParam.setUp();
    CsvMapper csvMapper = new CsvMapper();
    csvMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader();
    try (Reader reader = csvParam.getReader()) {
        MappingIterator<City> iterator = csvMapper.readerFor(City.class).with(bootstrapSchema).readValues(reader);
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) Reader(java.io.Reader) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CsvParam(org.simpleflatmapper.param.CsvParam)

Aggregations

CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)17 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)11 CsvSchema (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)5 CsvMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)5 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 File (java.io.File)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BulkWriter (org.apache.flink.api.common.serialization.BulkWriter)3 Converter (org.apache.flink.formats.common.Converter)3 Preconditions.checkNotNull (org.apache.flink.util.Preconditions.checkNotNull)3 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2