org.apache.commons.csv.QuoteMode Java Examples

The following examples show how to use org.apache.commons.csv.QuoteMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueueReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
protected QueueDataSet readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);



    for (CSVRecord record : records) {
        try {
            queueProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5), record.get(6));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return queueProcessor.getQueueDataSet();
}
 
Example #2
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtraFieldInWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new HashMap<>();
    values.put("id", "1");
    values.put("name", "John");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();
        writer.write(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id\n1\n", output);
}
 
Example #3
Source File: ITApacheCSVRecordReader.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionThrownOnParseProblem() throws IOException, MalformedRecordException {
    CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withQuoteMode(QuoteMode.ALL).withTrim().withDelimiter(',');
    final int NUM_LINES = 25;
    StringBuilder sb = new StringBuilder("\"id\",\"name\",\"balance\"");
    for (int i = 0; i < NUM_LINES; i++) {
        sb.append(String.format("\"%s\",\"John Doe\",\"4750.89D\"\n", i));
    }
    // cause a parse problem
    sb.append(String.format("\"%s\"dieParser,\"John Doe\",\"4750.89D\"\n", NUM_LINES ));
    sb.append(String.format("\"%s\",\"John Doe\",\"4750.89D\"\n", NUM_LINES + 1));
    final RecordSchema schema = new SimpleRecordSchema(createStringFields(new String[] {"id", "name", "balance"}));

    try (final InputStream bais = new ByteArrayInputStream(sb.toString().getBytes());
         final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, csvFormat, true, false,
                 RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), "UTF-8")) {

        while (reader.nextRecord() != null) {}
    } catch (Exception e) {
        assertThat(e, instanceOf(MalformedRecordException.class));
        assertThat(Throwables.getRootCause(e), instanceOf(IOException.class));
    }
}
 
Example #4
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMissingFieldWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();
        writer.writeRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}
 
Example #5
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMissingFieldWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}
 
Example #6
Source File: AbstractCsvLayout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape,
        final Character quote, final QuoteMode quoteMode, final String nullString, final String recordSeparator) {
    CSVFormat csvFormat = CSVFormat.valueOf(format);
    if (isNotNul(delimiter)) {
        csvFormat = csvFormat.withDelimiter(delimiter);
    }
    if (isNotNul(escape)) {
        csvFormat = csvFormat.withEscape(escape);
    }
    if (isNotNul(quote)) {
        csvFormat = csvFormat.withQuote(quote);
    }
    if (quoteMode != null) {
        csvFormat = csvFormat.withQuoteMode(quoteMode);
    }
    if (nullString != null) {
        csvFormat = csvFormat.withNullString(nullString);
    }
    if (recordSeparator != null) {
        csvFormat = csvFormat.withRecordSeparator(recordSeparator);
    }
    return csvFormat;
}
 
Example #7
Source File: LegacyRateReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
protected RateData readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(',')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);

    for (CSVRecord record : records) {
        try {
            processor.process(record.get(0), record.get(1));
        } catch (Exception e) {
            logger.warn("Unable to parse record: {}", e.getMessage(), e);
        }
    }

    return processor.getRateData();
}
 
Example #8
Source File: MemoryAreasReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
protected MemoryAreasDataSet readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);

    for (CSVRecord record : records) {
        try {
            memoryAreasProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return memoryAreasProcessor.getMemoryAreasDataSet();
}
 
Example #9
Source File: HeapReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
protected HeapData readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);



    for (CSVRecord record : records) {
        try {
            heapProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return heapProcessor.getHeapData();
}
 
Example #10
Source File: GeneralInfoReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
/**
 * Reader of csv file
 * @param reader reader
 * @return readed data
 * @throws IOException implementation specific
 */
@Override
protected GeneralInfoDataSet readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);



    for (CSVRecord record : records) {
        try {
            generalInfoProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5), record.get(6), record.get(7), record.get(8), record.get(9));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return generalInfoProcessor.getGeneralInfoDataSet();
}
 
