Search in sources :

Example 16 with CsvParserSettings

use of com.univocity.parsers.csv.CsvParserSettings in project Mycat_plus by coderczp.

the class ServerLoadDataInfileHandler method parseFileByLine.

private void parseFileByLine(String file, String encode, String split) {
    List<SQLExpr> columns = statement.getColumns();
    CsvParserSettings settings = new CsvParserSettings();
    settings.setMaxColumns(65535);
    settings.setMaxCharsPerColumn(65535);
    settings.getFormat().setLineSeparator(loadData.getLineTerminatedBy());
    settings.getFormat().setDelimiter(loadData.getFieldTerminatedBy().charAt(0));
    if (loadData.getEnclose() != null) {
        settings.getFormat().setQuote(loadData.getEnclose().charAt(0));
    }
    if (loadData.getEscape() != null) {
        settings.getFormat().setQuoteEscape(loadData.getEscape().charAt(0));
    }
    settings.getFormat().setNormalizedNewline(loadData.getLineTerminatedBy().charAt(0));
    /*
         *  fix #1074 : LOAD DATA local INFILE导入的所有Boolean类型全部变成了false
         *  不可见字符将在CsvParser被当成whitespace过滤掉, 使用settings.trimValues(false)来避免被过滤掉
         *  TODO : 设置trimValues(false)之后, 会引起字段值前后的空白字符无法被过滤!
         */
    settings.trimValues(false);
    CsvParser parser = new CsvParser(settings);
    InputStreamReader reader = null;
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(file);
        reader = new InputStreamReader(fileInputStream, encode);
        parser.beginParsing(reader);
        String[] row = null;
        while ((row = parser.parseNext()) != null) {
            parseOneLine(columns, tableName, row, true, loadData.getLineTerminatedBy());
        }
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } finally {
        parser.stopParsing();
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) CsvParser(com.univocity.parsers.csv.CsvParser)

Example 17 with CsvParserSettings

use of com.univocity.parsers.csv.CsvParserSettings in project lightning by automatictester.

the class JMeterCSVFileReader method getParser.

protected CsvParser getParser() {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setLineSeparatorDetectionEnabled(true);
    parserSettings.setHeaderExtractionEnabled(true);
    parserSettings.selectFields("label", "elapsed", "success", "timeStamp");
    RowListProcessor rowProcessor = new RowListProcessor();
    parserSettings.setProcessor(new ConcurrentRowProcessor(rowProcessor));
    return new CsvParser(parserSettings);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) RowListProcessor(com.univocity.parsers.common.processor.RowListProcessor) ConcurrentRowProcessor(com.univocity.parsers.common.processor.ConcurrentRowProcessor) CsvParser(com.univocity.parsers.csv.CsvParser)

Example 18 with CsvParserSettings

use of com.univocity.parsers.csv.CsvParserSettings in project lightning by automatictester.

the class PerfMonDataReader method getParser.

protected CsvParser getParser() {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setLineSeparatorDetectionEnabled(true);
    parserSettings.setHeaderExtractionEnabled(false);
    parserSettings.selectIndexes(TIMESTAMP, VALUE, HOST_AND_METRIC);
    RowListProcessor rowProcessor = new RowListProcessor();
    parserSettings.setProcessor(new ConcurrentRowProcessor(rowProcessor));
    return new CsvParser(parserSettings);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) RowListProcessor(com.univocity.parsers.common.processor.RowListProcessor) ConcurrentRowProcessor(com.univocity.parsers.common.processor.ConcurrentRowProcessor) CsvParser(com.univocity.parsers.csv.CsvParser)

Example 19 with CsvParserSettings

use of com.univocity.parsers.csv.CsvParserSettings in project junit5 by junit-team.

the class CsvArgumentsProvider method provideArguments.

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setDelimiter(delimiter);
    settings.getFormat().setLineSeparator(LINE_SEPARATOR);
    settings.getFormat().setQuote('\'');
    settings.getFormat().setQuoteEscape('\'');
    settings.setEmptyValue("");
    settings.setAutoConfigurationEnabled(false);
    CsvParser csvParser = new CsvParser(settings);
    AtomicLong index = new AtomicLong(0);
    // @formatter:off
    return Arrays.stream(lines).map(line -> Preconditions.notNull(csvParser.parseLine(line + LINE_SEPARATOR), () -> "Line at index " + index.get() + " contains invalid CSV: \"" + line + "\"")).peek(values -> index.incrementAndGet()).map(Arguments::of);
// @formatter:on
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) Preconditions(org.junit.platform.commons.util.Preconditions) Arrays(java.util.Arrays) Stream(java.util.stream.Stream) CsvParser(com.univocity.parsers.csv.CsvParser) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) AnnotationConsumer(org.junit.jupiter.params.support.AnnotationConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) CsvParser(com.univocity.parsers.csv.CsvParser)

Example 20 with CsvParserSettings

use of com.univocity.parsers.csv.CsvParserSettings in project junit5 by junit-team.

the class CsvFileArgumentsProvider method accept.

@Override
public void accept(CsvFileSource annotation) {
    resources = annotation.resources();
    charset = Charset.forName(annotation.encoding());
    numLinesToSkip = annotation.numLinesToSkip();
    settings = new CsvParserSettings();
    settings.getFormat().setDelimiter(annotation.delimiter());
    settings.getFormat().setLineSeparator(annotation.lineSeparator());
    settings.getFormat().setQuote('"');
    settings.getFormat().setQuoteEscape('"');
    settings.setEmptyValue("");
    settings.setAutoConfigurationEnabled(false);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings)

Aggregations

CsvParserSettings (com.univocity.parsers.csv.CsvParserSettings)22 CsvParser (com.univocity.parsers.csv.CsvParser)15 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)6 ParsingContext (com.univocity.parsers.common.ParsingContext)5 Reader (java.io.Reader)5 Benchmark (org.openjdk.jmh.annotations.Benchmark)4 AbstractRowProcessor (com.univocity.parsers.common.processor.AbstractRowProcessor)2 ConcurrentRowProcessor (com.univocity.parsers.common.processor.ConcurrentRowProcessor)2 RowListProcessor (com.univocity.parsers.common.processor.RowListProcessor)2 RouteResultset (io.mycat.route.RouteResultset)2 ArrayList (java.util.ArrayList)2 RouteResultset (com.actiontech.dble.route.RouteResultset)1 JSONElement (com.eden.common.json.JSONElement)1 TsvParser (com.univocity.parsers.tsv.TsvParser)1 TsvParserSettings (com.univocity.parsers.tsv.TsvParserSettings)1 JsonDocument (io.lumeer.api.dto.JsonDocument)1 Document (io.lumeer.api.model.Document)1 DataDocument (io.lumeer.engine.api.data.DataDocument)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1