下面列出了org.apache.commons.io.input.TailerListener#au.com.bytecode.opencsv.CSVParser 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a CsvStreamReader with supplied separator and quote char.
*
* @param source The file to an underlying CSV source
* @param separator The delimiter to use for separating entries
* @param quoteChar The character to use for quoted elements
* @param escape The character to use for escaping a separator or quote
* @param line The line number to skip for start reading
* @param strictQuotes Sets if characters outside the quotes are ignored
* @param ignoreLeadingWhiteSpace If true, parser should ignore
* white space before a quote in a field
*/
private CsvStreamReader(Source source, char separator, char quoteChar,
char escape, int line, boolean strictQuotes,
boolean ignoreLeadingWhiteSpace) {
super(new StringReader("")); // dummy call to base constructor
contentQueue = new ArrayDeque<>();
TailerListener listener = new CsvContentListener(contentQueue);
tailer = Tailer.create(source.file(), listener, DEFAULT_MONITOR_DELAY,
false, true, 4096);
this.parser = new CSVParser(separator, quoteChar, escape, strictQuotes,
ignoreLeadingWhiteSpace);
this.skipLines = line;
try {
// wait for tailer to capture data
Thread.sleep(DEFAULT_MONITOR_DELAY);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* Creates a CsvStreamReader with supplied separator and quote char.
*
* @param source The file to an underlying CSV source
* @param separator The delimiter to use for separating entries
* @param quoteChar The character to use for quoted elements
* @param escape The character to use for escaping a separator or quote
* @param line The line number to skip for start reading
* @param strictQuotes Sets if characters outside the quotes are ignored
* @param ignoreLeadingWhiteSpace If true, parser should ignore
* white space before a quote in a field
*/
private CsvStreamReader(Source source, char separator, char quoteChar,
char escape, int line, boolean strictQuotes,
boolean ignoreLeadingWhiteSpace) {
super(new StringReader("")); // dummy call to base constructor
contentQueue = new ArrayDeque<>();
TailerListener listener = new CsvContentListener(contentQueue);
tailer = Tailer.create(source.file(), listener, DEFAULT_MONITOR_DELAY,
false, true, 4096);
this.parser = new CSVParser(separator, quoteChar, escape, strictQuotes,
ignoreLeadingWhiteSpace);
this.skipLines = line;
try {
// wait for tailer to capture data
Thread.sleep(DEFAULT_MONITOR_DELAY);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Transactional
private void processResponseDocumentContent(String content, Integer orderId, Integer projectId) throws Exception {
BufferedReader reader = new BufferedReader(new StringReader(content));
String line;
String[] toks;
CSVParser parser = new CSVParser();
int counter = 1;
reader.readLine(); //skip the first line which is a header.
while ((line = reader.readLine()) != null) {
counter++;
line = line.trim();
if (line.length() <= 0) continue;
try {
toks = parser.parseLine(line);
if (toks.length != 4) {
throw new RuntimeException("Invalid number of columns in row " + counter);
}
updateTranslation(orderId, new Long(toks[0]), toks[1], toks[2], toks[3]);
} catch (Exception e) {
logger.error("Invalid line: " + line + " (" + e.getMessage() + ")");
throw new RuntimeException("Invalid line: " + line + " (" + e.getMessage() + ")");
}
}
}
CsvStreamReader(Source source) {
this(source,
CSVParser.DEFAULT_SEPARATOR,
CSVParser.DEFAULT_QUOTE_CHARACTER,
CSVParser.DEFAULT_ESCAPE_CHARACTER,
DEFAULT_SKIP_LINES,
CSVParser.DEFAULT_STRICT_QUOTES,
CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE);
}
public void parse(Reader fileReader, String fileType, LogRecordCollector collector) throws IOException {
try (CSVReader csvReader = new CSVReader(
fileReader,
config.getDelimiter(),
CSVParser.DEFAULT_QUOTE_CHARACTER,
CSVParser.NULL_CHARACTER);) {
long line = 0;
String[] headers = csvReader.readNext();
line++;
String[] fieldNames = constructFieldNames(headers, fileType, config.isCleanKeysForES());
String[] record = null;
while ((record = csvReader.readNext()) != null) {
if (record.length != headers.length) {
// unmatched record length
LOGGER.warn("Unmatched record column count detected at line {} - header: {} record: {}",
line, headers.length, record.length);
continue;
}
LogRecord logRecord = new LogRecord();
for (int i = 0; i < fieldNames.length; i++) {
if (config.isSkipBlankFields() && StringUtils.isBlank(record[i])) {
// skip
} else {
String value = config.isTrimFieldValue() ? StringUtils.trim(record[i]) : record[i];
logRecord.setValue(fieldNames[i], value);
}
}
collector.emit(logRecord);
}
}
}
CsvStreamReader(Source source) {
this(source,
CSVParser.DEFAULT_SEPARATOR,
CSVParser.DEFAULT_QUOTE_CHARACTER,
CSVParser.DEFAULT_ESCAPE_CHARACTER,
DEFAULT_SKIP_LINES,
CSVParser.DEFAULT_STRICT_QUOTES,
CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE);
}
static CSVParser newParser(CSVProperties props) {
return new CSVParser(
props.delimiter.charAt(0), props.quote.charAt(0),
props.escape.charAt(0),
false /* strict quotes off: don't ignore unquoted strings */,
true /* ignore leading white-space */ );
}
@Override
public FixTable create(TestRun testRun) throws Exception {
try (CSVReader reader = new CSVReader(getFileReader(testRun), separator, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER)) {
List<String[]> myEntries = reader.readAll();
String[] colNames = myEntries.get(0);
List<String[]> rowData = myEntries.subList(1, myEntries.size());
return StringArrayFixTableCreator.createFixTable(colNames, rowData);
}
}
public static CSVParser newParser(CSVProperties props) {
return new CSVParser(
props.delimiter.charAt(0), props.quote.charAt(0),
props.escape.charAt(0),
false /* strict quotes off: don't ignore unquoted strings */,
true /* ignore leading white-space */ );
}
public TestFileIterator(String relationName, Reader reader, char separator, char quotechar,
int skipLines,
boolean hasHeader) throws InputIterationException {
this(relationName, reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, skipLines,
CSVParser.DEFAULT_STRICT_QUOTES, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, hasHeader);
}
public InferredSchema(String pathToCsv, DataType defaultType, char delimiter) {
this.pathToCsv = pathToCsv;
this.defaultType = defaultType;
this.csvParser = new CSVParser(delimiter);
}
public InferredSchema(String pathToCsv, DataType defaultType, char delimiter, char quote) {
this.pathToCsv = pathToCsv;
this.defaultType = defaultType;
this.csvParser = new CSVParser(delimiter, quote);
}
public InferredSchema(String pathToCsv, DataType defaultType, char delimiter, char quote, char escape) {
this.pathToCsv = pathToCsv;
this.defaultType = defaultType;
this.csvParser = new CSVParser(delimiter, quote, escape);
}
public InferredSchema(String pathToCsv, DataType defaultType, char delimiter) {
this.pathToCsv = pathToCsv;
this.defaultType = defaultType;
this.csvParser = new CSVParser(delimiter);
}
public InferredSchema(String pathToCsv, DataType defaultType, char delimiter, char quote) {
this.pathToCsv = pathToCsv;
this.defaultType = defaultType;
this.csvParser = new CSVParser(delimiter, quote);
}
public InferredSchema(String pathToCsv, DataType defaultType, char delimiter, char quote, char escape) {
this.pathToCsv = pathToCsv;
this.defaultType = defaultType;
this.csvParser = new CSVParser(delimiter, quote, escape);
}