Example #11
Source File: RouterLinkReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
/**
 * Reader of csv file
 * @param reader reader
 * @return readed data
 * @throws IOException implementation specific
 */
@Override
protected RouterLinkDataSet readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);



    for (CSVRecord record : records) {
        try {
            routerLinkProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5), record.get(6), record.get(7), record.get(8), record.get(9), record.get(10),
                    record.get(11), record.get(12), record.get(13));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return routerLinkProcessor.getRouterLinkDataSet();
}
 
Example #12
Source File: ConnectionsReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
/**
 * Reader of csv file
 * @param reader reader
 * @return readed data
 * @throws IOException implementation specific
 */
@Override
protected ConnectionsDataSet readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);



    for (CSVRecord record : records) {
        try {
            connectionsProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5), record.get(6), record.get(7), record.get(8), record.get(9), record.get(10),
                    record.get(11), record.get(12), record.get(13), record.get(14));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return connectionsProcessor.getConnectionsDataSet();
}
 
Example #13
Source File: QDMemoryReader.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
/**
 * Reader of csv file
 * @param reader reader
 * @return readed data
 * @throws IOException implementation specific
 */
@Override
protected QDMemoryDataSet readReader(Reader reader) throws IOException {
    Iterable<CSVRecord> records = CSVFormat.RFC4180
            .withCommentMarker('#')
            .withFirstRecordAsHeader()
            .withRecordSeparator(';')
            .withQuote('"')
            .withQuoteMode(QuoteMode.NON_NUMERIC)
            .parse(reader);



    for (CSVRecord record : records) {
        try {
            qdMemoryProcessor.process(record.get(0), record.get(1), record.get(2), record.get(3), record.get(4),
                    record.get(5), record.get(6), record.get(7), record.get(8), record.get(9), record.get(10));
        } catch (Throwable t) {
            logger.warn("Unable to parse record: {}", t.getMessage(), t);
        }
    }

    return qdMemoryProcessor.getQDMemoryDataSet();
}
 
Example #14
Source File: SpannerConverters.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Prints Struct as a CSV String.
 *
 * @param struct Spanner Struct.
 * @return Spanner Struct encoded as a CSV String.
 */
public String print(Struct struct) {
  StringWriter stringWriter = new StringWriter();
  try {
    CSVPrinter printer =
        new CSVPrinter(stringWriter, CSVFormat.DEFAULT.withRecordSeparator("")
            .withQuoteMode(QuoteMode.ALL_NON_NULL));
    LinkedHashMap<String, BiFunction<Struct, String, String>> parsers = Maps.newLinkedHashMap();
    parsers.putAll(mapColumnParsers(struct.getType().getStructFields()));
    List<String> values = parseResultSet(struct, parsers);
    printer.printRecord(values);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  return stringWriter.toString();
}
 
Example #15
Source File: CsvParameterLayout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@PluginFactory
public static AbstractCsvLayout createLayout(
        // @formatter:off
        @PluginConfiguration final Configuration config,
        @PluginAttribute(defaultString = DEFAULT_FORMAT) final String format,
        @PluginAttribute final Character delimiter,
        @PluginAttribute final Character escape,
        @PluginAttribute final Character quote,
        @PluginAttribute final QuoteMode quoteMode,
        @PluginAttribute final String nullString,
        @PluginAttribute final String recordSeparator,
        @PluginAttribute(defaultString = DEFAULT_CHARSET) final Charset charset,
        @PluginAttribute final String header,
        @PluginAttribute final String footer)
        // @formatter:on
{

    final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
    return new CsvParameterLayout(config, charset, csvFormat, header, footer);
}
 
Example #16
Source File: CsvLogEventLayout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@PluginFactory
public static CsvLogEventLayout createLayout(
        // @formatter:off
        @PluginConfiguration final Configuration config,
        @PluginAttribute(defaultString = DEFAULT_FORMAT) final String format,
        @PluginAttribute final Character delimiter,
        @PluginAttribute final Character escape,
        @PluginAttribute final Character quote,
        @PluginAttribute final QuoteMode quoteMode,
        @PluginAttribute final String nullString,
        @PluginAttribute final String recordSeparator,
        @PluginAttribute(defaultString = DEFAULT_CHARSET) final Charset charset,
        @PluginAttribute final String header,
        @PluginAttribute final String footer)
        // @formatter:on
{

    final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
    return new CsvLogEventLayout(config, charset, csvFormat, header, footer);
}
 
Example #17
Source File: TextUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param elements numbers to join by space to make one line of text
 * @return one line of text, formatted according to PMML quoting rules
 */
public static String joinPMMLDelimitedNumbers(Iterable<? extends Number> elements) {
  // bit of a workaround because NON_NUMERIC quote mode still quote "-1"!
  CSVFormat format = formatForDelimiter(' ').withQuoteMode(QuoteMode.NONE);
  // No quoting, no need to convert quoting
  return doJoinDelimited(elements, format);
}
 
Example #18
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingAndExtraFieldWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name,dob\n1,,1/1/1970\n", output);
}
 
