本文整理汇总了Java中org.apache.commons.csv.CSVParser.forEach方法的典型用法代码示例。如果您正苦于以下问题:Java CSVParser.forEach方法的具体用法?Java CSVParser.forEach怎么用?Java CSVParser.forEach使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.csv.CSVParser
的用法示例。
在下文中一共展示了CSVParser.forEach方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadData
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
@Override
public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException {
for (Tuple<String, File> file : files) {
CSVParser parser = format.parse(new FileReader(file.getRight()));
String filename = file.getLeft();
//remove well-known extensions
if (filename.endsWith(".csv") || filename.endsWith(".tsv") || filename.endsWith(".txt")) {
filename = filename.substring(0, filename.length() - 4);
}
importer.startCollection(filename);
parser.getHeaderMap().forEach((name, column) -> importer.registerPropertyName(column, name));
parser.forEach(row -> {
importer.startEntity();
for (int i = 0; i < row.size(); i++) {
importer.setValue(i, row.get(i));
}
importer.finishEntity();
});
importer.finishCollection();
}
}
示例2: parse
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public List<PhotoLocationKeyword> parse() throws IOException{
CSVParser parser = CSVFormat.RFC4180.withHeader().
withDelimiter(',')
.withAllowMissingColumnNames(true)
.parse(new InputStreamReader(this.inputStreamSource.getInputStream()));
List<PhotoLocationKeyword> keywords = new ArrayList<>();
parser.forEach((record) -> {
keywords.add(parseRecord(record));
});
return keywords;
}
示例3: getMapFromFromCsvFile
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public Map<String,String> getMapFromFromCsvFile(String filename, CSVFormat csvFormat) throws IOException {
try(FileReader fileReader = new FileReader(new File(filename))) {
CSVParser csvParser = new CSVParser(fileReader, csvFormat);
Map<String,String> map = new HashMap<>();
csvParser.forEach(record -> map.put(record.get(0), record.get(1)));
return map;
} catch (IOException e) {
logger.error("Failed to open CSV file {} for reading", filename);
throw e;
}
}
示例4: parseFile
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
static void parseFile(final String fileStr, final PublishSubject<Map<String, String>> subject) throws Exception {
final CSVParser parser = CSVParser.parse(
new File(fileStr), StandardCharsets.UTF_8, CSVFormat.DEFAULT
);
try {
parser.forEach(csvRecord -> subject.onNext(toMap(csvRecord)));
subject.onComplete();
} finally {
parser.close();
}
}
示例5: importDatasource
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
@Override
protected void importDatasource(Datasource datasource, List<String> geographyScope, List<String> temporalScope, List<String> datasourceLocation) throws Exception {
LocalDateTime TIMESTAMP = TimedValueUtils.parseTimestampString("2011");
// Collect materialised attributes
List<Attribute> attributes = new ArrayList<>();
for (Attribute attribute : datasource.getTimedValueAttributes()) {
attributes.add(AttributeUtils.getByProviderAndLabel(attribute.getProvider(), attribute.getLabel()));
}
// Looping through all the subjects provided in the recipe and saving there respective values
for (SubjectRecipe subjectTypeFromRecipe : subjectRecipes) {
OaImporter.OaType oaType = OaImporter.OaType.valueOf(subjectTypeFromRecipe.getSubjectType());
SubjectType subjectType = SubjectTypeUtils.getOrCreate(AbstractONSImporter.PROVIDER,
oaType.name(), oaType.datasourceSpec.getDescription());
List<TimedValue> timedValueBuffer = new ArrayList<>();
String dataUrl = getDataUrl(datasource.getDatasourceSpec().getId(), subjectTypeFromRecipe.getSubjectType());
InputStream dataStream = downloadUtils.fetchInputStream(
new URL(dataUrl), getProvider().getLabel(), ".csv");
CSVParser csvParser = new CSVParser(new InputStreamReader(dataStream),
CSVFormat.RFC4180.withFirstRecordAsHeader());
csvParser.forEach(record -> {
Subject subject = SubjectUtils.getSubjectByTypeAndLabel(subjectType, record.get("geography code"));
if (subject != null) {
attributes.forEach(attribute -> {
String value = record.get(attribute.getDescription());
TimedValue timedValue = new TimedValue(subject, attribute, TIMESTAMP, Double.valueOf(value));
timedValueBuffer.add(timedValue);
});
}
});
saveAndClearTimedValueBuffer(timedValueBuffer);
}
}
示例6: readFromCsv
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public static ReadResult readFromCsv(InputStream input, Consumer<? super CSVRecord> action) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input, CHARSET), READ_BUFFER_SIZE);
CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL
.withDelimiter(';')
.withRecordSeparator('\n')
);
RecordReaderWrapper wrapper = new RecordReaderWrapper(action);
parser.forEach(wrapper);
parser.close();
reader.close();
return new ReadResult(wrapper.readedReacords, wrapper.successfulReadedRecors);
}