Search in sources :

Example 16 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema 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 17 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema 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 18 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema 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 19 with CsvSchema

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

the class CsvRowSchemaConverter method convert.

/**
 * Convert {@link RowTypeInfo} to {@link CsvSchema}.
 */
public static CsvSchema convert(RowTypeInfo rowType) {
    final Builder builder = new CsvSchema.Builder();
    final String[] fields = rowType.getFieldNames();
    final TypeInformation<?>[] types = rowType.getFieldTypes();
    for (int i = 0; i < rowType.getArity(); i++) {
        builder.addColumn(new Column(i, fields[i], convertType(fields[i], types[i])));
    }
    return builder.build();
}
Also used : Column(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema.Column) Builder(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema.Builder) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 20 with CsvSchema

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

the class CsvRowSchemaConverter method convert.

/**
 * Convert {@link RowType} to {@link CsvSchema}.
 */
public static CsvSchema convert(RowType rowType) {
    Builder builder = new CsvSchema.Builder();
    List<RowType.RowField> fields = rowType.getFields();
    for (int i = 0; i < rowType.getFieldCount(); i++) {
        String fieldName = fields.get(i).getName();
        LogicalType fieldType = fields.get(i).getType();
        builder.addColumn(new Column(i, fieldName, convertType(fieldName, fieldType)));
    }
    return builder.build();
}
Also used : Column(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema.Column) Builder(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema.Builder) LogicalType(org.apache.flink.table.types.logical.LogicalType)

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