Example #19
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingAndExtraFieldWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();
        writer.writeRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}
 
Example #20
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtraFieldInWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    // The fields defined in the schema should be written first followed by extra ones.
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("name", "John");
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,John\n", output);
}
 
Example #21
Source File: TestWriteCSVResult.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumbersNotQuoted() throws IOException {
    final Map<String, Object> values = new HashMap<>();
    values.put("name", "John Doe");
    values.put("age", 30);

    final List<RecordField> schemaFields = new ArrayList<>();
    schemaFields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    schemaFields.add(new RecordField("age", RecordFieldType.INT.getDataType()));

    final RecordSchema schema = new SimpleRecordSchema(schemaFields);
    final Record record = new MapRecord(schema, values);

    // Test with Non-Numeric Quote Mode
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CSVFormat csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC).withRecordSeparator("\n");
    try (final WriteCSVResult result = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "UTF-8")) {
        result.writeRecord(record);
    }

    String output = baos.toString();
    assertEquals("\"name\",\"age\"\n\"John Doe\",30\n", output);

    baos.reset();

    // Test with MINIMAL Quote Mode
    csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.MINIMAL).withRecordSeparator("\n");
    try (final WriteCSVResult result = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
        RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "UTF-8")) {
        result.writeRecord(record);
    }

    output = baos.toString();
    assertEquals("name,age\nJohn Doe,30\n", output);
}
 
Example #22
Source File: ToCsvPragmaInstance.java    From CQL with GNU Affero General Public License v3.0 5 votes vote down vote up
public static CSVFormat getFormat(AqlOptions op) {
	String format0 = "Default";
	CSVFormat format = CSVFormat.valueOf(format0);

	format = format.withDelimiter((Character) op.getOrDefault(AqlOption.csv_field_delim_char));
	format = format.withQuote((Character) op.getOrDefault(AqlOption.csv_quote_char));
	format = format.withEscape((Character) op.getOrDefault(AqlOption.csv_escape_char));
	format = format.withQuoteMode(QuoteMode.ALL);
	format = format.withNullString(null);

	return format;
}
 
Example #23
Source File: DataDstUECsv.java    From xresloader with MIT License 5 votes vote down vote up
@Override
protected Object buildForUEOnInit() throws IOException {
    UEBuildObject ret = new UEBuildObject();
    ret.sb = new StringBuffer();
    ret.csv = new CSVPrinter(ret.sb, CSVFormat.INFORMIX_UNLOAD_CSV.withQuoteMode(QuoteMode.ALL));

    appendCommonHeader(ret.csv);
    ret.csv.printComment(String.format("%s=%s", "xres_ver", ProgramOptions.getInstance().getVersion()));
    ret.csv.printComment(String.format("%s=%s", "data_ver", ProgramOptions.getInstance().getDataVersion()));
    ret.csv.printComment(String.format("%s=%d", "count", DataSrcImpl.getOurInstance().getRecordNumber()));
    ret.csv.printComment(String.format("%s=%s", "hash_code", "no hash code"));

    return ret;
}
 
