List of usage examples for org.apache.commons.csv CSVParser parse
public static CSVParser parse(final String string, final CSVFormat format) throws IOException
From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.synchronize.csvparser.DKBCsvParser.java
public Iterable<DKBTransaction> getTransactions() throws IOException { CSVParser parser = CSVParser.parse(contentCsv, csvFormat.withFirstRecordAsHeader()); List<DKBTransaction> transactions = new ArrayList<>(); for (CSVRecord csvRecord : parser.getRecords()) { transactions.add(createTransaction(csvRecord)); }//from w ww . j a v a 2 s . c om return transactions; }
From source file:com.adobe.acs.commons.exporters.impl.users.UserExportServletTest.java
@Test public void testWithGroupDirectFilter() throws Exception { // Build parameters JsonObject params = buildParameterObject("direct", "users"); Map<String, Object> parameters = new HashMap<>(); parameters.put("params", params); context.request().setParameterMap(parameters); servlet.doGet(context.request(), context.response()); assertEquals(context.response().getStatus(), 200); String output = context.response().getOutputAsString(); CSVParser parser = CSVParser.parse(output, CSVFormat.DEFAULT.withHeader()); assertAllUsersPresent(parser.getRecords(), "alice", "bob"); }
From source file:com.google.cloud.genomics.dockerflow.args.ArgsTableBuilder.java
/** * Load the workflow arguments from a CSV file. The header of the CSV contains the input or output * parameter names. Each row contains the workflow args for a single run. To run 100 instances of * a workflow concurrently, create a CSV with a header row plus 100 rows for each set of * parameters./*from ww w . j a v a 2 s . c o m*/ * * <p>Columns by default are input parameters, passed as environment variables to the Docker * script. For file parameters, you can prefix the column header with "<" for input or ">" for * output. For clarity, you can also prefix the regular input parameters as "<", if you like. * * <p>The column header can also be "logging", which is a reserved name for the logging path. * * @param csvFile CSV file (RFC4180) that's local or in GCS * @return a map with the key being the clientId * @throws IOException */ static Map<String, WorkflowArgs> loadCsv(String csvFile) throws IOException { Map<String, WorkflowArgs> retval = new HashMap<String, WorkflowArgs>(); String csv = FileUtils.readAll(csvFile); CSVParser parser = CSVParser.parse(csv, CSVFormat.RFC4180); // Parse header List<String> header = null; int row = 0; // Parse by row for (CSVRecord csvRecord : parser) { ArgsBuilder args = ArgsBuilder.of(String.valueOf(row)); LOG.debug(StringUtils.toJson(csvRecord)); // Parse header the first time if (row == 0) { header = new ArrayList<String>(); for (String col : csvRecord) { header.add(col); } } else { // Set parameter defined in each column for (int col = 0; col < header.size(); ++col) { String name = header.get(col); String val = csvRecord.get(col); if (name.startsWith(PREFIX_INPUT)) { name = name.replace(PREFIX_INPUT, ""); args.input(name, val); } else if (name.startsWith(PREFIX_OUTPUT)) { name = name.replace(PREFIX_OUTPUT, ""); args.output(name, val); } else if (LOGGING.equals(name)) { args.logging(val); } else { args.input(name, val); } } WorkflowArgs a = args.build(); a.setRunIndex(row); retval.put(a.getClientId(), a); } ++row; } return retval; }
From source file:com.adobe.acs.commons.exporters.impl.users.UserExportServletTest.java
@Test public void testWithGroupIndirectFilter() throws Exception { // Build parameters JsonObject params = buildParameterObject("indirect", "allusers"); Map<String, Object> parameters = new HashMap<>(); parameters.put("params", params); context.request().setParameterMap(parameters); servlet.doGet(context.request(), context.response()); assertEquals(context.response().getStatus(), 200); String output = context.response().getOutputAsString(); CSVParser parser = CSVParser.parse(output, CSVFormat.DEFAULT.withHeader()); assertAllUsersPresent(parser.getRecords(), "alice", "bob"); }
From source file:co.cask.hydrator.transforms.ParseCSV.java
@Override public void transform(StructuredRecord in, Emitter<StructuredRecord> emitter) throws Exception { // Field has to string to be parsed correctly. For others throw an exception. String body = in.get(config.field); // Parse the text as CSV and emit it as structured record. try {/*from w ww . j a v a 2s.c o m*/ CSVParser parser = CSVParser.parse(body, csvFormat); List<CSVRecord> records = parser.getRecords(); for (CSVRecord record : records) { if (fields.size() == record.size()) { StructuredRecord sRecord = createStructuredRecord(record); emitter.emit(sRecord); } else { LOG.warn("Skipping record as ouput schema specified has '{}' fields, while CSV record has '{}'", fields.size(), record.size()); // Write the record to error Dataset. } } } catch (IOException e) { LOG.error("There was a issue parsing the record. ", e.getLocalizedMessage()); } }
From source file:com.adobe.acs.commons.exporters.impl.users.UserExportServletTest.java
@Test public void testWithGroupBothFIlter() throws Exception { // Build parameters JsonObject params = buildParameterObject("", "allusers"); Map<String, Object> parameters = new HashMap<>(); parameters.put("params", params); context.request().setParameterMap(parameters); servlet.doGet(context.request(), context.response()); assertEquals(context.response().getStatus(), 200); String output = context.response().getOutputAsString(); CSVParser parser = CSVParser.parse(output, CSVFormat.DEFAULT.withHeader()); assertAllUsersPresent(parser.getRecords(), "alice", "bob", "charly"); }
From source file:com.joeyfrazee.nifi.processors.DuplicateByAttribute.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { FlowFile flowFile = session.get();/*from w ww .jav a2 s . c o m*/ if (flowFile == null) { return; } String attributeToDuplicateBy = context.getProperty(ATTRIBUTE_TO_DUPLICATE_BY) .evaluateAttributeExpressions(flowFile).getValue(); String outputAttribute = context.getProperty(OUTPUT_ATTRIBUTE).evaluateAttributeExpressions(flowFile) .getValue(); try { final String csv = flowFile.getAttribute(attributeToDuplicateBy); final CSVParser parser = CSVParser.parse(csv, CSVFormat.DEFAULT); for (final CSVRecord record : parser) { for (final String v : record) { FlowFile copy = session.clone(flowFile); copy = session.removeAttribute(copy, attributeToDuplicateBy); copy = session.putAttribute(copy, outputAttribute, v); session.transfer(copy, REL_SUCCESS); } } } catch (Exception e) { getLogger().error("{} value {} could not be parsed", new Object[] { ATTRIBUTE_TO_DUPLICATE_BY.getName(), attributeToDuplicateBy }, e); session.transfer(flowFile, REL_FAILURE); return; } session.transfer(flowFile, REL_ORIGINAL); }
From source file:com.team3637.service.ScheduleServiceMySQLImpl.java
@Override public void importCSV(String inputFile) { try {/*from w w w.jav a2 s . c om*/ String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile))); csvData = csvData.replaceAll("\\r", ""); CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n")); for (CSVRecord record : parser) { Schedule schedule = new Schedule(); schedule.setId(Integer.parseInt(record.get(0))); schedule.setMatchNum(Integer.parseInt(record.get(1))); schedule.setB1(Integer.parseInt(record.get(2))); schedule.setB2(Integer.parseInt(record.get(3))); schedule.setB3(Integer.parseInt(record.get(4))); schedule.setR1(Integer.parseInt(record.get(5))); schedule.setR2(Integer.parseInt(record.get(6))); schedule.setR3(Integer.parseInt(record.get(7))); if (checkForMatch(schedule)) update(schedule); else create(schedule); } } catch (IOException e) { e.printStackTrace(); } }
From source file:co.cask.hydrator.transforms.CSVParser2.java
@Override public void transform(StructuredRecord in, Emitter<StructuredRecord> emitter) throws Exception { // Field has to string to be parsed correctly. For others throw an exception. String body = in.get(config.field); // If decoder is not NONE, then apply decoder. byte[] decodedPayLoad; if (!config.decoder.equalsIgnoreCase("NONE")) { decodedPayLoad = decodePayLoad(body); } else {/* w w w. jav a 2s .c o m*/ decodedPayLoad = body.getBytes(); } // If decompess is not NONE, then apply decompressor. byte[] uncompressedPayLoad = decodedPayLoad.clone(); if (!config.decompress.equalsIgnoreCase("NONE")) { if (config.decompress.equalsIgnoreCase("SNAPPY")) { uncompressedPayLoad = Snappy.uncompress(decodedPayLoad); } else if (config.decompress.equalsIgnoreCase("GZIP")) { uncompressedPayLoad = ungzip(decodedPayLoad); } else if (config.decompress.equalsIgnoreCase("ZIP")) { uncompressedPayLoad = unzip(decodedPayLoad); } } // Parse the text as CSV and emit it as structured record. try { CSVParser parser = CSVParser.parse(new String(uncompressedPayLoad), csvFormat); List<CSVRecord> records = parser.getRecords(); for (CSVRecord record : records) { if (fields.size() == record.size()) { StructuredRecord sRecord = createStructuredRecord(record); emitter.emit(sRecord); } else { // Write the record to error Dataset. } } } catch (IOException e) { } }
From source file:edu.ucla.cs.scai.swim.qa.ontology.dbpedia.DBpediaOntologyOld.java
private void processFile(BufferedReader in, DBpediaCategory category, HashMap<String, HashSet<DBpediaAttribute>> map) throws IOException { //The first header contains the properties labels. //The second header contains the properties URIs. //The third header contains the properties range labels. //The fourth header contains the properties range URIs. String l1 = in.readLine();//from www . j a v a2s. c o m String l2 = in.readLine(); String l3 = in.readLine(); String l4 = in.readLine(); Iterator<CSVRecord> it = CSVParser.parse(l1 + "\n" + l2 + "\n" + l3 + "\n" + l4, CSVFormat.RFC4180) .iterator(); Iterator<String> r1 = it.next().iterator(); Iterator<String> r2 = it.next().iterator(); Iterator<String> r3 = it.next().iterator(); Iterator<String> r4 = it.next().iterator(); while (r1.hasNext() && r2.hasNext() && r3.hasNext() && r4.hasNext()) { String name = r1.next(); String uri = r2.next(); String range = r3.next(); String rangeUri = r4.next(); HashSet<DBpediaAttribute> as = map.get(name); if (as == null) { as = new HashSet<>(); map.put(name, as); } DBpediaAttribute a = attributesByUri.get(uri); if (a == null) { a = new DBpediaAttribute(); a.setLabel(name); a.setRange(range); a.rangeUri.add(rangeUri); a.setUri(uri); attributesByUri.put(a.getUri(), a); } as.add(a); if (abstractAttribute == null && uri.equals("http://www.w3.org/2000/01/rdf-schema#comment")) { abstractAttribute = a; System.out.println("Abstract attribute found"); } category.domainOfAttributes.add(a); } if (r1.hasNext() || r2.hasNext() || r3.hasNext() || r4.hasNext()) { System.out.println( "Error: number of columns not matching in first rows of " + category.getLabel() + " csv file"); } }