Search in sources :

Example 16 with CsvMapper

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

the class FileHandler method serializeDiseaseTraitAnalysisFile.

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

Example 17 with CsvMapper

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

the class DiseaseTraitDtoAssembler method disassemble.

public static List<DiseaseTrait> disassemble(MultipartFile multipartFile) {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = FileHandler.getSchemaFromMultiPartFile(multipartFile);
    List<DiseaseTraitDto> diseaseTraitDtos;
    try {
        InputStream inputStream = multipartFile.getInputStream();
        MappingIterator<DiseaseTraitDto> iterator = mapper.readerFor(DiseaseTraitDto.class).with(schema).readValues(inputStream);
        diseaseTraitDtos = iterator.readAll();
    } catch (IOException ex) {
        throw new FileUploadException("Could not read the file");
    }
    List<DiseaseTrait> diseaseTraits = new ArrayList<>();
    diseaseTraitDtos.forEach(diseaseTraitDTO -> {
        DiseaseTrait diseaseTrait = new DiseaseTrait();
        diseaseTrait.setTrait(diseaseTraitDTO.getTrait());
        diseaseTraits.add(diseaseTrait);
    });
    return diseaseTraits;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) DiseaseTrait(uk.ac.ebi.spot.goci.model.DiseaseTrait) InputStream(java.io.InputStream) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) DiseaseTraitDto(uk.ac.ebi.spot.goci.curation.dto.DiseaseTraitDto) IOException(java.io.IOException) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException)

Example 18 with CsvMapper

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

the class DataStreamCsvITCase method factoryForPojo.

private static <T> BulkWriter.Factory<T> factoryForPojo(Class<T> pojoClass) {
    final Converter<T, T, Void> converter = (value, context) -> value;
    final CsvMapper csvMapper = new CsvMapper();
    final CsvSchema schema = csvMapper.schemaFor(pojoClass).withoutQuoteChar();
    return (out) -> new CsvBulkWriter<>(csvMapper, schema, converter, null, out);
}
Also used : Arrays(java.util.Arrays) RpcServiceSharing(org.apache.flink.runtime.minicluster.RpcServiceSharing) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CsvSchema(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema) HashMap(java.util.HashMap) RestartStrategies(org.apache.flink.api.common.restartstrategy.RestartStrategies) FileSink(org.apache.flink.connector.file.sink.FileSink) FileSource(org.apache.flink.connector.file.src.FileSource) MiniClusterResourceConfiguration(org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration) JsonPropertyOrder(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonPropertyOrder) ArrayList(java.util.ArrayList) TestLoggerExtension(org.apache.flink.util.TestLoggerExtension) BigDecimal(java.math.BigDecimal) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Path(org.apache.flink.core.fs.Path) ClientAndIterator(org.apache.flink.streaming.api.operators.collect.ClientAndIterator) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) FunctionWithException(org.apache.flink.util.function.FunctionWithException) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) BulkWriter(org.apache.flink.api.common.serialization.BulkWriter) Collection(java.util.Collection) DataStreamUtils(org.apache.flink.streaming.api.datastream.DataStreamUtils) MiniClusterExtension(org.apache.flink.test.junit5.MiniClusterExtension) FileOutputStream(java.io.FileOutputStream) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) StreamFormat(org.apache.flink.connector.file.src.reader.StreamFormat) DataStream(org.apache.flink.streaming.api.datastream.DataStream) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Converter(org.apache.flink.formats.common.Converter) List(java.util.List) BasePathBucketAssigner(org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner) TempDir(org.junit.jupiter.api.io.TempDir) NotNull(org.jetbrains.annotations.NotNull) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) CsvMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper) CsvSchema(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema) CsvMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)

Example 19 with CsvMapper

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

the class DataStreamCsvITCase method testCsvReaderFormatFromSchema.

@Test
public void testCsvReaderFormatFromSchema() throws Exception {
    writeFile(outDir, "data.csv", CSV_LINES_PIPE_SEPARATED);
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(CityPojo.class).withoutQuoteChar().withColumnSeparator('|');
    final CsvReaderFormat<CityPojo> csvFormat = CsvReaderFormat.forSchema(mapper, schema, TypeInformation.of(CityPojo.class));
    final List<CityPojo> result = initializeSourceAndReadData(outDir, csvFormat);
    assertThat(Arrays.asList(POJOS)).isEqualTo(result);
}
Also used : CsvSchema(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema) CsvMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper) Test(org.junit.jupiter.api.Test)

Example 20 with CsvMapper

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

the class RowDataToCsvConverters method createArrayRowFieldConverter.

private static RowFieldConverter createArrayRowFieldConverter(ArrayType type) {
    LogicalType elementType = type.getElementType();
    final ArrayElementConverter elementConverter = createNullableArrayElementConverter(elementType);
    return (csvMapper, container, row, pos) -> {
        ArrayNode arrayNode = csvMapper.createArrayNode();
        ArrayData arrayData = row.getArray(pos);
        int numElements = arrayData.size();
        for (int i = 0; i < numElements; i++) {
            arrayNode.add(elementConverter.convert(csvMapper, arrayNode, arrayData, i));
        }
        return arrayNode;
    };
}
Also used : Arrays(java.util.Arrays) SQL_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) RowType(org.apache.flink.table.types.logical.RowType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) TimestampType(org.apache.flink.table.types.logical.TimestampType) SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) DecimalType(org.apache.flink.table.types.logical.DecimalType) LocalTime(java.time.LocalTime) ISO_LOCAL_DATE(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) DecimalData(org.apache.flink.table.data.DecimalData) ArrayType(org.apache.flink.table.types.logical.ArrayType) Serializable(java.io.Serializable) Converter(org.apache.flink.formats.common.Converter) ArrayData(org.apache.flink.table.data.ArrayData) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Internal(org.apache.flink.annotation.Internal) ISO_LOCAL_TIME(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME) ContainerNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ContainerNode) CsvMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper) LogicalType(org.apache.flink.table.types.logical.LogicalType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) ArrayData(org.apache.flink.table.data.ArrayData)

Aggregations

CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)21 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)15 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)7 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)5 InputStream (java.io.InputStream)5 List (java.util.List)5 CsvMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)5 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)4 File (java.io.File)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Converter (org.apache.flink.formats.common.Converter)4 CsvSchema (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema)4 FileUploadException (uk.ac.ebi.spot.goci.curation.exception.FileUploadException)4 OutputStream (java.io.OutputStream)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)2 JetException (com.hazelcast.jet.JetException)2 FileOutputStream (java.io.FileOutputStream)2