Example #24
Source File: TerminologyLoaderDao.java    From careconnect-reference-implementation with Apache License 2.0 5 votes vote down vote up
public UploadStatistics processLoincFiles(List<byte[]> theZipBytes, RequestDetails theRequestDetails) throws OperationOutcomeException {
    String url = LOINC_URL;
    final CodeSystemEntity codeSystemVersion = codeSvc.findBySystem(url);
    final Map<String, ConceptEntity> code2concept = new HashMap<String, ConceptEntity>();

    IRecordHandler handler = new LoincHandler(codeSystemVersion, code2concept);
    iterateOverZipFile(theZipBytes, LOINC_FILE, handler, ',', QuoteMode.NON_NUMERIC);

    handler = new LoincHierarchyHandler(codeSystemVersion, code2concept);
    iterateOverZipFile(theZipBytes, LOINC_HIERARCHY_FILE, handler, ',', QuoteMode.NON_NUMERIC);

    theZipBytes.clear();

    for (Iterator<Map.Entry<String, ConceptEntity>> iter = code2concept.entrySet().iterator(); iter.hasNext();) {
        Map.Entry<String, ConceptEntity> next = iter.next();
        // if (isBlank(next.getKey())) {
        // ourLog.info("Removing concept with blankc code[{}] and display [{}", next.getValue().getCode(), next.getValue().getDisplay());
        // iter.remove();
        // continue;
        // }
        ConceptEntity nextConcept = next.getValue();
        if (nextConcept.getParents().isEmpty()) {
            codeSystemVersion.getConcepts().add(nextConcept);
        }
    }

    ourLog.info("Have {} total concepts, {} root concepts", code2concept.size(), codeSystemVersion.getConcepts().size());


    storeCodeSystem(theRequestDetails, codeSystemVersion);

    return new UploadStatistics(code2concept.size());
}
 
Example #25
Source File: CsvEngine.java    From pippo with Apache License 2.0 4 votes vote down vote up
public void setQuoteMode(QuoteMode quoteMode) {
    this.quoteMode = quoteMode;
}
 
Example #26
Source File: DataSetCsvGroup.java    From pentaho-pdi-dataset with Apache License 2.0 4 votes vote down vote up
public static CSVFormat getCsvFormat( RowMetaInterface rowMeta ) {
  return CSVFormat.DEFAULT.withHeader( rowMeta.getFieldNames() ).withQuote( '\"' ).withQuoteMode( QuoteMode.MINIMAL );
}
 
Example #27
Source File: CSVUtils.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static CSVFormat buildCustomFormat(final PropertyContext context, final Map<String, String> variables) {
    final Character valueSeparator = getCharUnescapedJava(context, VALUE_SEPARATOR, variables);
    CSVFormat format = CSVFormat.newFormat(valueSeparator)
        .withAllowMissingColumnNames()
        .withIgnoreEmptyLines();

    final PropertyValue firstLineIsHeaderPropertyValue = context.getProperty(FIRST_LINE_IS_HEADER);
    if (firstLineIsHeaderPropertyValue.getValue() != null && firstLineIsHeaderPropertyValue.asBoolean()) {
        format = format.withFirstRecordAsHeader();
    }

    final Character quoteChar = getCharUnescaped(context, QUOTE_CHAR, variables);
    format = format.withQuote(quoteChar);

    final Character escapeChar = getCharUnescaped(context, ESCAPE_CHAR, variables);
    format = format.withEscape(escapeChar);

    format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());

    if (context.getProperty(COMMENT_MARKER).isSet()) {
        final Character commentMarker = getCharUnescaped(context, COMMENT_MARKER, variables);
        if (commentMarker != null) {
            format = format.withCommentMarker(commentMarker);
        }
    }
    if (context.getProperty(NULL_STRING).isSet()) {
        format = format.withNullString(unescape(context.getProperty(NULL_STRING).getValue()));
    }

    final PropertyValue quoteValue = context.getProperty(QUOTE_MODE);
    if (quoteValue != null && quoteValue.isSet()) {
        final QuoteMode quoteMode = QuoteMode.valueOf(quoteValue.getValue());
        format = format.withQuoteMode(quoteMode);
    }

    final PropertyValue trailingDelimiterValue = context.getProperty(TRAILING_DELIMITER);
    if (trailingDelimiterValue != null && trailingDelimiterValue.isSet()) {
        final boolean trailingDelimiter = trailingDelimiterValue.asBoolean();
        format = format.withTrailingDelimiter(trailingDelimiter);
    }

    final PropertyValue recordSeparator = context.getProperty(RECORD_SEPARATOR);
    if (recordSeparator != null && recordSeparator.isSet()) {
        final String separator = unescape(recordSeparator.getValue());
        format = format.withRecordSeparator(separator);
    }

    return format;
}
 
