Java Code Examples for org.apache.commons.csv.CSVPrinter#printRecord()

The following examples show how to use org.apache.commons.csv.CSVPrinter#printRecord() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InformationExtraction2Postgres.java    From newsleak with GNU Affero General Public License v3.0 8 votes vote down vote up
private void mappingIdsInMetadata(String mappedMetadataFile) throws Exception {
	// read mappings file
	FileInputStream fis = new FileInputStream(this.dataDirectory + File.separator + this.metadataFile + ".id-map");
	ObjectInputStream ois = new ObjectInputStream(fis);
	HashMap<Integer, ArrayList<Integer>> documentIdMapping = (HashMap<Integer, ArrayList<Integer>>) ois
			.readObject();
	ois.close();

	// open metadata file, replace ids, write to temporary metadata file
	BufferedWriter writer = new BufferedWriter(new FileWriter(mappedMetadataFile));
	CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.RFC4180);

	BufferedReader reader = new BufferedReader(new FileReader(this.dataDirectory + File.separator + this.metadataFile));
	Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(reader);
	for (CSVRecord record : records) {
		Integer tmpDocId = Integer.parseInt(record.get(0));
		if (documentIdMapping.containsKey(tmpDocId)) { 
			ArrayList<Integer> mappedIds = documentIdMapping.get(tmpDocId);
			int nParts = mappedIds.size();
			int partCounter = 0;
			for (Integer newsleakDocId : mappedIds) {
				String key = StringUtils.capitalize(record.get(1));
				String value = record.get(2);
				if (nParts > 1 && key.equals("Subject")) {
					partCounter++;
					value += " (" + partCounter + "/" + nParts + ")";
				}
				ArrayList<String> meta = new ArrayList<String>();
				meta.add(newsleakDocId.toString());
				meta.add(key);
				meta.add(value);
				meta.add(record.get(3));
				csvPrinter.printRecord(meta);
			}
		}
	}
	csvPrinter.close();
	reader.close();
}
 
Example 2
Source File: ExportHelper.java    From argument-reasoning-comprehension-task with Apache License 2.0 6 votes vote down vote up
public static String exportMetaDataToCSV(List<StandaloneArgument> arguments)
        throws IOException
{
    StringWriter sw = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(
            "id", "author", "annotatedStance", "timestamp", "debateMetaData.title",
            "debateMetaData.description", "debateMetaData.url"
    ));

    for (StandaloneArgument argument : arguments) {
        csvPrinter.printRecord(
                argument.getId(),
                argument.getAuthor(),
                argument.getAnnotatedStance(),
                argument.getTimestamp(),
                argument.getDebateMetaData().getTitle(),
                argument.getDebateMetaData().getDescription(),
                argument.getDebateMetaData().getUrl()
        );
    }

    sw.flush();

    return sw.toString();
}
 
Example 3
Source File: CSVGenerator.java    From janusgraph-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Create csv files for a VertexType
 * @param type a vertex type
 * @param outputDirectory the output folder to write the csv file
 */
