本文整理匯總了Java中org.apache.commons.csv.CSVParser.getHeaderMap方法的典型用法代碼示例。如果您正苦於以下問題:Java CSVParser.getHeaderMap方法的具體用法?Java CSVParser.getHeaderMap怎麽用?Java CSVParser.getHeaderMap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.csv.CSVParser
的用法示例。
在下文中一共展示了CSVParser.getHeaderMap方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: apply
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
@Override
public List<CSVReportEntry> apply(final GetCredentialReportResult report) {
Assert.state(Textcsv.toString().equals(report.getReportFormat()), "unknown credential report format: " + report.getReportFormat());
try (final Reader r = new BufferedReader(new InputStreamReader(new ByteBufferBackedInputStream(report.getContent())))) {
final CSVParser parser = new CSVParser(r, CSV_FORMAT);
final Map<String, Integer> headers = parser.getHeaderMap();
Assert.state(headers.containsKey("user"), "Header 'user' not found in CSV");
Assert.state(headers.containsKey("arn"), "Header 'arn' not found in CSV");
Assert.state(headers.containsKey("password_enabled"), "Header 'password_enabled' not found in CSV");
Assert.state(headers.containsKey("mfa_active"), "Header 'mfa_active' not found in CSV");
Assert.state(headers.containsKey("access_key_1_active"), "Header 'access_key_1_active' not found in CSV");
Assert.state(headers.containsKey("access_key_2_active"), "Header 'access_key_2_active' not found in CSV");
return stream(parser.spliterator(), false).map(this::toCSVReportEntry).filter(Objects::nonNull).collect(toList());
} catch (final IOException e) {
throw new RuntimeException("Could not read csv report", e);
}
}
示例2: getColumnLabels
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
private static List<String> getColumnLabels(final CSVParser parser) {
final Map<String, Integer> headerMap = parser.getHeaderMap();
final List<String> labels = new ArrayList<>(headerMap.size());
for (final String label : headerMap.keySet()) {
final int pos = headerMap.get(label);
labels.add(pos, label);
}
return labels;
}
示例3: getCSVHParser
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static CSVHParser getCSVHParser(File file) {
if (file.exists()) {
try (Reader in = new FileReader(file)) {
CSVParser parser = CSVFormat.EXCEL.withHeader().withSkipHeaderRecord().withIgnoreEmptyLines().parse(in);
if (!parser.getHeaderMap().isEmpty()) {
return new CSVHParser(parser.getHeaderMap(), parser.getRecords());
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
} else {
LOGGER.log(Level.SEVERE, "File [{0}] doesn''t exist", file.getAbsolutePath());
}
return null;
}
示例4: readHeader
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
private static Set<String> readHeader(final CSVParser parser) {
final Map<String, Integer> map = parser.getHeaderMap();
if (map == null) {
throw new IWSException(IWSErrors.CSV_HEADER_ERROR, "The CSV did not have a valid header.");
}
return map.keySet();
}
示例5: execute
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
@Override
public void execute(Query query) {
String queryName = query.name;
String queryValue = query.value.trim();
log.info("Running {}", queryName);
Map<String, String> metadata = query.getMetadata();
String delimiter = Query.getKey(metadata, METADATA_DELIMITER_KEY).orElse(defaultDelimiter);
char character = delimiter.charAt(0);
log.info("Using delimiter as a character: {}", character);
boolean isFile = isPath(queryValue);
log.info("Running or loading data from \n{}", queryValue);
try (InputStream stream = isFile ? new FileInputStream(queryValue) : new ByteArrayInputStream(getBytes(queryValue));
Reader reader = new InputStreamReader(stream)) {
CSVFormat format = CSVFormat.RFC4180.withDelimiter(character).withFirstRecordAsHeader().withIgnoreSurroundingSpaces();
CSVParser parser = new CSVParser(reader, format);
Map<String, Integer> headerIndices = parser.getHeaderMap();
Map<String, Type> typeMap = getTypeMapping(headerIndices, metadata);
Result result = query.createResults();
parser.iterator().forEachRemaining(r -> addRow(r, result, typeMap));
} catch (Exception e) {
log.error("Error while parsing data for {} with {}", queryName, queryValue);
log.error("Error", e);
query.setFailure(e.toString());
}
}
示例6: CsvRecordReader
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public CsvRecordReader(Reader reader, CSVFormat format, long length, boolean strict) throws IOException {
this.length = length;
this.strict = strict;
parser = new CSVParser(reader, format);
iterator = parser.iterator();
if (parser.getHeaderMap() == null) {
colLength = null;
} else {
colLength = parser.getHeaderMap().size();
}
}
示例7: getRows
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
@Override
public RowSet getRows(String baseQuery,
Map<String, String> preds,
int limit,
int offset) throws Exception{
filename = conf.getString(MacroBaseConf.CSV_INPUT_FILE);
Compression compression = conf.getCsvCompression();
if (compression == Compression.GZIP) {
InputStream fileStream = new FileInputStream(filename);
InputStream gzipStream = new GZIPInputStream(fileStream);
Reader decoder = new InputStreamReader(gzipStream);
csvParser = new CSVParser(decoder, CSVFormat.DEFAULT.withHeader());
} else {
File csvFile = new File(conf.getString(MacroBaseConf.CSV_INPUT_FILE));
csvParser = CSVParser.parse(csvFile, Charset.defaultCharset(), CSVFormat.DEFAULT.withHeader());
}
schema = csvParser.getHeaderMap();
Iterator<CSVRecord> rawIterator = csvParser.iterator();
int rowCount = 0;
List<RowSet.Row> rows = Lists.newArrayList();
while (rawIterator.hasNext() && rowCount < limit) {
CSVRecord record = rawIterator.next();
List<ColumnValue> columnValues = Lists.newArrayList();
if (includeRow(record, preds)) {
for (Map.Entry<String, Integer> se : schema.entrySet()) {
columnValues.add(new ColumnValue(se.getKey(),record.get(se.getValue())));
}
rows.add(new RowSet.Row(columnValues));
rowCount++;
}
}
return new RowSet(rows);
}
示例8: importPatterns
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
public static Map<String, Pattern> importPatterns(String patternDirectoryPath) {
Map<String, Pattern> patterns = new HashMap<String, Pattern>();
File patternDir = new File(patternDirectoryPath);
if (patternDir.exists()) {
File[] patternFiles = patternDir.listFiles();
for (File file : patternFiles) {
try {
CSVParser csvParser = CSVParser.parse(file, Charsets.UTF_8, CSVFormat.RFC4180.withHeader());
Map<String, Integer> headerMap = csvParser.getHeaderMap();
for (CSVRecord record : csvParser) {
Pattern p = getPattern(record, headerMap);
if (p != null) {
patterns.put(p.getId(), p);
// System.out.println(p.getPrintStr());
} else {
logger.error("Error in reading the pattern: " + record.toString());
}
}
} catch (IOException e) {
logger.error("Error in reading the patterns from the file " + file.getAbsolutePath());
e.printStackTrace();
}
}
}
return patterns;
}
示例9: getInputFromCSV
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
/**
* Takes a CSV File and parses into a List of CSVRecords objects. Also verifies
* that the SYMBOL variable exists in the csv since that is the critical piece
* @param csvFile the file to input
* @return List of CSVRecords
* @throws IOException
*/
private static List<CSVRecord> getInputFromCSV(File csvFile)
throws IOException {
FileReader fr = new FileReader(csvFile);
// For Testing
try {
printFile(csvFile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
CSVParser csvp = new CSVParser(fr, CSVFormat.EXCEL.withHeader());
List<CSVRecord> list = csvp.getRecords();
Map<String, Integer> headers = csvp.getHeaderMap();
// Get Header map and index of SYMBOL
try {
// Sole purpose of i is to check to see if Symbol exists
@SuppressWarnings("unused")
int i = headers.get(SYMBOL).intValue();
csvp.close();
return list;
} catch (NullPointerException e) {
System.err.println("No " + SYMBOL + " Column Found");
csvp.close();
throw new IOException("Invalid File Format");
}
}
示例10: getStream
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
@Override
public MBStream<Datum> getStream() throws Exception {
if(!loaded) {
long st = System.currentTimeMillis();
filename = conf.getString(MacroBaseConf.CSV_INPUT_FILE);
Compression compression = conf.getCsvCompression();
if (compression == Compression.GZIP) {
InputStream fileStream = new FileInputStream(filename);
InputStream gzipStream = new GZIPInputStream(fileStream);
Reader decoder = new InputStreamReader(gzipStream);
csvParser = new CSVParser(decoder, CSVFormat.DEFAULT.withHeader());
} else {
File csvFile = new File(conf.getString(MacroBaseConf.CSV_INPUT_FILE));
csvParser = CSVParser.parse(csvFile, Charset.defaultCharset(), CSVFormat.DEFAULT.withHeader());
}
schema = csvParser.getHeaderMap(); //equal to resultSet.getmetadata or smt
for (Map.Entry<String, Integer> se : schema.entrySet()) {
conf.getEncoder().recordAttributeName(se.getValue() + 1, se.getKey()); //numbering off each column for encoding
}
// Load all records into memory to filter out rows with missing data
Iterator<CSVRecord> rawIterator = csvParser.iterator();
int numRows = 0;
while (rawIterator.hasNext()) {
try {
CSVRecord record = rawIterator.next();
Datum curRow = parseRecord(record);
dataStream.add(curRow);
numRows++;
} catch (NumberFormatException e) {
badRows++;
}
}
log.info("{}/{} rows successfully parsed ({} malformed rows)", numRows, numRows + badRows, badRows);
}
return dataStream;
}
示例11: fromCsv
import org.apache.commons.csv.CSVParser; //導入方法依賴的package包/類
static public Series fromCsv(String path, boolean header, DateTimeFormatter dtf, LocalTime lt) throws Exception {
if(dtf == null) {
if(lt == null) dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
else dtf = DateTimeFormatter.ISO_DATE;
}
// Parse and import the csv
CSVFormat csvFmt = CSVFormat
.DEFAULT
.withCommentMarker('#')
.withIgnoreSurroundingSpaces();
if(header) csvFmt = csvFmt.withHeader();
CSVParser csv = csvFmt.parse(new BufferedReader(new FileReader(path)));
int ncols = -1;
Series result = null;
double [] values = null;
for(CSVRecord rec : csv.getRecords()) {
if(result == null) {
ncols = rec.size() - 1;
values = new double[ncols];
result = new Series(ncols);
}
for(int ii = 0; ii < ncols; ++ii) {
values[ii] = Double.parseDouble(rec.get(ii + 1));
}
LocalDateTime ldt;
if(lt != null) {
ldt = LocalDate.parse(rec.get(0), dtf).atTime(lt);
} else {
ldt = LocalDateTime.parse(rec.get(0), dtf);
}
result.append(ldt, values);
}
if(header) {
Map<String,Integer> headerMap = csv.getHeaderMap();
result.clearNames();
for(Map.Entry<String,Integer> me : headerMap.entrySet()) {
if(me.getValue() > 0) result.setName(me.getKey(), me.getValue() - 1);
}
}
return result;
}