Example #28
Source File: CsvLoader.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@JsonCreator
public CsvLoader(@JsonProperty("config") Map<String, String> config) {
  CSVFormat format = CSVFormat.EXCEL;
  this.config = config;
  if (config.containsKey("delimiter")) {
    format = format.withDelimiter(onlyChar(config, "delimiter"));
  }
  if (config.containsKey("quoteChar")) {
    format = format.withQuote(onlyChar(config, "quoteChar"));
  }
  if (config.containsKey("quoteMode")) {
    format = format.withQuoteMode(QuoteMode.valueOf(config.get("quoteMode")));
  }
  if (config.containsKey("commentStart")) {
    format = format.withCommentMarker(onlyChar(config, "commentStart"));
  }
  if (config.containsKey("escape")) {
    format = format.withEscape(onlyChar(config, "escape"));
  }
  if (config.containsKey("ignoreSurroundingSpaces")) {
    format = format.withIgnoreSurroundingSpaces(config.get("ignoreSurroundingSpaces").equals("true"));
  }
  if (config.containsKey("ignoreEmptyLines")) {
    format = format.withIgnoreEmptyLines(config.get("ignoreEmptyLines").equals("true"));
  }
  if (config.containsKey("recordSeparator")) {
    format = format.withRecordSeparator(config.get("recordSeparator"));
  }
  if (config.containsKey("nullString")) {
    format = format.withNullString(config.get("nullString"));
  }
  if (config.containsKey("trim")) {
    format = format.withTrim(config.get("trim").equals("true"));
  }
  if (config.containsKey("trailingDelimiter")) {
    format = format.withTrailingDelimiter(config.get("trailingDelimiter").equals("true"));
  }
  this.format = format
    .withAllowMissingColumnNames()
    .withHeader();
}
 
