Search in sources :

Example 21 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project solr-cmd-utils by tblsoft.

the class BaseBlacklistFilter method init.

@Override
public void init() {
    topicValues = new HashMap<String, HashSet<String>>();
    InputStream in = null;
    try {
        String filename = getProperty("filename", null);
        String absoluteFilename = IOUtils.getAbsoluteFile(getBaseDir(), filename);
        in = IOUtils.getInputStream(absoluteFilename);
        java.io.Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8.name());
        CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',');
        CSVParser parser = format.parse(reader);
        Iterator<CSVRecord> csvIterator = parser.iterator();
        while (csvIterator.hasNext()) {
            CSVRecord record = csvIterator.next();
            String topic = record.get(0);
            String value = record.get(1);
            if (!topicValues.containsKey(topic)) {
                topicValues.put(topic, new HashSet<String>());
            }
            topicValues.get(topic).add(value);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
    fieldTopic = getProperty("fieldTopic", null);
    fieldValue = getProperty("fieldValue", null);
    super.init();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) HashSet(java.util.HashSet)

Example 22 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project solr-cmd-utils by tblsoft.

the class CSVWriter method document.

@Override
public void document(Document document) {
    if (firstDocument) {
        try {
            if (headers == null) {
                headers = getFieldNames(document);
            }
            // PrintWriter out = new PrintWriter(absoluteFilename);
            OutputStream out = IOUtils.getOutputStream(absoluteFilename);
            CSVFormat format = CSVFormat.RFC4180;
            if (withHeaders) {
                format = format.withDelimiter(delimiter.charAt(0)).withHeader(headers);
            } else {
                format = format.withDelimiter(delimiter.charAt(0));
            }
            Writer out1 = new BufferedWriter(new OutputStreamWriter(out));
            printer = new CSVPrinter(out1, format);
            firstDocument = false;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    try {
        List<List<String>> csvRows = new ArrayList<List<String>>();
        List<String> csvList = new ArrayList<String>();
        for (String headerField : headers) {
            Field field = document.getField(headerField);
            String value = null;
            if (field != null && field.getValues() != null) {
                value = Joiner.on(multiValueSeperator).skipNulls().join(field.getValues());
            }
            csvList.add(value);
        }
        csvRows.add(csvList);
        printer.printRecords(csvRows);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    super.document(document);
}
Also used : OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) CSVPrinter(org.apache.commons.csv.CSVPrinter) Field(de.tblsoft.solr.pipeline.bean.Field) CSVFormat(org.apache.commons.csv.CSVFormat) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 23 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project LogHub by fbacchella.

the class FileMap method mapFromCsv.

private Map<Object, Object> mapFromCsv() {
    try (Reader in = new FileReader(mappingFile)) {
        CSVFormat format;
        switch(csvFormat.toUpperCase()) {
            case "EXCEL":
                format = CSVFormat.EXCEL;
                break;
            case "RFC4180":
                format = CSVFormat.RFC4180;
                break;
            case "TDF":
                format = CSVFormat.TDF;
                break;
            case "DEFAULT":
                format = CSVFormat.DEFAULT;
                break;
            default:
                logger.error("Unknown CSV format name");
                return null;
        }
        boolean withHeaders;
        if (key != null && value != null) {
            format = format.withFirstRecordAsHeader();
            withHeaders = true;
        } else if (keyColumn > 0 && valueColumn > 0) {
            format = format.withSkipHeaderRecord(false);
            withHeaders = false;
        } else {
            logger.error("Neither column name or number defined");
            return null;
        }
        Iterable<CSVRecord> records = format.parse(in);
        Map<Object, Object> mapping = new HashMap<>();
        for (CSVRecord record : records) {
            if (withHeaders) {
                mapping.put(record.get(key), record.get(value));
            } else {
                mapping.put(record.get(keyColumn), record.get(valueColumn));
            }
        }
        return mapping;
    } catch (IOException e) {
        logger.error("Can't read mapping file {}", mappingFile);
        return null;
    }
}
Also used : HashMap(java.util.HashMap) FileReader(java.io.FileReader) Reader(java.io.Reader) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException)

Example 24 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project structr by structr.

the class FromCsvFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 5)) {
        try {
            final List<Map<String, String>> objects = new LinkedList<>();
            final String source = sources[0].toString();
            String delimiter = ";";
            String quoteChar = "\"";
            String recordSeparator = "\n";
            boolean customColumnNamesSupplied = false;
            switch(sources.length) {
                case 5:
                    customColumnNamesSupplied = (sources[4] instanceof Collection);
                case 4:
                    recordSeparator = (String) sources[3];
                case 3:
                    quoteChar = (String) sources[2];
                case 2:
                    delimiter = (String) sources[1];
                    break;
            }
            CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0));
            if (customColumnNamesSupplied) {
                format = format.withHeader(((Collection<String>) sources[4]).toArray(new String[] {})).withSkipHeaderRecord(false);
            } else {
                format = format.withHeader().withSkipHeaderRecord(true);
            }
            format = format.withQuote(quoteChar.charAt(0));
            format = format.withRecordSeparator(recordSeparator);
            format = format.withIgnoreEmptyLines(true);
            format = format.withIgnoreSurroundingSpaces(true);
            format = format.withQuoteMode(QuoteMode.ALL);
            CSVParser parser = new CSVParser(new StringReader(source), format);
            for (final CSVRecord record : parser.getRecords()) {
                objects.add(record.toMap());
            }
            return objects;
        } catch (Throwable t) {
            logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) Collection(java.util.Collection) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 25 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project structr by structr.

the class GetCsvHeadersFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4)) {
        try {
            final String source = sources[0].toString();
            String delimiter = ";";
            String quoteChar = "\"";
            String recordSeparator = "\n";
            switch(sources.length) {
                case 4:
                    recordSeparator = (String) sources[3];
                case 3:
                    quoteChar = (String) sources[2];
                case 2:
                    delimiter = (String) sources[1];
                    break;
            }
            CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
            if (quoteChar.length() > 0) {
                format = format.withQuote(quoteChar.charAt(0));
            } else {
                format = format.withQuote(null);
            }
            format = format.withRecordSeparator(recordSeparator);
            format = format.withIgnoreEmptyLines(true);
            format = format.withIgnoreSurroundingSpaces(true);
            format = format.withQuoteMode(QuoteMode.ALL);
            try (final CSVParser parser = new CSVParser(new StringReader(source), format)) {
                return parser.getHeaderMap().keySet();
            }
        } catch (Throwable t) {
            logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVFormat(org.apache.commons.csv.CSVFormat)

Aggregations

CSVFormat (org.apache.commons.csv.CSVFormat)59 IOException (java.io.IOException)23 CSVRecord (org.apache.commons.csv.CSVRecord)22 CSVParser (org.apache.commons.csv.CSVParser)19 ArrayList (java.util.ArrayList)14 StringReader (java.io.StringReader)13 CSVPrinter (org.apache.commons.csv.CSVPrinter)10 InputStream (java.io.InputStream)9 InputStreamReader (java.io.InputStreamReader)8 HashMap (java.util.HashMap)8 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)8 RecordField (org.apache.nifi.serialization.record.RecordField)8 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)8 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Reader (java.io.Reader)7 LinkedHashMap (java.util.LinkedHashMap)7 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)7 MapRecord (org.apache.nifi.serialization.record.MapRecord)7 Record (org.apache.nifi.serialization.record.Record)7