Search in sources :

Example 11 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper 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)

Example 12 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project mapping-benchmark by arnaudroger.

the class JacksonCsvParserBenchmark method setUp.

@Setup
public void setUp() {
    csvMapperToStringArray = new CsvMapper();
    csvMapperToStringArray.enable(com.fasterxml.jackson.dataformat.csv.CsvParser.Feature.WRAP_AS_ARRAY);
    CsvMapper csvMapperToCity = new CsvMapper();
    csvMapperToCity.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader();
    cityReader = csvMapperToCity.readerFor(City.class).with(bootstrapSchema);
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) Setup(org.openjdk.jmh.annotations.Setup)

Example 13 with CsvMapper

use of com.fasterxml.jackson.dataformat.csv.CsvMapper in project hub-fortify-ssc-integration-service by blackducksoftware.

the class CSVUtils method writeToCSV.

/**
 * It will be used to render the list of vulnerabilities in CSV
 *
 * @param vulnerabilities
 * @param fileName
 * @param delimiter
 * @throws JsonGenerationException
 * @throws JsonMappingException
 * @throws FileNotFoundException
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
@SuppressWarnings("resource")
public static void writeToCSV(List<Vulnerability> vulnerabilities, String fileName, char delimiter) throws JsonGenerationException, JsonMappingException, FileNotFoundException, UnsupportedEncodingException, IOException {
    // create mapper and schema
    CsvMapper mapper = new CsvMapper();
    // Create the schema with the header
    CsvSchema schema = mapper.schemaFor(Vulnerability.class).withHeader();
    schema = schema.withColumnSeparator(delimiter);
    // output writer
    ObjectWriter objectWriter = mapper.writer(schema);
    File file = new File(fileName);
    FileOutputStream fileOutputStream;
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        throw new FileSystemNotFoundException(fileName + " CSV file is not created successfully");
    }
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024);
    OutputStreamWriter writerOutputStream;
    try {
        writerOutputStream = new OutputStreamWriter(bufferedOutputStream, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new UnsupportedEncodingException(e.getMessage());
    }
    // write to CSV file
    try {
        objectWriter.writeValue(writerOutputStream, vulnerabilities);
    } catch (IOException e) {
        throw new IOException("Error while rendering the vulnerabilities in CSV file::" + fileName, e);
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Vulnerability(com.blackducksoftware.integration.fortify.batch.model.Vulnerability) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 14 with CsvMapper

use of 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 15 with CsvMapper

use of 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)

Aggregations

CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)18 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 Map (java.util.Map)5 CsvMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)5 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 List (java.util.List)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 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)3 File (java.io.File)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 OutputStreamWriter (java.io.OutputStreamWriter)2