Example #29
Source File: CSVFormatFactory.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
static CSVFormat newFormat(final String format,
                                      final String delimiter,
                                      final String quoteCharacter,
                                      final String quoteMode,
                                      final String commentMarker,
                                      final String escapeCharacter,
                                      final String ignoreSurroundingSpaces,
                                      final String ignoreEmptyLines,
                                      final String recordSeparator,
                                      final String nullString,
                                      final String headerComments,
                                      final String header,
                                      final String skipHeaderRecord,
                                      final String allowMissingColumnNames,
                                      final String readHeaders) {
//CHECKSTYLE:ON
        CSVFormat out = format == null ? CSVFormat.DEFAULT : CSVFormat.valueOf(format);
        if (delimiter != null) {
            out = out.withDelimiter(delimiter.charAt(0));
        }
        if (quoteCharacter != null) {
            out = out.withQuote(quoteCharacter.charAt(0));
        }
        if (quoteMode != null) {
            out = out.withQuoteMode(QuoteMode.valueOf(quoteMode));
        }
        if (commentMarker != null) {
            out = out.withCommentMarker(commentMarker.charAt(0));
        }
        if (escapeCharacter != null) {
            out = out.withEscape(escapeCharacter.charAt(0));
        }
        if (ignoreSurroundingSpaces != null) {
            out = out.withIgnoreSurroundingSpaces(Boolean.parseBoolean(ignoreSurroundingSpaces));
        }
        if (ignoreEmptyLines != null) {
            out = out.withIgnoreEmptyLines(Boolean.parseBoolean(ignoreEmptyLines));
        }
        if (recordSeparator != null) {
            if ("\\n".equals(recordSeparator)) {
                out = out.withRecordSeparator('\n');
            } else if ("\\r\\n".equals(recordSeparator)) {
                out = out.withRecordSeparator("\r\n");
            } else {
                out = out.withRecordSeparator(recordSeparator);
            }
        }
        if (nullString != null) {
            out = out.withNullString(nullString);
        }
        if (headerComments != null && !headerComments.trim().isEmpty()) {
            out = out.withHeaderComments(headerComments.split(" *, *"));
        }
        if (Boolean.parseBoolean(readHeaders)) {
            out = out.withHeader();
        }
        if (header != null && !header.trim().isEmpty()) {
            try { // headers can have CSV header names so parse it there
                final Iterator<CSVRecord> iterator = out.withHeader(new String[0]).parse(new StringReader(header + '\n' + header)).iterator();
                final CSVRecord record = iterator.next();
                final List<String> list = new ArrayList<String>(record.size());
                for (final String h : record) {
                    list.add(h);
                }
                out = out.withHeader(list.toArray(new String[record.size()]));
            } catch (final IOException e) { // can't occur actually
                out = out.withHeader(header.split(" *, *"));
            }
        }
        if (skipHeaderRecord != null) {
            out = out.withSkipHeaderRecord(Boolean.parseBoolean(skipHeaderRecord));
        }
        if (allowMissingColumnNames != null) {
            out = out.withAllowMissingColumnNames(Boolean.parseBoolean(allowMissingColumnNames));
        }
        return out;
    }
 
Example #30
Source File: CsvStaticDataWriter.java    From obevo with Apache License 2.0 4 votes vote down vote up
public static void start(final AquaRevengArgs args, File workDir) {
    final DbEnvironment env = new DbEnvironment();
    DbPlatform platform = (DbPlatform) args.getPlatform();
    env.setPlatform(platform);
    env.setSystemDbPlatform(platform);
    if (args.getJdbcUrl() != null) {
        env.setJdbcUrl(args.getJdbcUrl());
    } else {
        env.setDbHost(args.getDbHost());
        env.setDbPort(args.getDbPort());
        env.setDbServer(args.getDbServer());
    }
    if (args.getDriverClass() != null) {
        env.setDriverClassName(args.getDriverClass());
    }
    final PhysicalSchema physicalSchema = PhysicalSchema.parseFromString(args.getDbSchema());
    final Schema schema = new Schema(physicalSchema.getPhysicalName());  // use the physical name as the schema name for the reverse-engineering
    env.setSchemas(Sets.immutable.with(schema));

    Credential credential = credentialReader.getCredential(args.getUsername(), args.getPassword(), false, null, null,
            null);

    DbDeployerAppContext ctxt = env.getAppContextBuilder().setCredential(credential).setWorkDir(workDir).buildDbContext();
    final CsvStaticDataWriter mw = new CsvStaticDataWriter(ctxt.getSqlExecutor(),
            ctxt.getDbMetadataManager());

    final MutableList<String> dataTables;
    if (args.getTables() != null && args.getTables().length > 0) {
        dataTables = Lists.mutable.with(args.getTables());
    } else {
        dataTables = FileUtilsCobra.readLines(new File(args.getInputDir(), STATIC_DATA_TABLES_FILE_NAME));
    }

    for (String table : dataTables) {
        System.out.println("Working on table " + table + " at " + new Date());

        CSVFormat csvFormat = CsvStaticDataReader.getCsvFormat(env.getDataDelimiter(), env.getNullToken()).withQuoteMode(QuoteMode.NON_NUMERIC);
        mw.writeTable(env.getPlatform(), physicalSchema, table.trim(),
                new File(args.getOutputPath(), env.getPlatform().getChangeType(ChangeType.STATICDATA_STR).getDirectoryName()),
                args.getUpdateTimeColumns(), csvFormat);
    }
}