Search in sources :

Example 26 with CsvMapper

use of org.apache.flink.shaded.jackson2.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(final List<Vulnerability> vulnerabilities, final String fileName, final char delimiter) throws JsonGenerationException, JsonMappingException, FileNotFoundException, UnsupportedEncodingException, IOException {
    // create mapper and schema
    final CsvMapper mapper = new CsvMapper();
    // Create the schema with the header
    CsvSchema schema = mapper.schemaFor(Vulnerability.class).withHeader();
    schema = schema.withColumnSeparator(delimiter);
    // output writer
    final ObjectWriter objectWriter = mapper.writer(schema);
    final File file = new File(fileName);
    FileOutputStream fileOutputStream;
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (final FileNotFoundException e) {
        throw new FileSystemNotFoundException(fileName + " CSV file is not created successfully");
    }
    final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024);
    OutputStreamWriter writerOutputStream;
    try {
        writerOutputStream = new OutputStreamWriter(bufferedOutputStream, "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        throw new UnsupportedEncodingException(e.getMessage());
    }
    // write to CSV file
    try {
        objectWriter.writeValue(writerOutputStream, vulnerabilities);
    } catch (final 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)

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