public synchronized List<Result> read() throws IOException { CSVParser parser = null; util.ensureBaseResultDirExists(); try { File file = new File(resultFileName); parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.DEFAULT); List<CSVRecord> records = parser.getRecords(); List<Result> results = new ArrayList<>(); String header = null; for (CSVRecord record : records) { // First record is the CSV Header if (record.getRecordNumber() == 1) { header = record.toString(); continue; } List<ResultValue> resultValues = new ArrayList<>(); for (String val : record.toString().split(PherfConstants.RESULT_FILE_DELIMETER)) { resultValues.add(new ResultValue(val)); } Result result = new Result(resultFileDetails, header, resultValues); results.add(result); } return results; } finally { parser.close(); } }
protected static Map<String, Serializable> parseRecord(CSVRecord record, CSVRecord fullHeader) { if (record == null) { log.warn("Record is null."); return null; } log.info("Parsing record number: " + (record.getRecordNumber() - 1)); log.debug(" with content: " + record.toString()); final Map<String, Serializable> map = new HashMap<>(); for (int i = 0; i < record.size(); i++) { String headerFieldType = getHeaderFieldType(fullHeader.get(i)); Object recordField = getRecordField(headerFieldType, record.get(i)); if (recordField != null) { String headerField = getHeaderField(fullHeader.get(i)); String[] headerFieldPart = headerField.split("\\."); Map<String, Serializable> targetMap = map; for (int j = 0; j < headerFieldPart.length - 1; j++) { targetMap = (Map<String, Serializable>) targetMap.computeIfAbsent(headerFieldPart[j], x -> new HashMap<>()); } targetMap.put(headerFieldPart[headerFieldPart.length - 1], (Serializable) recordField); } else { log.warn("Skipped value for record item: " + record.get(i)); } } log.debug("Parsed map: " + map); return map; }
record.size(), record.toString()));
private final static String DATA = "\"Transaction Date\"\t\"Id\"\t\"Product\"\t\"Type\"\t\"Preorder\"\t\"Qty\"\t\"Primary ISBN\"\t\"Imprint Name\"\t\"Title\"\t\"Author\"\t\"Original List Price Currency\"\t\"Original List Price\"\t\"List Price Currency\"\t\"List Price [tax inclusive]\"\t\"List Price [tax exclusive]\"\t\"Country of Sale\"\t\"Publisher Revenue %\"\t\"Publisher Revenue\"\t\"Payment Currency\"\t\"Payment Amount\"\t\"Currency Conversion Rate\"\n" + "\"2016. 09. 01.\"\t\"ID:1166315449551685\"\t\"Single Purchase\"\t\"Sale\"\t\"None\"\t\"1\"\t\"9789633780664\"\t\"Book and Walk Kft\"\t\"Bánk bán\"\t\"József Katona\"\t\"HUF\"\t\"0,00\"\t\"HUF\"\t\"0,00\"\t\"0,00\"\t\"HU\"\t\"52,0%\"\t\"0,00\"\t\"\"\t\"\"\t\"\""; @Test public void parseCsv() throws IOException { final CSVFormat format = CSVFormat.TDF.withQuote('\"').withFirstRecordAsHeader(); Iterable<CSVRecord> records = format.parse(new StringReader(DATA)); System.out.println("Headers: " + Arrays.toString(format.getHeader())); for (CSVRecord record : records) { assertNotNull(record.get("Author")); assertNotNull(record.get("Title")); assertNotNull(record.get("Imprint Name")); assertNotNull(record.get("Primary ISBN")); assertNotNull(record.get("Id")); assertNotNull(record.get("Country of Sale")); assertNotNull(record.get("Qty")); assertNotNull(record.get("List Price [tax exclusive]")); assertNotNull(record.get("Transaction Date")); assertNotNull(record.get("List Price Currency")); assertNotNull(record.get("Payment Amount")); assertNotNull(record.get("Payment Currency")); System.out.println("Record: " + record.toString()); } }
logger.debug("Invalid GDELT record: " + e.toString() + " " + record.toString());
for (CSVRecord record : parser.getRecords()) { if (record.size() < EXPECTED_COLUMNS) { result.markError(record.getRecordNumber(), new Error(ErrorContext.Entity, Errors.CSV_TOO_FEW_COLUMNS, record.getRecordNumber(), record.toString(), EXPECTED_COLUMNS, record.size())); continue;