@Test public void testParseCSV() throws Exception { String[] directives = new String[] { "parse-as-csv body , false", "drop body", "rename body_1 date", "parse-as-csv date / false", "rename date_1 month", "rename date_2 day", "rename date_3 year" }; List<Row> rows = Arrays.asList( new Row("body", "07/29/2013,Debt collection,\"Other (i.e. phone, health club, etc.)\",Cont'd attempts collect " + "debt not owed,Debt is not mine,,,\"NRA Group, LLC\",VA,20147,,N/A,Web,08/07/2013,Closed with non-monetary " + "relief,Yes,No,467801"), new Row("body", "07/29/2013,Mortgage,Conventional fixed mortgage,\"Loan servicing, payments, escrow account\",," + ",,Franklin Credit Management,CT,06106,,N/A,Web,07/30/2013,Closed with explanation,Yes,No,475823") ); rows = TestingRig.execute(directives, rows); Assert.assertEquals(2, rows.size()); Assert.assertEquals("07/29/2013", rows.get(0).getValue("date")); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(columnArg.value()); if (idx == -1) { continue; } String line = (String) row.getValue(idx); if (line == null || line.isEmpty()) { continue; } CSVParser parser = null; try { parser = CSVParser.parse(line, format); List<CSVRecord> csvRecords = parser.getRecords(); for (CSVRecord csvRecord : csvRecords) { if (!checkedHeader && hasHeader && isHeader(csvRecord)) { for (int i = 0; i < csvRecord.size(); i++) { headers.add(csvRecord.get(i).trim().replaceAll("\\s+", "_")); } if (rows.size() > 0) { return new ArrayList<>(); } } else { toRow(csvRecord, row); } } } catch (IOException e) { throw new ErrorRowException(NAME, e.getMessage(), 1); } } return rows; }
CsvParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(columnArg.value()); if (idx == -1) { continue; } String line = (String) row.getValue(idx); if (line == null || line.isEmpty()) { continue; } CSVParser parser = null; try { parser = CSVParser.parse(line, format); List<CSVRecord> csvRecords = parser.getRecords(); for (CSVRecord csvRecord : csvRecords) { if (!checkedHeader && hasHeader && isHeader(csvRecord)) { for (int i = 0; i < csvRecord.size(); i++) { headers.add(csvRecord.get(i).trim().replaceAll("\\s+", "_")); } if (rows.size() > 0) { return new ArrayList<>(); } } else { toRow(csvRecord, row); } } } catch (IOException e) { throw new ErrorRowException(NAME, e.getMessage(), 1); } } return rows; } }
CsvParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(columnArg.value()); if (idx == -1) { continue; } String line = (String) row.getValue(idx); if (line == null || line.isEmpty()) { continue; } CSVParser parser = null; try { parser = CSVParser.parse(line, format); List<CSVRecord> csvRecords = parser.getRecords(); for (CSVRecord csvRecord : csvRecords) { if (!checkedHeader && hasHeader && isHeader(csvRecord)) { for (int i = 0; i < csvRecord.size(); i++) { headers.add(csvRecord.get(i).trim().replaceAll("\\s+", "_")); } if (rows.size() > 0) { return new ArrayList<>(); } } else { toRow(csvRecord, row); } } } catch (IOException e) { throw new ErrorRowException(NAME, e.getMessage(), 1); } } return rows; }  }
CsvParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(columnArg.value()); if (idx == -1) { continue; } String line = (String) row.getValue(idx); if (line == null || line.isEmpty()) { continue; } CSVParser parser = null; try { parser = CSVParser.parse(line, format); List<CSVRecord> csvRecords = parser.getRecords(); for (CSVRecord csvRecord : csvRecords) { if (!checkedHeader && hasHeader && isHeader(csvRecord)) { for (int i = 0; i < csvRecord.size(); i++) { headers.add(csvRecord.get(i).trim().replaceAll("\\s+", "_")); } if (rows.size() > 0) { return new ArrayList<>(); } } else { toRow(csvRecord, row); } } } catch (IOException e) { throw new ErrorRowException(NAME, e.getMessage(), 1); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
CsvParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(columnArg.value()); if (idx == -1) { continue; } String line = (String) row.getValue(idx); if (line == null || line.isEmpty()) { continue; } CSVParser parser = null; try { parser = CSVParser.parse(line, format); List<CSVRecord> csvRecords = parser.getRecords(); for (CSVRecord csvRecord : csvRecords) { if (!checkedHeader && hasHeader && isHeader(csvRecord)) { for (int i = 0; i < csvRecord.size(); i++) { headers.add(csvRecord.get(i).trim().replaceAll("\\s+", "_")); } if (rows.size() > 0) { return new ArrayList<>(); } } else { toRow(csvRecord, row); } } } catch (IOException e) { throw new ErrorRowException(NAME, e.getMessage(), 1); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; }