本文整理匯總了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);
}