void writeVertexCSV(VertexTypeBean type, String outputDirectory ){
    String csvFile = outputDirectory + "/" + type.name + ".csv";
    ArrayList<String> header = new ArrayList<String>();
    header.add("node_id");
    header.addAll(type.columns.keySet());
    int botId = idFactory.getMinId(type.name);
    int topId = idFactory.getMaxId(type.name);
    try {
        CSVPrinter csvFilePrinter = new CSVPrinter(new FileWriter(csvFile), csvFileFormat);
        csvFilePrinter.printRecord(header);
        for (int i = botId; i<=topId; i++){
            ArrayList<Object> record = new ArrayList<Object>();
            record.add(i);
            record.addAll(generateOneRecord(type.columns));
            csvFilePrinter.printRecord(record);
        }
        csvFilePrinter.close();
        System.out.println("Generated vertex file: "+ csvFile);
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}
 
Example 4
Source File: CSVUtil.java    From searoute with European Union Public License 1.2 6 votes vote down vote up
public static void save(Collection<Map<String, String>> data, String outFile, CSVFormat cf) {
	try {
		FileWriter out = new FileWriter(outFile);
		String[] header = cf.getHeader(); int nb = header.length;
		CSVPrinter printer = new CSVPrinter(out, cf);
		for(Map<String, String> raw : data) {
			String[] values = new String[nb];
			for(int i=0; i<nb; i++) values[i]=raw.get(header[i]);
			printer.printRecord(values);
		}
		printer.close();
		out.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: ImportantChemicalsWikipedia.java    From act with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This function writes the important chemicals set to a TSV file.
 * @param outputPath a String indicating where the file should be written (including its name)
 */
public void writeToTSV(String outputPath) {
  try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath));
    CSVPrinter printer = new CSVPrinter(writer, TSV_FORMAT);
    printer.printComment("This file has been generated by the ImportantChemicalsWikipedia.java script.");
    printer.printComment("Format: WIKIPEDIA<tab><wikipedia url><tab><inchi><tab><metadata>");
    for (ImportantChemical importantChemical : importantChemicalsWikipedia) {
      List<String> nextLine = new ArrayList<>();
      nextLine.add(importantChemical.getType());
      nextLine.add(importantChemical.getDbid());
      nextLine.add(importantChemical.getInchi());
      nextLine.add(mapper.writeValueAsString(importantChemical.getMetadata()));
      printer.printRecord(nextLine);
    }
    printer.flush();
    writer.close();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 6
Source File: JarRchedWriter.java    From Decca with MIT License 6 votes vote down vote up
public void writeCsv(String outPath, boolean append) {
	try {
		// final String[] header = { "projectId", "conflictId", "type", "origin",
		// "load", "other" };
		// CSVFormat format = CSVFormat.DEFAULT.withHeader(header);

		CSVPrinter printer = new CSVPrinter(new FileWriter(outPath, append), CSVFormat.DEFAULT);
		for (NodeConflict conflict : NodeConflicts.i().getConflicts()) {
			FourRow fourRow = conflict.getRiskAna().getFourRow();
			printer.printRecord(fourRow.mthdRow);
			printer.printRecord(fourRow.mthdNameRow);
			printer.printRecord(fourRow.serviceRow);
			printer.printRecord(fourRow.serviceNameRow);
			printer.flush();
		}
		printer.close();
	} catch (Exception e) {
		MavenUtil.i().getLog().error("can't write risk result:", e);
	}
}
 
Example 7
Source File: ClassDupRiskWriter.java    From Decca with MIT License 6 votes vote down vote up
public void writeRchNum(String outPath, boolean append) {
	try {
		CSVPrinter printer = new CSVPrinter(new FileWriter(outPath, append), CSVFormat.DEFAULT);
		DepJarCgs jarCgs = new DepJarCgs();
		for (DupClsJarPair jarPair : getJarPairs().getAllJarPair()) {
			FourRow fourRow = jarPair.getPairRisk(jarCgs).getFourRow();
			printer.printRecord(fourRow.mthdRow);
			printer.printRecord(fourRow.mthdNameRow);
			printer.printRecord(fourRow.serviceRow);
			printer.printRecord(fourRow.serviceNameRow);
		}
		printer.close();
	} catch (Exception e) {
		MavenUtil.i().getLog().error("can't write reach class number:", e);
	}
}
 
Example 8
Source File: CsvUtils.java    From webtau with Apache License 2.0 5 votes vote down vote up
public static String serialize(Stream<String> header, Stream<List<Object>> rows) {
    try {
        StringWriter out = new StringWriter();
        CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader(header.toArray(String[]::new)));

        Iterator<List<Object>> it = rows.iterator();
        while (it.hasNext()) {
            csvPrinter.printRecord(it.next());
        }

        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: TableUtils.java    From argument-reasoning-comprehension-task with Apache License 2.0 5 votes vote down vote up
/**
 * Converts Guava table to a CSV table
 *
 * @param table                   table
 * @param csvFormat               CSV format
 * @param missingValuePlaceholder print if a value is missing (empty string by default)
 * @param <T>                     object type (string)
 * @return table
 * @throws IOException exception
 */
public static <T> String tableToCsv(Table<String, String, T> table, CSVFormat csvFormat,
        String missingValuePlaceholder)
        throws IOException
{
    StringWriter sw = new StringWriter();
    CSVPrinter printer = new CSVPrinter(sw, csvFormat);

    List<String> firstRow = new ArrayList<>();
    firstRow.add(" ");
    firstRow.addAll(table.columnKeySet());
    printer.printRecord(firstRow);

    for (String rowKey : table.rowKeySet()) {
        printer.print(rowKey);
        for (String columnKey : table.columnKeySet()) {
            T value = table.get(rowKey, columnKey);

            if (value == null) {
                printer.print(missingValuePlaceholder);
            }
            else {
                printer.print(value);
            }
        }
        printer.println();
    }

    printer.close();

    return sw.toString();
}
 
Example 10
Source File: CommandLineInterface.java    From utah-parser with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a list of headers, prints the headers, then prints the records for each header.
 * If a record doesn't exist for a header, a blank space is put in its place
 *
 * @param parser
 * @param target
 * @throws IOException
 */
void printToCSV(Parser parser, PrintStream target) throws IOException {
    Map<String, String> curr = parser.next();

    List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();

    Set<String> CSV_HEADERS = new TreeSet<>();
    // Get headers and create list
    while (curr != null) {
        mapList.add(curr);
        for (String str : curr.keySet()) {
            CSV_HEADERS.add(str);
        }
        curr = parser.next();
    }

    CSVFormat csvFormat = CSVFormat.DEFAULT;
    CSVPrinter printer = new CSVPrinter(target, csvFormat);
    printer.printRecord(CSV_HEADERS);

    // Cycle through array of maps printing value for each header
    for (Map<String, String> map : mapList) {
        List<String> values = new ArrayList<String>();
        for (String header : CSV_HEADERS) {
            String value = StringUtils.trimToEmpty(map.get(header));
            values.add(value);
        }
        printer.printRecord(values.toArray());
    }
}
 
Example 11
Source File: GeographyCode.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static void writeExternalCSVHeader(CSVPrinter printer) {
    try {
        printer.printRecord(externalNames());
    } catch (Exception e) {
        logger.debug(e.toString());
    }
}
 
Example 12
Source File: CSVUtils.java    From govpay with GNU General Public License v3.0 5 votes vote down vote up
public String toCsv(String ...strings) throws IOException {
	StringWriter writer = new StringWriter();
	CSVPrinter printer = new CSVPrinter(writer, csvFormat);
	printer.printRecord(Arrays.asList(strings));
	printer.flush();
	printer.close();
	
	String string = writer.toString();
	return string;
}
 
Example 13
Source File: Handler.java    From cloudsync with GNU General Public License v2.0 5 votes vote down vote up
private void writeStructureToCSVPrinter(final CSVPrinter out, final Item parentItem) throws IOException
{
	for (final Item child : parentItem.getChildren().values())
	{
		out.printRecord(Arrays.asList(child.toCSVArray()));
		if (child.isType(ItemType.FOLDER))
		{
			writeStructureToCSVPrinter(out, child);
		}
	}
}
 
Example 14
Source File: CSVExporter.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
@Override
public void write(Writer writer, List<Subject> subjects, List<Field> fields, Boolean timeStamp) throws IOException {
	this.timeStamp = null == timeStamp ? true : timeStamp;
	List<String> columnNames = getColumnNames(fields);

	CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT);
	printer.printRecord(columnNames);

	for (Subject subject : subjects) {
		printer.printRecord(
				tabulateSubjectMap(columnNames,
						flattenSubject(fields, subject)));
	}
}
 
Example 15
Source File: GetSubscriptionUsage.java    From development with Apache License 2.0 5 votes vote down vote up
private StringBuffer toCSV(VOSubscriptionUsageEntry entry) throws IOException {
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator());
    StringBuffer appendable = new StringBuffer();
    CSVPrinter csvFilePrinter = new CSVPrinter(appendable, csvFileFormat);
    List<String> columns = toList(entry);
    csvFilePrinter.printRecord(columns);
    csvFilePrinter.close();
    return appendable;
}
 
Example 16
Source File: EpidemicReport.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static void writeInternalCSVHeader(CSVPrinter printer) {
    try {
        printer.printRecord("DataSet", "Time", "Locationid",
                "Confirmed", "Healed", "Dead",
                "IncreasedConfirmed", "IncreasedHealed", "IncreasedDead",
                "DataSource");
    } catch (Exception e) {
        logger.debug(e.toString());
    }
}
 
Example 17
Source File: DataDstUECsv.java    From xresloader with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeConstData(CSVPrinter sp, Object data, String prefix) throws IOException {
    // null
    if (null == data) {
        sp.printRecord(prefix, "");
        return;
    }

    // 数字
    // 枚举值已被转为Java Long,会在这里执行
    if (data instanceof Number) {
        sp.printRecord(prefix, data);
        return;
    }

    // 布尔
    if (data instanceof Boolean) {
        sp.printRecord(prefix, ((Boolean) data) ? 1 : 0);
        return;
    }

    // 字符串&二进制
    if (data instanceof String) {
        sp.printRecord(prefix, data);
        return;
    }

    // 列表
    if (data instanceof List) {
        List<Object> ls = (List<Object>) data;
        for (int i = 0; i < ls.size(); ++i) {
            if (prefix.isEmpty()) {
                writeConstData(sp, ls.get(i), String.format("%d", i));
            } else {
                writeConstData(sp, ls.get(i), String.format("%s.%d", prefix, i));
            }
        }
        return;
    }

    // Hashmap
    if (data instanceof Map) {
        Map<String, Object> mp = (Map<String, Object>) data;
        ArrayList<Map.Entry<String, Object>> sorted_array = new ArrayList<Map.Entry<String, Object>>();
        sorted_array.ensureCapacity(mp.size());
        sorted_array.addAll(mp.entrySet());
        sorted_array.sort((l, r) -> {
            if (l.getValue() instanceof Integer && r.getValue() instanceof Integer) {
                return ((Integer) l.getValue()).compareTo((Integer) r.getValue());
            }

            return l.getKey().compareTo(r.getKey());
        });

        for (Map.Entry<String, Object> item : sorted_array) {
            if (prefix.isEmpty()) {
                writeConstData(sp, item.getValue(), String.format("%s", item.getKey()));
            } else {
                writeConstData(sp, item.getValue(), String.format("%s.%s", prefix, item.getKey()));
            }
        }
        return;
    }

    sp.printRecord(prefix, data.toString());
}
 
Example 18
Source File: RDBAccess.java    From rmlmapper-java with MIT License 4 votes vote down vote up
/**
 * This method creates an CSV-formatted InputStream from a Result Set.
 * @param rs the Result Set that is used.
 * @return a CSV-formatted InputStream.
 * @throws SQLException
 */
private InputStream getCSVInputStream(ResultSet rs) throws SQLException {
    // Get number of requested columns
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();

    boolean filledInDataTypes = false;
    StringWriter writer = new StringWriter();

    try {
        CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(getCSVHeader(rsmd, columnCount)));
        printer.printRecords();

        // Extract data from result set
        while (rs.next()) {
            String[] csvRow = new String[columnCount];

            // Iterate over column names
            for (int i = 1; i <= columnCount; i++) {
                String columnName = rsmd.getColumnName(i);

                if (!filledInDataTypes) {
                    String dataType = getColumnDataType(rsmd.getColumnTypeName(i));

                    if (dataType != null) {
                        datatypes.put(columnName, dataType);
                    }
                }

                // Add value to CSV row.
                csvRow[i - 1] = rs.getString(columnName);
            }

            // Add CSV row to CSVPrinter.
            // non-varargs call
            printer.printRecord((Object[]) csvRow);
            filledInDataTypes = true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Get InputStream from StringWriter.
    return new ByteArrayInputStream(writer.toString().getBytes());
}
 
Example 19
Source File: FileIndexerPipeline.java    From dataflow-opinion-analysis with Apache License 2.0 4 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
	
	ContentIndexSummary summary = c.element();

	if (summary.sentiments == null) 
		return;
	
	try {
	
		StringWriter stringWriter = new StringWriter();
		CSVPrinter csvPrinter = new CSVPrinter(stringWriter,CSVFormat.DEFAULT);
			
		for (int i=0; i < summary.sentiments.length; i++)
		{
			
			ArrayList<String> linefields = new ArrayList<String>();
			
			addField(linefields,"RecordID",summary.doc.collectionItemId);
	
			ArrayList<String> sttags = new ArrayList<>();
			if (summary.sentiments[i].tags != null) 
				for (int j=0; j < summary.sentiments[i].tags.length; j++)
					sttags.add(summary.sentiments[i].tags[j].tag);
			
			addField(linefields,"Tags",sttags.toString()); // will write as [a,b,c]
			
			addField(linefields,"SentimentHash", summary.sentiments[i].sentimentHash);
			addField(linefields,"Text", summary.sentiments[i].text);
			addField(linefields,"LabelledPositions", summary.sentiments[i].labelledPositions);
			addField(linefields,"AnnotatedText", summary.sentiments[i].annotatedText);
			addField(linefields,"AnnotatedHtml", summary.sentiments[i].annotatedHtmlText);
			addField(linefields,"SentimentTotalScore", summary.sentiments[i].sentimentTotalScore);
			addField(linefields,"DominantValence", summary.sentiments[i].dominantValence.ordinal());
			addField(linefields,"StAcceptance", summary.sentiments[i].stAcceptance);
			addField(linefields,"StAnger", summary.sentiments[i].stAnger);
			addField(linefields,"StAnticipation", summary.sentiments[i].stAnticipation);
			addField(linefields,"StAmbiguous", summary.sentiments[i].stAmbiguous);
			addField(linefields,"StDisgust", summary.sentiments[i].stDisgust);
			addField(linefields,"StFear", summary.sentiments[i].stFear);
			addField(linefields,"StGuilt", summary.sentiments[i].stGuilt);
			addField(linefields,"StInterest", summary.sentiments[i].stInterest);
			addField(linefields,"StJoy", summary.sentiments[i].stJoy);
			addField(linefields,"StSadness", summary.sentiments[i].stSadness);
			addField(linefields,"StShame", summary.sentiments[i].stShame);
			addField(linefields,"StSurprise", summary.sentiments[i].stSurprise);
			addField(linefields,"StPositive", summary.sentiments[i].stPositive);
			addField(linefields,"StNegative", summary.sentiments[i].stNegative);
			addField(linefields,"StSentiment", summary.sentiments[i].stSentiment);
			addField(linefields,"StProfane", summary.sentiments[i].stProfane);
			addField(linefields,"StUnsafe", summary.sentiments[i].stUnsafe);
			
			ArrayList<String> signalsarray = new ArrayList<>();
			if (summary.sentiments[i].signals != null) 
				for (int j=0; j < summary.sentiments[i].signals.length; j++)
					signalsarray.add(summary.sentiments[i].signals[j]);
			
			addField(linefields,"Signals",signalsarray.toString());
			
			csvPrinter.printRecord(linefields);
			
			String output = stringWriter.toString().trim(); // need to trim, because printRecord will add the record separator, as will the TextIO.write method at the end of the pipeline
			csvPrinter.flush(); // will also flush the stringWriter
			
			c.output(output);
			
			
		}
		
		csvPrinter.close();
	} catch (IOException e) {
		LOG.warn(e.getMessage());
	}
}
 
Example 20
Source File: Tool.java    From coming with MIT License 4 votes vote down vote up
private void genWeightsCSV() {
        String filePath = Support.getFilePath(Support.DirType.PARAMETER_DIR, option);
        String inputFilePath = filePath + "ParameterVector";
//        System.out.println("inputFilePath" + inputFilePath);
        String[] tmp = Support.getFilePath(Support.DirType.FEATURE_DIR, option).split("/");
        String prefix = tmp[tmp.length - 1];
        String outputFilePath = filePath + prefix + "feature-weights.csv";
//        System.out.println("outputFilePath" + outputFilePath);

        ParameterVector parameterVector = new ParameterVector(option.featureOption);
        parameterVector.load(inputFilePath);

        List<String> header = new ArrayList<>();
        List<String> values = new ArrayList<>();
        header.add("info");
        values.add("weights");
        for (int idx = 0; idx < parameterVector.size(); idx++) {
            FeatureCross featureCross;
            switch (option.featureOption) {
                case ORIGINAL:
                    featureCross = new OriginalFeatureCross(idx);
                    header.add(featureCross.getFeatures().toString());
                    break;
                case EXTENDED:
                    featureCross = new ExtendedFeatureCross(idx);
                    header.add(featureCross.getFeatures().toString());
                    break;
                default:
                    System.out.println("Out of Expectation");
                    break;
            }
            values.add(String.valueOf(parameterVector.get(idx)));
        }

        try {
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputFilePath));
            CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(header.toArray(new String[0])));
            // only one line of data
            csvPrinter.printRecord(values);
            csvPrinter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }