Java Examples for org.apache.commons.csv.CSVPrinter
The following java examples will help you to understand the usage of org.apache.commons.csv.CSVPrinter. These source code samples are taken from different open source projects.
Example 1
| Project: Archimulator-master File: CSVHelper.java View source code |
/**
* To CSV.
*
* @param outputCSVFileName the output CSV file name
* @param results the list of results
* @param fields the list of fields
* @param <ResultT> the result type
*/
public static <ResultT> void toCsv(String outputCSVFileName, List<ResultT> results, List<CSVField<ResultT>> fields) {
File resultDirFile = new File(outputCSVFileName).getParentFile();
if (!resultDirFile.exists()) {
if (!resultDirFile.mkdirs()) {
throw new RuntimeException();
}
}
CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',').withQuoteMode(QuoteMode.ALL).withQuote('"');
try {
FileWriter writer = new FileWriter(outputCSVFileName);
CSVPrinter printer = new CSVPrinter(writer, format);
printer.printRecord(fields);
for (ResultT result : results) {
List<String> record = new ArrayList<>();
for (CSVField<ResultT> field : fields) {
record.add(field.getFunc().apply(result));
}
printer.printRecord(record);
}
printer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}Example 2
| Project: jglor-master File: CSVFileExporter.java View source code |
public void write(List<Patient> patients, String file) {
CSVFormat csvFileFormat = CSVFormat.EXCEL;
try {
StringBuilder builder = new StringBuilder();
CSVPrinter csvPrinter = new CSVPrinter(builder, csvFileFormat);
csvPrinter.printRecord(PatientCSVConverter.FILE_HEADER);
for (Patient patient : patients) {
csvPrinter.printRecord(new PatientCSVConverter().convert(patient));
}
FileWriter writer = new FileWriter(file);
writer.write(builder.toString());
writer.flush();
} catch (IOException e) {
throw new RuntimeException("Cannot generate csv file", e);
}
}Example 3
| Project: sw360portal-master File: ComponentUploadPortlet.java View source code |
@NotNull
private ByteArrayOutputStream writeCsvStream(List<List<String>> listList) throws TException, IOException {
final ByteArrayOutputStream riskCategoryCsvStream = new ByteArrayOutputStream();
Writer out = new BufferedWriter(new OutputStreamWriter(riskCategoryCsvStream));
CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat);
csvPrinter.printRecords(listList);
csvPrinter.flush();
csvPrinter.close();
return riskCategoryCsvStream;
}Example 4
| Project: ambari-master File: JobService.java View source code |
@Override
public void write(OutputStream os) throws IOException, WebApplicationException {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
try {
List<ColumnDescription> descriptions = resultSet.getDescriptions();
List<String> headers = Lists.newArrayList();
for (ColumnDescription description : descriptions) {
headers.add(description.getName());
}
csvPrinter.printRecord(headers.toArray());
while (resultSet.hasNext()) {
csvPrinter.printRecord(resultSet.next().getRow());
writer.flush();
}
} finally {
writer.close();
}
}Example 5
| Project: mathosphere-master File: FlinkMlpRelationFinder.java View source code |
public void writeRelevanceTemplates(String qId, List<Relation> relations) throws IOException {
if (config.getOutputDir() != null) {
final File output = new File(config.getOutputDir() + "/q" + qId + ".csv");
output.createNewFile();
OutputStreamWriter w = new FileWriter(output);
CSVPrinter printer = CSVFormat.DEFAULT.withRecordSeparator("\n").print(w);
for (Relation relation : relations) {
String sScore;
if (relation.getRelevance() == null) {
sScore = "";
} else {
sScore = String.valueOf(relation.getRelevance());
}
String[] out = new String[] { relation.getIdentifier(), relation.getDefinition(), sScore };
printer.printRecord(out);
}
w.flush();
w.close();
}
}Example 6
| Project: roda-master File: InventoryReportPlugin.java View source code |
@Override
public Report execute(IndexService index, ModelService model, StorageService storage, List<LiteOptionalWithCause> liteList) throws PluginException {
final CSVPrinter csvFilePrinter = createCSVPrinter();
return PluginHelper.processObjects(this, new RODAObjectProcessingLogic<AIP>() {
@Override
public void process(IndexService index, ModelService model, StorageService storage, Report report, Job cachedJob, SimpleJobPluginInfo jobPluginInfo, Plugin<AIP> plugin, AIP object) {
processAIP(model, storage, jobPluginInfo, csvFilePrinter, object);
}
}, new RODAProcessingLogic<AIP>() {
@Override
public void process(IndexService index, ModelService model, StorageService storage, Report report, Job cachedJob, SimpleJobPluginInfo jobPluginInfo, Plugin<AIP> plugin) {
IOUtils.closeQuietly(csvFilePrinter);
}
}, index, model, storage, liteList);
}Example 7
| Project: stocks-master File: PerformanceIndex.java View source code |
private void exportTo(File file, Predicate<Integer> filter) throws IOException {
CSVStrategy strategy = new CSVStrategy(';', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, false);
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
CSVPrinter printer = new CSVPrinter(writer);
printer.setStrategy(strategy);
printer.println(new String[] { //
Messages.CSVColumn_Date, //
Messages.CSVColumn_Value, //
Messages.CSVColumn_Transferals, //
Messages.CSVColumn_DeltaInPercent, Messages.CSVColumn_CumulatedPerformanceInPercent });
for (int ii = 0; ii < totals.length; ii++) {
if (!filter.test(ii))
continue;
printer.print(dates[ii].toString());
printer.print(Values.Amount.format(totals[ii]));
printer.print(Values.Amount.format(transferals[ii]));
printer.print(Values.Percent.format(delta[ii]));
printer.print(Values.Percent.format(accumulated[ii]));
printer.println();
}
}
}Example 8
| Project: tabula-java-master File: UtilsForTesting.java View source code |
public static String loadCsv(String path) throws IOException {
StringBuilder out = new StringBuilder();
CSVParser parse = org.apache.commons.csv.CSVParser.parse(new File(path), Charset.forName("utf-8"), CSVFormat.EXCEL);
CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL);
printer.printRecords(parse);
printer.close();
String csv = out.toString().replaceAll("(?<!\r)\n", "\r");
return csv;
}Example 9
| Project: ApkCategoryChecker-master File: WriterCSV.java View source code |
@Override
public void Write(AnalyzerResult result, String _csvPath, int _counter) {
try {
ArrayList<AnalyzerResult> resultList = new ArrayList<AnalyzerResult>();
resultList.add(result);
/*--Create the CSVFormat object--*/
CSVFormat format = CSVFormat.DEFAULT.withHeader();
/*--Writing in a CSV file--*/
FileWriter _out = new FileWriter(_csvPath, true);
CSVPrinter printer;
printer = new CSVPrinter(_out, format.withDelimiter('#'));
/*--Retrieve APKResult and Write in file--*/
Iterator<AnalyzerResult> it = resultList.iterator();
while (it.hasNext()) {
AnalyzerResult _resultElement = it.next();
List<String> resultData = new ArrayList<>();
resultData.add(String.valueOf(_counter));
resultData.add(_resultElement.get_APKName());
resultData.add(_resultElement.get_APKPath());
resultData.add(_resultElement.get_Package());
resultData.add(_resultElement.get_APKMainFramework());
resultData.add(_resultElement.get_APKBaseFramework());
resultData.add(String.valueOf(_resultElement.get_html()));
resultData.add(String.valueOf(_resultElement.get_js()));
resultData.add(String.valueOf(_resultElement.get_css()));
resultData.add(_resultElement.get_debuggable());
resultData.add(_resultElement.get_permissions());
resultData.add(_resultElement.get_minSdkVersion());
resultData.add(_resultElement.get_maxSdkVersion());
resultData.add(_resultElement.get_targetSdkVersion());
resultData.add(_resultElement.get_fileSize());
resultData.add(String.valueOf(_resultElement.get_startAnalysis()));
resultData.add(String.valueOf(_resultElement.get_durationAnalysis()));
resultData.add(String.valueOf(_resultElement.get_decodeSuccess()));
printer.printRecord(resultData);
}
/*--Close the printer--*/
printer.close();
System.out.println("Record added to CSV file");
this.removeBlankLines(_csvPath);
} catch (IOException ex) {
Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
}
}Example 10
| Project: kempes-master File: EvomagEventHandler.java View source code |
private void writeToCsv(Product product) throws IOException {
// TODO improve
if (csvPrinter == null) {
CSVFormat csvFormat = CSVFormat.RFC4180.withHeader().withDelimiter(',');
csvPrinter = new CSVPrinter(new FileWriter(CSV_FILE), csvFormat.withDelimiter('#'));
csvPrinter.printRecord("Name", "Price");
}
List<String> data = new ArrayList<String>();
data.add(product.getName());
data.add(String.valueOf(product.getPrice()));
csvPrinter.printRecord(data);
csvPrinter.flush();
// TODO
// csvPrinter.close();
}Example 11
| Project: keycloak-master File: PerformanceMeasurement.java View source code |
public void printToCSV(String testName) {
checkStatisticsNotNull();
for (String statistic : statistics.keySet()) {
File csvFile = new File(PROJECT_BUILD_DIRECTORY + "/measurements" + (testName == null ? "" : "/" + testName), statistic + ".csv");
boolean csvFileCreated = false;
if (!csvFile.exists()) {
try {
csvFile.getParentFile().mkdirs();
csvFileCreated = csvFile.createNewFile();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile, true))) {
CSVPrinter printer = new CSVPrinter(writer, CSVFormat.RFC4180);
if (csvFileCreated) {
printer.printRecord(HEADER);
}
printer.printRecord(toRecord(statistic));
printer.flush();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}Example 12
| Project: LiquidDonkey-master File: CSVWriter.java View source code |
public void files(Collection<ICloud.MBSFile> files, Path path) throws IOException {
logger.trace("<< write() < files: {} path: {}", files.size(), path);
Files.createDirectories(path.getParent());
try (CSVPrinter printer = new CSVPrinter(new FileWriter(path.toFile()), csvFormat)) {
printer.printRecord(HEADER);
for (ICloud.MBSFile file : files) {
String mode = file.getAttributes().hasMode() ? "0x" + Integer.toString(file.getAttributes().getMode(), 16) : "";
String size = file.hasSize() ? Long.toString(file.getSize()) : "";
String lastModified = file.getAttributes().hasLastModified() ? Long.toString(file.getAttributes().getLastModified()) : "";
printer.print(mode);
printer.print(size);
printer.print(lastModified);
printer.print(file.getDomain());
printer.print(file.getRelativePath());
printer.println();
}
}
logger.trace(">> write()");
}Example 13
| Project: rakam-master File: ExportUtil.java View source code |
public static byte[] exportAsCSV(QueryResult result) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final CSVPrinter csvPrinter;
try {
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC);
csvPrinter = new CSVPrinter(new PrintWriter(out), format);
csvPrinter.printRecord(result.getMetadata().stream().map(SchemaField::getName).collect(Collectors.toList()));
csvPrinter.printRecords(Iterables.transform(result.getResult(), input -> Iterables.transform(input, input1 -> {
if (input1 instanceof List || input1 instanceof Map) {
return JsonHelper.encode(input1);
}
if (input1 instanceof byte[]) {
return DatatypeConverter.printBase64Binary((byte[]) input1);
}
return input1;
})));
csvPrinter.flush();
} catch (IOException e) {
throw Throwables.propagate(e);
}
return out.toByteArray();
}Example 14
| Project: rce-master File: OptimizerComponent.java View source code |
private void writeResultToCSVFile(String path) throws IOException {
if (path != null && !iterationData.isEmpty()) {
List<String> orderedOutputs = new LinkedList<>(output);
List<String> orderedInputs = new LinkedList<>(input);
List<String> insert = new LinkedList<>();
List<String> remove = new LinkedList<>();
for (String outputName : orderedOutputs) {
if (componentContext.getOutputDataType(outputName) == DataType.Vector) {
remove.add(outputName);
for (int i = 0; i < Integer.parseInt(componentContext.getOutputMetaDataValue(outputName, OptimizerComponentConstants.METADATA_VECTOR_SIZE)); i++) {
insert.add(outputName + OptimizerComponentConstants.OPTIMIZER_VECTOR_INDEX_SYMBOL + i);
}
}
}
for (String toRemove : remove) {
orderedOutputs.remove(toRemove);
}
for (String toInsert : insert) {
orderedOutputs.add(toInsert);
}
insert = new LinkedList<>();
remove = new LinkedList<>();
for (String inputName : orderedInputs) {
if (componentContext.getInputDataType(inputName) == DataType.Vector) {
int vectorSize = 0;
if (inputName.contains(OptimizerComponentConstants.GRADIENT_DELTA)) {
String outputName = inputName.substring(inputName.lastIndexOf(OptimizerComponentConstants.GRADIENT_DELTA) + 1);
vectorSize = Integer.parseInt(componentContext.getOutputMetaDataValue(outputName, OptimizerComponentConstants.METADATA_VECTOR_SIZE));
} else {
vectorSize = Integer.parseInt(componentContext.getInputMetaDataValue(inputName, OptimizerComponentConstants.METADATA_VECTOR_SIZE));
}
remove.add(inputName);
for (int i = 0; i < vectorSize; i++) {
insert.add(inputName + OptimizerComponentConstants.OPTIMIZER_VECTOR_INDEX_SYMBOL + i);
}
}
}
for (String toRemove : remove) {
orderedInputs.remove(toRemove);
}
for (String toInsert : insert) {
orderedInputs.add(toInsert);
}
Collections.sort(orderedOutputs);
Collections.sort(orderedInputs);
try (FileWriter fw = new FileWriter(new File(path));
CSVPrinter printer = CSVFormat.newFormat(';').withIgnoreSurroundingSpaces().withAllowMissingColumnNames().withRecordSeparator("\n").print(fw)) {
printer.print(ITERATION);
for (String outputName : orderedOutputs) {
printer.print(outputName);
}
for (String inputName : orderedInputs) {
printer.print(inputName);
}
printer.println();
// Iteration start at 1.
for (Integer i = 1; i < iterationData.keySet().size() + 1; i++) {
Map<String, Double> iteration = iterationData.get(i);
printer.print(i);
for (String out : orderedOutputs) {
printer.print(iteration.get(out));
}
for (String in : orderedInputs) {
printer.print(iteration.get(INPUT_PREFIX_CONSTANT + in));
}
printer.println();
printer.flush();
}
}
}
}Example 15
| Project: camel-master File: CsvMarshaller.java View source code |
/**
* Marshals the given object into the given stream.
*
* @param exchange Exchange (used for access to type conversion)
* @param object Body to marshal
* @param outputStream Output stream of the CSV
* @throws NoTypeConversionAvailableException if the body cannot be converted
* @throws IOException if we cannot write into the given stream
*/
public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws NoTypeConversionAvailableException, IOException {
CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(outputStream, IOHelper.getCharsetName(exchange)), format);
try {
Iterator it = ObjectHelper.createIterator(object);
while (it.hasNext()) {
Object child = it.next();
printer.printRecord(getRecordValues(exchange, child));
}
} finally {
IOHelper.close(printer);
}
}Example 16
| Project: easybatch-framework-master File: ApacheCommonCsvRecordMarshaller.java View source code |
@Override
public StringRecord processRecord(final Record<P> record) throws Exception {
StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
Iterable<Object> iterable = fieldExtractor.extractFields(record.getPayload());
csvPrinter.printRecord(iterable);
csvPrinter.flush();
csvPrinter.close();
return new StringRecord(record.getHeader(), stringWriter.toString());
}Example 17
| Project: migration-tools-master File: CsvOutput.java View source code |
@Override
protected void init(Writer writer) {
CsvFormatBuilder builder = new CsvFormatBuilder(this);
CSVFormat format = builder.build();
doubleQuote = valueOf(builder.getQuote()) + valueOf(builder.getQuote());
try {
csvPrinter = new CSVPrinter(wrapWriter(writer), format);
} catch (IOException exception) {
throw new OutputException(exception);
}
}Example 18
| Project: alma-toolkit-master File: TaskFetchReport.java View source code |
@Override
public void run() {
log.info("executing task: {}", this.getClass().getSimpleName());
// ensure we have a proper number.
try {
Integer.parseInt(limit);
} catch (NumberFormatException nfe) {
log.error("option specified is not an integer: {}", limit, nfe);
return;
}
if (title == null) {
log.error("title required");
return;
}
if (csv == null) {
log.error("csv required");
return;
}
// get an instance of the web client.
WebTarget target = webTargetProvider.get();
// the 'path' specifies the resource we are targeting e.g. 'users'.
// this will result in: https://api-ap.hosted.exlibrisgroup.com/almaws/v1/users
// we are also specifying some parameters: limit and offset (apikey preconfigured)
// this results in: https://api-ap.hosted.exlibrisgroup.com/almaws/v1/users?apikey=xxx&limit=100&offset=0
String path = title;
// path = "/users/1024839030002171_2171_d/User Analysis/Loan Data";
String filter = getFilter(new Date(), new Date());
log.debug("filter:\n{}", filter);
target = target.path("analytics/reports").queryParam("limit", limit).queryParam("path", path).queryParam("filter", filter);
try (CSVPrinter printer = new CSVPrinter(new FileWriter(csv), CSVFormat.EXCEL)) {
String token = null;
boolean finished = false;
while (!finished) {
if (token != null)
target = target.queryParam("token", token);
// we avoid dealing with any XML/JSON. We just make the call.
ReportType report = target.request(MediaType.APPLICATION_XML_TYPE).get(ReportType.class);
QueryResultType result = report.getQueryResult();
finished = result.isIsFinished();
token = result.getResumptionToken();
Element resultXml = (Element) result.getAny();
NodeList rows = resultXml.getElementsByTagName("Row");
if (rows.getLength() == 0) {
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
log.debug("sleep interrupted.");
}
}
for (int x = 0; x < rows.getLength(); x++) {
List<String> csvRow = new ArrayList<String>();
Element row = (Element) rows.item(x);
NodeList cols = row.getChildNodes();
for (int y = 0; y < cols.getLength(); y++) {
Node node = cols.item(y);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element col = (Element) cols.item(y);
csvRow.add(col.getFirstChild().getNodeValue());
log.debug("column: {}", col.getNodeName());
}
}
printer.printRecord(csvRow);
}
}
printer.flush();
printer.close();
} catch (IOException ioe) {
log.error("unable to write records to csv file: {}", csv, ioe);
}
}Example 19
| Project: aws-big-data-blog-master File: LambdaContainer.java View source code |
//Validation/Conversion Layer function
public void validateAndNormalizeInputData(S3Event event, Context ctx) throws Exception {
AmazonS3 s3Client;
InputStream inputFileStream = null;
InputStream readableDataStream = null;
List<S3EventNotificationRecord> notificationRecords = event.getRecords();
s3Client = new AmazonS3Client();
String eventFileName, siteName, dbfName;
CSVParser fileParser = null;
for (S3EventNotificationRecord record : notificationRecords) {
eventFileName = record.getS3().getObject().getKey();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(record.getS3().getBucket().getName(), record.getS3().getObject().getKey()));
inputFileStream = s3Object.getObjectContent();
fileParser = new CSVParser(new InputStreamReader(inputFileStream), CSVFormat.TDF.withCommentMarker('-'));
List<CSVRecord> records = fileParser.getRecords();
StringWriter writer = new StringWriter();
CSVPrinter printer = null;
if (records.get(0).toString().matches(".*[^0-9].*")) {
records.remove(0);
}
printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator")));
printer.printRecords(records);
printer.flush();
readableDataStream = new ByteArrayInputStream(writer.toString().getBytes("utf-8"));
s3Client.putObject(record.getS3().getBucket().getName(), "validated/" + eventFileName + ".csv", readableDataStream, new ObjectMetadata());
printer.close();
readableDataStream.close();
}
}Example 20
| Project: Carolina-Digital-Repository-master File: PerformanceMonitorController.java View source code |
/**
* Gets totals for metrics that are only aggregated daily such as moves & enhancements
* It also grabs throughput totals for older data, before these fields were set to measure performance at the individual deposit level
* @return
*/
public String getOperationsData() {
String filePath = dataPath + "ingest-times-daily.csv";
if (buildFile(filePath)) {
Set<String> deposits = getDepositMetrics();
try (Jedis jedis = getJedisPool().getResource()) {
FileWriter fileWriter = null;
Set<String> operations = null;
Map<String, String> depositJob = null;
Map<String, String> operationJob = null;
String[] depositKeys = null;
operations = jedis.keys("operation-metrics:*");
fileWriter = new FileWriter(filePath);
try (CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat)) {
csvFilePrinter.printRecord(FILE_HEADERS);
Boolean matchingDate = false;
for (String deposit : deposits) {
depositKeys = deposit.split(":");
// Ignore data for individual deposits by uuid. Only need the daily ones in this instance
if (depositKeys.length > 2) {
continue;
}
depositJob = jedis.hgetAll(deposit);
String jobDate = depositKeys[1];
String throughputFiles = depositJob.get("throughput-files");
String throughputBytes = depositJob.get("throughput-bytes");
String finished = depositJob.get("finished");
String failed = depositJob.get("failed");
String failedDepositJob = depositJob.get("failed-job:edu.unc.lib.dl.cdr.services.techmd.TechnicalMetadataEnhancementService");
for (String operation : operations) {
String operationDate = operation.split(":")[1];
if (operationDate.equals(jobDate)) {
operationJob = jedis.hgetAll(operation);
List<String> data = new ArrayList<>();
data.add(jobDate);
data.add("N/A");
data.add(throughputFiles);
data.add(throughputBytes);
data.add("0");
data.add("0");
data.add(finished);
for (String field : MOVES_ENHANCEMENTS_JOBS_ARRAY) {
String fieldValue = operationJob.get(field);
data.add(fieldValue);
}
data.add(failed);
data.add(failedDepositJob);
csvFilePrinter.printRecord(data);
matchingDate = true;
break;
} else {
matchingDate = false;
}
}
if (!matchingDate) {
List<String> data = new ArrayList<>();
data.add(jobDate);
data.add("N/A");
data.add(throughputFiles);
data.add(throughputBytes);
data.add("0");
data.add("0");
data.add(finished);
this.addEmptyFields(data, MOVES_ENHANCEMENTS_JOBS_ARRAY);
data.add(failed);
data.add(failedDepositJob);
csvFilePrinter.printRecord(data);
}
}
}
} catch (Exception e) {
log.error("Failed to write data to {}", filePath, e);
}
}
try {
return FileUtils.readFileToString(new File(filePath));
} catch (IOException e) {
log.error("Error unable to read file to string from filepath {}", filePath, e);
return null;
}
}Example 21
| Project: data-quality-master File: SemanticQualityAnalyzerPerformanceTest.java View source code |
// To generate a bigger validation_big_file.csv if necessary
public static void main(String[] args) {
try {
final String resourcePath = SemanticDictionaryGenerator.class.getResource(".").getFile();
final String projectRoot = new File(resourcePath).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getPath() + File.separator;
File f = new File(projectRoot + BIG_FILE_PATH);
CSVPrinter writer = new CSVPrinter(new FileWriter(f), CSVFormat.DEFAULT.withDelimiter(';'));
List<String[]> records = new ArrayList<>();
Random randomGenerator = new Random();
for (int i = 0; i < RECORD_LINES_NUMBER; i++) {
records.add(new String[EXPECTED_CATEGORIES_DICT.length]);
}
for (int j = 0; j < EXPECTED_CATEGORIES_DICT.length; j++) {
List<String> file = getFile(EXPECTED_CATEGORIES_DICT[j]);
for (int i = 0; i < RECORD_LINES_NUMBER; i++) {
records.get(i)[j] = file.get(randomGenerator.nextInt(file.size()));
}
}
for (String[] record : records) writer.printRecord(record);
} catch (IOException e) {
e.printStackTrace();
}
}Example 22
| Project: geosummly-master File: CSVDataIO.java View source code |
/**
* Print result of transformation matrix to csv file.
*/
public void printResultHorizontal(ArrayList<Long> timestamps, ArrayList<ArrayList<Double>> matrix, ArrayList<String> features, String directoryName, String fileName) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(bout);
try {
CSVPrinter csv = new CSVPrinter(osw, CSVFormat.DEFAULT);
//print the header of the matrix
for (String f : features) {
csv.print(f);
}
csv.println();
//iterate per each row of the matrix
for (int i = 0; i < matrix.size(); i++) {
if (timestamps != null)
csv.print(timestamps.get(0));
for (int j = 0; j < matrix.get(i).size(); j++) {
csv.print(matrix.get(i).get(j));
}
csv.println();
}
csv.flush();
csv.close();
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream outputStream;
try {
//create the output directory if it doesn't exist
File dir = new File(directoryName);
dir.mkdirs();
outputStream = new FileOutputStream(dir.getPath().concat(fileName));
bout.writeTo(outputStream);
bout.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}Example 23
| Project: gephi-master File: ExporterSpreadsheet.java View source code |
private void exportData(Graph graph) throws Exception {
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(fieldDelimiter);
try (CSVPrinter csvWriter = new CSVPrinter(writer, format)) {
boolean isEdgeTable = tableToExport != ExportTable.NODES;
Table table = isEdgeTable ? graph.getModel().getEdgeTable() : graph.getModel().getNodeTable();
ElementIterable<? extends Element> rows;
Object[] edgeLabels = graph.getModel().getEdgeTypeLabels();
boolean includeEdgeKindColumn = false;
for (Object edgeLabel : edgeLabels) {
if (edgeLabel != null && !edgeLabel.toString().isEmpty()) {
includeEdgeKindColumn = true;
}
}
TimeFormat timeFormat = graph.getModel().getTimeFormat();
DateTimeZone timeZone = graph.getModel().getTimeZone();
List<Column> columns = new ArrayList<>();
if (columnIdsToExport != null) {
for (String columnId : columnIdsToExport) {
Column column = table.getColumn(columnId);
if (column != null) {
columns.add(column);
}
}
} else {
for (Column column : table) {
columns.add(column);
}
}
//Write column headers:
if (isEdgeTable) {
csvWriter.print("Source");
csvWriter.print("Target");
csvWriter.print("Type");
if (includeEdgeKindColumn) {
csvWriter.print("Kind");
}
}
for (Column column : columns) {
//Use the title only if it's the same as the id (case insensitive):
String columnId = column.getId();
String columnTitle = column.getTitle();
String columnHeader = columnId.equalsIgnoreCase(columnTitle) ? columnTitle : columnId;
csvWriter.print(columnHeader);
}
csvWriter.println();
//Write rows:
if (isEdgeTable) {
rows = graph.getEdges();
} else {
rows = graph.getNodes();
}
for (Element row : rows) {
if (isEdgeTable) {
Edge edge = (Edge) row;
csvWriter.print(edge.getSource().getId());
csvWriter.print(edge.getTarget().getId());
csvWriter.print(edge.isDirected() ? "Directed" : "Undirected");
if (includeEdgeKindColumn) {
csvWriter.print(edge.getTypeLabel().toString());
}
}
for (Column column : columns) {
Object value = row.getAttribute(column);
String text;
if (value != null) {
if (value instanceof Number) {
text = NUMBER_FORMAT.format(value);
} else {
text = AttributeUtils.print(value, timeFormat, timeZone);
}
} else {
text = "";
}
csvWriter.print(text);
}
csvWriter.println();
}
}
}Example 24
| Project: pippo-master File: CsvEngine.java View source code |
public String toCsv(Csv... records) {
if (records != null && records.length > 0) {
String[] header = records[0].getCsvHeader();
StringWriter writer = new StringWriter();
try (CSVPrinter printer = getCSVFormat().withHeader(header).print(writer)) {
for (Csv record : records) {
Object[] data = record.getCsvData();
if (data == null || data.length == 0) {
log.debug("Skipping null or empty record");
continue;
}
for (Object column : data) {
printer.print(objectToString(column));
}
printer.println();
}
} catch (IOException e) {
log.error("Failed to generate CSV", e);
}
return writer.toString();
}
return null;
}Example 25
| Project: pwm-master File: StatisticsManager.java View source code |
public int outputStatsToCsv(final OutputStream outputStream, final Locale locale, final boolean includeHeader) throws IOException {
LOGGER.trace("beginning output stats to csv process");
final Instant startTime = Instant.now();
final StatisticsManager statsManger = pwmApplication.getStatisticsManager();
final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(outputStream);
if (includeHeader) {
final List<String> headers = new ArrayList<>();
headers.add("KEY");
headers.add("YEAR");
headers.add("DAY");
for (final Statistic stat : Statistic.values()) {
headers.add(stat.getLabel(locale));
}
csvPrinter.printRecord(headers);
}
int counter = 0;
final Map<StatisticsManager.DailyKey, String> keys = statsManger.getAvailableKeys(PwmConstants.DEFAULT_LOCALE);
for (final StatisticsManager.DailyKey loopKey : keys.keySet()) {
counter++;
final StatisticsBundle bundle = statsManger.getStatBundleForKey(loopKey.toString());
final List<String> lineOutput = new ArrayList<>();
lineOutput.add(loopKey.toString());
lineOutput.add(String.valueOf(loopKey.year));
lineOutput.add(String.valueOf(loopKey.day));
for (final Statistic stat : Statistic.values()) {
lineOutput.add(bundle.getStatistic(stat));
}
csvPrinter.printRecord(lineOutput);
}
csvPrinter.flush();
LOGGER.trace("completed output stats to csv process; output " + counter + " records in " + TimeDuration.fromCurrent(startTime).asCompactString());
return counter;
}Example 26
| Project: TundraCSV.java-master File: IDataCSVParser.java View source code |
/**
* Returns a CSV representation of the given IData object.
*
* @param document The IData to convert to CSV.
* @return The CSV representation of the IData.
*/
@Override
public String encodeToString(IData document) throws IOException {
if (document == null)
return null;
IDataCursor cursor = document.getCursor();
IData[] records = IDataUtil.getIDataArray(cursor, "recordWithNoID");
cursor.destroy();
if (records == null)
return null;
if (records.length == 0)
return "";
StringBuilder builder = new StringBuilder();
CSVFormat format = CSVFormat.DEFAULT.withHeader(IDataHelper.getKeys(records)).withDelimiter(delimiter).withNullString("");
CSVPrinter printer = new CSVPrinter(builder, format);
for (IData record : records) {
if (record != null)
printer.printRecord(IDataHelper.getValues(record));
}
return builder.toString();
}Example 27
| Project: Viz-master File: ExporterSpreadsheet.java View source code |
private void exportData(Graph graph) throws Exception {
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(fieldDelimiter);
try (CSVPrinter csvWriter = new CSVPrinter(writer, format)) {
boolean isEdgeTable = tableToExport != ExportTable.NODES;
Table table = isEdgeTable ? graph.getModel().getEdgeTable() : graph.getModel().getNodeTable();
ElementIterable<? extends Element> rows;
Object[] edgeLabels = graph.getModel().getEdgeTypeLabels();
boolean includeEdgeKindColumn = false;
for (Object edgeLabel : edgeLabels) {
if (edgeLabel != null && !edgeLabel.toString().isEmpty()) {
includeEdgeKindColumn = true;
}
}
TimeFormat timeFormat = graph.getModel().getTimeFormat();
DateTimeZone timeZone = graph.getModel().getTimeZone();
List<Column> columns = new ArrayList<>();
if (columnIdsToExport != null) {
for (String columnId : columnIdsToExport) {
Column column = table.getColumn(columnId);
if (column != null) {
columns.add(column);
}
}
} else {
for (Column column : table) {
columns.add(column);
}
}
//Write column headers:
if (isEdgeTable) {
csvWriter.print("Source");
csvWriter.print("Target");
csvWriter.print("Type");
if (includeEdgeKindColumn) {
csvWriter.print("Kind");
}
}
for (Column column : columns) {
//Use the title only if it's the same as the id (case insensitive):
String columnId = column.getId();
String columnTitle = column.getTitle();
String columnHeader = columnId.equalsIgnoreCase(columnTitle) ? columnTitle : columnId;
csvWriter.print(columnHeader);
}
csvWriter.println();
//Write rows:
if (isEdgeTable) {
rows = graph.getEdges();
} else {
rows = graph.getNodes();
}
for (Element row : rows) {
if (isEdgeTable) {
Edge edge = (Edge) row;
csvWriter.print(edge.getSource().getId());
csvWriter.print(edge.getTarget().getId());
csvWriter.print(edge.isDirected() ? "Directed" : "Undirected");
if (includeEdgeKindColumn) {
csvWriter.print(edge.getTypeLabel().toString());
}
}
for (Column column : columns) {
Object value = row.getAttribute(column);
String text;
if (value != null) {
if (value instanceof Number) {
text = NUMBER_FORMAT.format(value);
} else {
text = AttributeUtils.print(value, timeFormat, timeZone);
}
} else {
text = "";
}
csvWriter.print(text);
}
csvWriter.println();
}
}
}Example 28
| Project: ServalMaps-master File: CsvAsyncTask.java View source code |
private Integer doLocationExport() {
if (V_LOG) {
Log.v(TAG, "doLocationExport called: ");
}
progressBar.setProgress(0);
Integer mRecordCount = 0;
updateUI = true;
updateForLocation = true;
ContentResolver mContentResolver = context.getApplicationContext().getContentResolver();
Cursor mCursor = mContentResolver.query(LocationsContract.CONTENT_URI, null, null, null, null);
if (mCursor.getCount() > 0) {
progressBar.setMax(mCursor.getCount());
mRecordCount = mCursor.getCount();
String mOutputPath = Environment.getExternalStorageDirectory().getPath();
mOutputPath += context.getString(R.string.system_path_export_data);
if (FileUtils.isDirectoryWritable(mOutputPath) == false) {
Log.e(TAG, "unable to access the required output directory");
mCursor.close();
return 0;
}
String mFileName = "serval-maps-export-locations-" + TimeUtils.getToday() + ".csv";
BufferedWriter mOutput = null;
String[] mLine = new String[LocationsContract.Table.COLUMNS.length];
try {
mOutput = new BufferedWriter(new FileWriter(mOutputPath + mFileName, false));
CSVPrinter mPrinter = new CSVPrinter(mOutput, csvFormat);
mPrinter.printComment("Location data sourced from the Serval Maps application");
mPrinter.printComment("File created: " + TimeUtils.getToday());
mPrinter.printComment(Arrays.toString(LocationsContract.Table.COLUMNS));
while (mCursor.moveToNext()) {
for (int i = 0; i < LocationsContract.Table.COLUMNS.length; i++) {
mLine[i] = mCursor.getString(mCursor.getColumnIndex(LocationsContract.Table.COLUMNS[i]));
}
mPrinter.println(mLine);
publishProgress(mCursor.getPosition());
if (isCancelled() == true) {
break;
}
}
} catch (FileNotFoundException e) {
Log.e(TAG, "unable to open the output file", e);
} catch (IOException e) {
Log.e(TAG, "unable to write the message at '" + mCursor.getPosition() + "' in the cursor", e);
} finally {
try {
if (mOutput != null) {
mOutput.close();
}
} catch (IOException e) {
Log.e(TAG, "unable to close the output file", e);
}
mCursor.close();
}
}
return mRecordCount;
}Example 29
| Project: vitam-master File: JsonTransformer.java View source code |
/**
* Generates execution time by step relative to a logbook operation
*
* @param logbookOperation
* @return CSV report
* @throws VitamException
* @throws IOException
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static ByteArrayOutputStream buildLogbookStatCsvFile(JsonNode logbookOperation) throws VitamException, IOException {
final ByteArrayOutputStream csvOutputStream = new ByteArrayOutputStream();
try (Writer csvWriter = new BufferedWriter(new OutputStreamWriter(csvOutputStream))) {
// Total execution time
final String startOperationTimeStr = logbookOperation.get(EVENT_DATE_TIME_FIELD).asText();
final List<JsonNode> events = IteratorUtils.toList(logbookOperation.get(EVENTS_FIELD).iterator());
// Last event
final JsonNode lastEvent = events.get(events.size() - 1);
final String endOperationTimeStr = lastEvent.get(EVENT_DATE_TIME_FIELD).asText();
// Generate CSV report
final CSVPrinter csvPrinter = new CSVPrinter(csvWriter, CSVFormat.newFormat(SEMI_COLON_SEPARATOR).withRecordSeparator(RECORD_SEPARATOR));
final List<String> header = IteratorUtils.toList(lastEvent.fieldNames());
header.add(START_EVENT_DATETIME_HEADER);
header.add(END_EVENT_DATETIME_HEADER);
header.add(EXECUTION_TIME_HEADER);
csvPrinter.printRecord(header);
for (int i = 0; i < events.size() - 1; i += 2) {
final JsonNode startEvent = events.get(i);
final JsonNode endEvent = events.get(i + 1);
final List<String> eventReportDetails = IteratorUtils.toList(endEvent.elements());
final String startEventDateTimeStr = startEvent.get(EVENT_DATE_TIME_FIELD).asText();
final String endEventDateTimeStr = endEvent.get(EVENT_DATE_TIME_FIELD).asText();
eventReportDetails.add(startEventDateTimeStr);
eventReportDetails.add(endEventDateTimeStr);
eventReportDetails.add(calculateExecutionTime(startEventDateTimeStr, endEventDateTimeStr).toString());
csvPrinter.printRecord(eventReportDetails);
}
// Last Event
final List<String> lastEventDetails = IteratorUtils.toList(lastEvent.elements());
lastEventDetails.add(startOperationTimeStr);
lastEventDetails.add(endOperationTimeStr);
lastEventDetails.add(calculateExecutionTime(startOperationTimeStr, endOperationTimeStr).toString());
csvPrinter.printRecord(lastEventDetails);
csvPrinter.flush();
csvPrinter.close();
} catch (final Exception e) {
csvOutputStream.close();
throw new VitamException(UNEXPECTED_EXCEPTION_DURING_CSV_FILE_GENERATION);
}
return csvOutputStream;
}Example 30
| Project: act-master File: StandardIonAnalysis.java View source code |
public static void main(String[] args) throws Exception {
Options opts = new Options();
for (Option.Builder b : OPTION_BUILDERS) {
opts.addOption(b.build());
}
CommandLine cl = null;
try {
CommandLineParser parser = new DefaultParser();
cl = parser.parse(opts, args);
} catch (ParseException e) {
System.err.format("Argument parsing failed: %s\n", e.getMessage());
HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
System.exit(1);
}
if (cl.hasOption("help")) {
HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
return;
}
File lcmsDir = new File(cl.getOptionValue(OPTION_DIRECTORY));
if (!lcmsDir.isDirectory()) {
System.err.format("File at %s is not a directory\n", lcmsDir.getAbsolutePath());
HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
System.exit(1);
}
try (DB db = DB.openDBFromCLI(cl)) {
ScanFile.insertOrUpdateScanFilesInDirectory(db, lcmsDir);
StandardIonAnalysis analysis = new StandardIonAnalysis();
HashMap<Integer, Plate> plateCache = new HashMap<>();
String plateBarcode = cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE);
String inputChemicals = cl.getOptionValue(OPTION_STANDARD_CHEMICAL);
String medium = cl.getOptionValue(OPTION_MEDIUM);
// If standard chemical is specified, do standard LCMS ion selection analysis
if (inputChemicals != null && !inputChemicals.equals("")) {
String[] chemicals;
if (!inputChemicals.contains(",")) {
chemicals = new String[1];
chemicals[0] = inputChemicals;
} else {
chemicals = inputChemicals.split(",");
}
String outAnalysis = cl.getOptionValue(OPTION_OUTPUT_PREFIX) + "." + CSV_FORMAT;
String plottingDirectory = cl.getOptionValue(OPTION_PLOTTING_DIR);
String[] headerStrings = { "Molecule", "Plate Bar Code", "LCMS Detection Results" };
CSVPrinter printer = new CSVPrinter(new FileWriter(outAnalysis), CSVFormat.DEFAULT.withHeader(headerStrings));
for (String inputChemical : chemicals) {
List<StandardWell> standardWells;
Plate queryPlate = Plate.getPlateByBarcode(db, cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE));
if (plateBarcode != null && medium != null) {
standardWells = analysis.getStandardWellsForChemicalInSpecificPlateAndMedium(db, inputChemical, queryPlate.getId(), medium);
} else if (plateBarcode != null) {
standardWells = analysis.getStandardWellsForChemicalInSpecificPlate(db, inputChemical, queryPlate.getId());
} else {
standardWells = analysis.getStandardWellsForChemical(db, inputChemical);
}
if (standardWells.size() == 0) {
throw new RuntimeException("Found no LCMS wells for " + inputChemical);
}
// Sort in descending order of media where MeOH and Water related media are promoted to the top and
// anything derived from yeast media are demoted.
Collections.sort(standardWells, new Comparator<StandardWell>() {
@Override
public int compare(StandardWell o1, StandardWell o2) {
if (StandardWell.doesMediaContainYeastExtract(o1.getMedia()) && !StandardWell.doesMediaContainYeastExtract(o2.getMedia())) {
return 1;
} else {
return 0;
}
}
});
Map<StandardWell, StandardIonResult> wellToIonRanking = StandardIonAnalysis.getBestMetlinIonsForChemical(inputChemical, lcmsDir, db, standardWells, plottingDirectory);
if (wellToIonRanking.size() != standardWells.size() && !cl.hasOption(OPTION_OVERRIDE_NO_SCAN_FILE_FOUND)) {
throw new Exception("Could not find a scan file associated with one of the standard wells");
}
for (StandardWell well : wellToIonRanking.keySet()) {
LinkedHashMap<String, XZ> snrResults = wellToIonRanking.get(well).getAnalysisResults();
String snrRankingResults = "";
int numResultsToShow = 0;
Plate plateForWellToAnalyze = Plate.getPlateById(db, well.getPlateId());
for (Map.Entry<String, XZ> ionToSnrAndTime : snrResults.entrySet()) {
if (numResultsToShow > 3) {
break;
}
String ion = ionToSnrAndTime.getKey();
XZ snrAndTime = ionToSnrAndTime.getValue();
snrRankingResults += String.format(ion + " (%.2f SNR at %.2fs); ", snrAndTime.getIntensity(), snrAndTime.getTime());
numResultsToShow++;
}
String[] resultSet = { inputChemical, plateForWellToAnalyze.getBarcode() + " " + well.getCoordinatesString() + " " + well.getMedia() + " " + well.getConcentration(), snrRankingResults };
printer.printRecord(resultSet);
}
}
try {
printer.flush();
printer.close();
} catch (IOException e) {
System.err.println("Error while flushing/closing csv writer.");
e.printStackTrace();
}
} else {
// Get the set of chemicals that includes the construct and all it's intermediates
Pair<ConstructEntry, List<ChemicalAssociatedWithPathway>> constructAndPathwayChems = analysis.getChemicalsForConstruct(db, cl.getOptionValue(OPTION_CONSTRUCT));
System.out.format("Construct: %s\n", constructAndPathwayChems.getLeft().getCompositionId());
for (ChemicalAssociatedWithPathway pathwayChem : constructAndPathwayChems.getRight()) {
System.out.format(" Pathway chem %s\n", pathwayChem.getChemical());
// Get all the standard wells for the pathway chemicals. These wells contain only the
// the chemical added with controlled solutions (ie no organism or other chemicals in the
// solution)
List<StandardWell> standardWells;
if (plateBarcode != null) {
Plate queryPlate = Plate.getPlateByBarcode(db, cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE));
standardWells = analysis.getStandardWellsForChemicalInSpecificPlate(db, pathwayChem.getChemical(), queryPlate.getId());
} else {
standardWells = analysis.getStandardWellsForChemical(db, pathwayChem.getChemical());
}
for (StandardWell wellToAnalyze : standardWells) {
List<StandardWell> negativeControls = analysis.getViableNegativeControlsForStandardWell(db, wellToAnalyze);
Map<StandardWell, List<ScanFile>> allViableScanFiles = analysis.getViableScanFilesForStandardWells(db, wellToAnalyze, negativeControls);
List<String> primaryStandardScanFileNames = new ArrayList<>();
for (ScanFile scanFile : allViableScanFiles.get(wellToAnalyze)) {
primaryStandardScanFileNames.add(scanFile.getFilename());
}
Plate plate = plateCache.get(wellToAnalyze.getPlateId());
if (plate == null) {
plate = Plate.getPlateById(db, wellToAnalyze.getPlateId());
plateCache.put(plate.getId(), plate);
}
System.out.format(" Standard well: %s @ %s, '%s'%s%s\n", plate.getBarcode(), wellToAnalyze.getCoordinatesString(), wellToAnalyze.getChemical(), wellToAnalyze.getMedia() == null ? "" : String.format(" in %s", wellToAnalyze.getMedia()), wellToAnalyze.getConcentration() == null ? "" : String.format(" @ %s", wellToAnalyze.getConcentration()));
System.out.format(" Scan files: %s\n", StringUtils.join(primaryStandardScanFileNames, ", "));
for (StandardWell negCtrlWell : negativeControls) {
plate = plateCache.get(negCtrlWell.getPlateId());
if (plate == null) {
plate = Plate.getPlateById(db, negCtrlWell.getPlateId());
plateCache.put(plate.getId(), plate);
}
List<String> negativeControlScanFileNames = new ArrayList<>();
for (ScanFile scanFile : allViableScanFiles.get(negCtrlWell)) {
negativeControlScanFileNames.add(scanFile.getFilename());
}
System.out.format(" Viable negative: %s @ %s, '%s'%s%s\n", plate.getBarcode(), negCtrlWell.getCoordinatesString(), negCtrlWell.getChemical(), negCtrlWell.getMedia() == null ? "" : String.format(" in %s", negCtrlWell.getMedia()), negCtrlWell.getConcentration() == null ? "" : String.format(" @ %s", negCtrlWell.getConcentration()));
System.out.format(" Scan files: %s\n", StringUtils.join(negativeControlScanFileNames, ", "));
// TODO: do something useful with the standard wells and their scan files, and then stop all the printing.
}
}
}
}
}
}Example 31
| Project: AisLib-master File: AisPacketCSVOutputSink.java View source code |
@Override
public void process(OutputStream stream, AisPacket packet, long count) throws IOException {
csvLock.lock();
try {
if (csv == null) {
csv = new CSVPrinter(new OutputStreamWriter(stream), CSVFormat.DEFAULT.withHeader(columns));
}
AisMessage m = packet.tryGetAisMessage();
if (m != null) {
List fields = Lists.newArrayList();
for (String column : columns) {
switch(column) {
case "timestamp":
addTimestamp(fields, packet);
break;
case "timestamp_pretty":
addTimestampPretty(fields, packet);
break;
case "msgid":
addMsgId(fields, m);
break;
case "mmsi":
addMmsi(fields, m);
break;
case "posacc":
addPositionAccuracy(fields, m);
break;
case "lat":
addLatitude(fields, m);
break;
case "lon":
addLongitude(fields, m);
break;
case "targetType":
addTargetType(fields, m);
break;
case "sog":
addSog(fields, m);
break;
case "cog":
addCog(fields, m);
break;
case "trueHeading":
addTrueHeading(fields, m);
break;
case "draught":
addDraught(fields, m);
break;
case "name":
addName(fields, m);
break;
case "dimBow":
addDimBow(fields, m);
break;
case "dimPort":
addDimPort(fields, m);
break;
case "dimStarboard":
addDimStarboard(fields, m);
break;
case "dimStern":
addDimStern(fields, m);
break;
case "shipTypeCargoTypeCode":
addShipTypeCargoTypeCode(fields, m);
break;
case "shipType":
addShipType(fields, m);
break;
case "shipCargo":
addShipCargo(fields, m);
break;
case "callsign":
addCallsign(fields, m);
break;
case "imo":
addImo(fields, m);
break;
case "destination":
addDestination(fields, m);
break;
case "eta":
addEta(fields, m);
break;
default:
LOG.warn("Unknown column: " + column);
fields.add(NULL_INDICATOR);
}
}
csv.printRecord(fields);
}
} finally {
csvLock.unlock();
}
}Example 32
| Project: buendia-master File: DataExportServlet.java View source code |
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set the default merge mode
boolean merge = true;
// Defines the interval in minutes that will be used to merge encounters.
int interval = DEFAULT_INTERVAL_MINS;
String intervalParameter = request.getParameter("interval");
if (intervalParameter != null) {
int newInterval = Integer.valueOf(intervalParameter);
if (newInterval >= 0) {
interval = newInterval;
if (interval == 0) {
merge = false;
}
} else {
log.error("Interval value is less then 0. Default used.");
}
}
CSVPrinter printer = new CSVPrinter(response.getWriter(), CSVFormat.EXCEL.withDelimiter(','));
//check for authenticated users
if (!XformsUtil.isAuthenticated(request, response, null))
return;
Date now = new Date();
DateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
String filename = String.format("buendiadata_%s.csv", format.format(now));
String contentDispositionHeader = String.format("attachment; filename=%s;", filename);
response.addHeader("Content-Disposition", contentDispositionHeader);
PatientService patientService = Context.getPatientService();
EncounterService encounterService = Context.getEncounterService();
List<Patient> patients = new ArrayList<>(patientService.getAllPatients());
Collections.sort(patients, PATIENT_COMPARATOR);
// We may want to get the observations displayed in the chart/xform, in which case there
// are a few
// sensible orders:
// 1: UUID
// 2: Order in chart
// 3: Order in Xform
// Order in Xform/chart is not good as stuff changes every time we change xform
// So instead we will use UUID order, but use the Chart form to use the concepts to display.
Set<Concept> questionConcepts = new HashSet<>();
for (Form form : ChartResource.getCharts(Context.getFormService())) {
TreeMap<Integer, TreeSet<FormField>> formStructure = FormUtil.getFormStructure(form);
for (FormField groupField : formStructure.get(0)) {
for (FormField fieldInGroup : formStructure.get(groupField.getId())) {
questionConcepts.add(fieldInGroup.getField().getConcept());
}
}
}
FixedSortedConceptIndexer indexer = new FixedSortedConceptIndexer(questionConcepts);
// Write English headers.
writeHeaders(printer, indexer);
Calendar calendar = Calendar.getInstance();
// Loop through all the patients and get their encounters.
for (Patient patient : patients) {
// Define an array that will represent the line that will be inserted in the CSV.
Object[] previousCSVLine = new Object[FIXED_HEADERS.length + indexer.size() * COLUMNS_PER_OBS];
Date deadLine = new Date(0);
ArrayList<Encounter> encounters = new ArrayList<>(encounterService.getEncountersByPatient(patient));
Collections.sort(encounters, ENCOUNTER_COMPARATOR);
// TODO: For now patients with no encounters are ignored. List them on the future.
if (encounters.size() == 0)
continue;
// Loop through all the encounters for this patient to get the observations.
for (Encounter encounter : encounters) {
try {
// Flag to whether we will use the merged version of the encounter
// or the single version.
boolean useMerged = merge;
// Array that will be used to merge in previous encounter with the current one.
Object[] mergedCSVLine = new Object[previousCSVLine.length];
// Duplicate previous encounter into the (future to be) merged one.
System.arraycopy(previousCSVLine, 0, mergedCSVLine, 0, previousCSVLine.length);
// Define the array to be used to store the current encounter.
Object[] currentCSVLine = new Object[FIXED_HEADERS.length + indexer.size() * COLUMNS_PER_OBS];
// If the current encounter is more then "interval" minutes from the previous
// print the previous and reset it.
Date encounterTime = encounter.getEncounterDatetime();
if (encounterTime.after(deadLine)) {
printer.printRecord(previousCSVLine);
previousCSVLine = new Object[FIXED_HEADERS.length + indexer.size() * COLUMNS_PER_OBS];
useMerged = false;
}
// Set the next deadline as the current encounter time plus "interval" minutes.
calendar.setTime(encounterTime);
calendar.add(Calendar.MINUTE, interval);
deadLine = calendar.getTime();
// Fill the fixed columns values.
currentCSVLine[0] = patient.getUuid();
currentCSVLine[1] = patient.getPatientIdentifier("MSF");
if (patient.getBirthdate() != null) {
currentCSVLine[2] = Utils.YYYYMMDD_UTC_FORMAT.format(patient.getBirthdate());
}
currentCSVLine[3] = encounter.getUuid();
currentCSVLine[4] = encounterTime.getTime();
currentCSVLine[5] = Utils.toIso8601(encounterTime);
currentCSVLine[6] = Utils.SPREADSHEET_FORMAT.format(encounterTime);
// All the values fo the fixed columns saved in the current encounter line
// will also be saved to the merged line.
System.arraycopy(currentCSVLine, 0, mergedCSVLine, 0, 7);
// Loop through all the observations for this encounter
for (Obs obs : encounter.getAllObs()) {
Integer index = indexer.getIndex(obs.getConcept());
if (index == null)
continue;
// For each observation there are three columns: if the value of the
// observation is a concept, then the three columns contain the English
// name, the OpenMRS ID, and the UUID of the concept; otherwise all
// three columns contain the formatted value.
int valueColumn = FIXED_HEADERS.length + index * COLUMNS_PER_OBS;
// Coded values are treated differently
if (obs.getValueCoded() != null) {
Concept value = obs.getValueCoded();
currentCSVLine[valueColumn] = NAMER.getClientName(value);
currentCSVLine[valueColumn + 1] = value.getId();
currentCSVLine[valueColumn + 2] = value.getUuid();
if (useMerged) {
// If we are still merging the current encounter values into
// the previous one get the previous value and see if it had
// something in it.
String previousValue = (String) mergedCSVLine[valueColumn];
if ((previousValue == null) || (previousValue.isEmpty())) {
// If the previous value was empty copy the current value into it.
mergedCSVLine[valueColumn] = currentCSVLine[valueColumn];
mergedCSVLine[valueColumn + 1] = currentCSVLine[valueColumn + 1];
mergedCSVLine[valueColumn + 2] = currentCSVLine[valueColumn + 2];
} else {
// If the previous encounter have values stored for this
// observation we cannot merge them anymore.
useMerged = false;
}
}
} else // All values except the coded ones will be treated equally.
{
// Return the value of the the current observation using the visitor.
String value = (String) VisitObsValue.visit(obs, stringVisitor);
// Check if we have values stored for this observation
if ((value != null) && (!value.isEmpty())) {
// Save the value of the observation on the current encounter line.
currentCSVLine[valueColumn] = value;
currentCSVLine[valueColumn + 1] = value;
currentCSVLine[valueColumn + 2] = value;
if (useMerged) {
// Since we are still merging this encounter with the previous
// one let's get the previous value to see if it had something
// stored on it.
String previousValue = (String) mergedCSVLine[valueColumn];
if ((previousValue != null) && (!previousValue.isEmpty())) {
// the previous encounter
if (obs.getValueText() != null) {
// We only continue merging if the observation is of
// type text, so we concatenate it.
// TODO: add timestamps to the merged values that are of type text
previousValue += "\n" + value;
value = previousValue;
} else {
// Any other type of value we stop the merging.
useMerged = false;
}
}
mergedCSVLine[valueColumn] = value;
mergedCSVLine[valueColumn + 1] = value;
mergedCSVLine[valueColumn + 2] = value;
}
}
}
}
if (useMerged) {
// If after looping through all the observations we didn't had any
// overlapped values we keep the merged line.
previousCSVLine = mergedCSVLine;
} else {
// current encounter the previous one. Only if the previous line is not empty.
if (previousCSVLine[0] != null) {
printer.printRecord(previousCSVLine);
}
previousCSVLine = currentCSVLine;
}
} catch (Exception e) {
log.error("Error exporting encounter", e);
}
}
// For the last encounter we print the remaining line.
printer.printRecord(previousCSVLine);
}
}Example 33
| Project: open-rmbt-master File: ExportResource.java View source code |
@Override
public void write(OutputStream out) throws IOException {
//cache in file => create temporary temporary file (to
// handle errors while fulfilling a request)
String property = System.getProperty("java.io.tmpdir");
final File cachedFile = new File(property + File.separator + ((zip) ? filename_zip : filename_csv) + "_tmp");
OutputStream outf = new FileOutputStream(cachedFile);
if (zip) {
final ZipOutputStream zos = new ZipOutputStream(outf);
final ZipEntry zeLicense = new ZipEntry("LIZENZ.txt");
zos.putNextEntry(zeLicense);
final InputStream licenseIS = getClass().getResourceAsStream("DATA_LICENSE.txt");
IOUtils.copy(licenseIS, zos);
licenseIS.close();
final ZipEntry zeCsv = new ZipEntry(filename_csv);
zos.putNextEntry(zeCsv);
outf = zos;
}
final OutputStreamWriter osw = new OutputStreamWriter(outf);
final CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat);
for (final String c : columns) csvPrinter.print(c);
csvPrinter.println();
for (final String[] line : data) {
for (final String f : line) csvPrinter.print(f);
csvPrinter.println();
}
csvPrinter.flush();
if (zip)
outf.close();
//if we reach this code, the data is now cached in a temporary tmp-file
//so, rename the file for "production use2
//concurrency issues should be solved by the operating system
File newCacheFile = new File(property + File.separator + ((zip) ? filename_zip : filename_csv));
Files.move(cachedFile.toPath(), newCacheFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
FileInputStream fis = new FileInputStream(newCacheFile);
IOUtils.copy(fis, out);
fis.close();
out.close();
}Example 34
| Project: AisAbnormal-master File: FreeFlowAnalysis.java View source code |
private void writeToCSVFile(FreeFlowData freeFlowData) {
if (csvFileName == null)
return;
final File csvFile = new File(csvFileName);
final boolean fileExists = csvFile.exists() == true || csvFile.length() > 0;
lock.lock();
if (!fileExists) {
try {
if (csvFilePrinter != null)
csvFilePrinter.close();
if (fileWriter != null)
fileWriter.close();
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
csvFilePrinter = null;
fileWriter = null;
}
try {
if (fileWriter == null) {
try {
fileWriter = new FileWriter(csvFile, true);
if (csvFilePrinter == null) {
try {
csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.RFC4180.withCommentMarker('#'));
} catch (IOException e) {
csvFilePrinter = null;
LOG.error(e.getMessage(), e);
LOG.error("Failed to write line to CSV file: " + freeFlowData);
return;
}
}
} catch (IOException e) {
fileWriter = null;
LOG.error(e.getMessage(), e);
LOG.error("Failed to write line to CSV file: " + freeFlowData);
return;
}
}
if (!fileExists) {
LOG.info("Created new CSV file: " + csvFile.getAbsolutePath());
csvFilePrinter.printComment("Generated by AIS Abnormal Behaviour Analyzer");
csvFilePrinter.printComment("File created: " + LocalDateTime.now().format(fmt));
csvFilePrinter.printRecord("TIMESTAMP (GMT)", "MMSI1", "NAME1", "TP1", "LOA1", "BM1", "COG1", "HDG1", "SOG1", "LAT1", "LON1", "MMSI2", "NAME2", "TP2", "LOA2", "BM2", "COG2", "HDG2", "SOG2", "LAT2", "LON2", "BRG", "DST");
}
final Track t0 = freeFlowData.getTrackSnapshot();
final Position p0 = freeFlowData.getTrackCenterPosition();
List<FreeFlowData.TrackInsideEllipse> tracks = freeFlowData.getTracksInsideEllipse();
for (FreeFlowData.TrackInsideEllipse track : tracks) {
final Track t1 = track.getTrackSnapshot();
final Position p1 = track.getTrackCenterPosition();
final int d = (int) p0.distanceTo(p1, CoordinateSystem.CARTESIAN);
final int b = (int) p0.rhumbLineBearingTo(p1);
List csvRecord = new ArrayList<>();
csvRecord.add(String.format(Locale.ENGLISH, "%s", t0.getTimeOfLastPositionReportTyped().format(fmt)));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getMmsi()));
csvRecord.add(String.format(Locale.ENGLISH, "%s", trimAisString(t0.getShipName()).replace(',', ' ')));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getShipType()));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getVesselLength()));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getVesselBeam()));
csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t0.getCourseOverGround()));
csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t0.getTrueHeading()));
csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t0.getSpeedOverGround()));
csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p0.getLatitude()));
csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p0.getLongitude()));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getMmsi()));
csvRecord.add(String.format(Locale.ENGLISH, "%s", trimAisString(t1.getShipName()).replace(',', ' ')));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getShipType()));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getVesselLength()));
csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getVesselBeam()));
csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t1.getCourseOverGround()));
csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t1.getTrueHeading()));
csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t1.getSpeedOverGround()));
csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p1.getLatitude()));
csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p1.getLongitude()));
csvRecord.add(String.format(Locale.ENGLISH, "%d", b));
csvRecord.add(String.format(Locale.ENGLISH, "%d", d));
try {
csvFilePrinter.printRecord(csvRecord);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
LOG.error("Failed to write line to CSV file: " + freeFlowData);
}
}
csvFilePrinter.flush();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
lock.unlock();
}
}Example 35
| Project: cloudsync-master File: Handler.java View source code |
private void releaseLock() throws CloudsyncException {
if (!isLocked || duplicates.size() > 0)
return;
try {
Files.delete(lockFilePath);
} catch (IOException e) {
throw new CloudsyncException("Couldn't remove '" + lockFilePath.toString() + "'");
}
try {
if (root.getChildren().size() > 0) {
LOGGER.log(Level.INFO, "write structure to cache file");
final PrintWriter out = new PrintWriter(cacheFilePath.toFile());
final CSVPrinter csvOut = new CSVPrinter(out, CSVFormat.EXCEL);
writeStructureToCSVPrinter(csvOut, root);
out.close();
}
} catch (final IOException e) {
throw new CloudsyncException("Can't write cache file on '" + cacheFilePath.toString() + "'", e);
}
isLocked = false;
}Example 36
| Project: datacollector-master File: KafkaTestUtil.java View source code |
public static List<KeyedMessage<String, String>> produceCsvMessages(String topic, String partition, CSVFormat csvFormat, File csvFile) throws IOException {
List<KeyedMessage<String, String>> messages = new ArrayList<>();
String line;
BufferedReader bufferedReader = new BufferedReader(new FileReader(KafkaTestUtil.class.getClassLoader().getResource("testKafkaTarget.csv").getFile()));
while ((line = bufferedReader.readLine()) != null) {
String[] strings = line.split(",");
StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
csvPrinter.printRecord(strings);
csvPrinter.flush();
csvPrinter.close();
messages.add(new KeyedMessage<>(topic, partition, stringWriter.toString()));
}
return messages;
}Example 37
| Project: kado-master File: PrestoUtil.java View source code |
/**
* Write Result to CSV format file
* @param table table name
* @param outputPath Local custom output path
* @param finalName if outputPath contain file name
* @return The local absolute path of the file output
*/
public String writeAsCSV(String table, String outputPath, boolean finalName) {
// Remove the extra separator
while (outputPath.lastIndexOf(File.separator) == (outputPath.length() - 1)) {
outputPath = outputPath.substring(0, outputPath.length() - 1);
}
String resultPath = "";
if (finalName)
resultPath = outputPath + "@" + Instant.now().toEpochMilli() + ".csv";
else
resultPath = outputPath + File.separator + table + "@" + Instant.now().toEpochMilli() + ".csv";
ResultMap resultMap = readJsonAsResult(table, 0, -1);
File csvfile = new File(resultPath);
// create parent dir
if (!csvfile.getParentFile().exists())
csvfile.getParentFile().mkdirs();
if (csvfile.exists())
csvfile.delete();
CSVPrinter csvp = null;
CSVFormat format = CSVFormat.EXCEL;
try {
FileWriter fw = new FileWriter(csvfile);
csvp = new CSVPrinter(fw, format);
csvp.printRecord(resultMap.getSchema());
for (int i = 0; i < resultMap.getCount(); i++) {
csvp.printRecord(resultMap.getData().get(i));
if (i % batchSize == 0)
fw.flush();
}
fw.flush();
fw.close();
csvp.close();
} catch (IOException e) {
this.setSuccess(false);
this.setException(ExceptionUtils.getStackTrace(e));
return "";
}
return resultPath;
}Example 38
| Project: mdrill-master File: CSVResponseWriter.java View source code |
public void writeResponse() throws IOException {
SolrParams params = req.getParams();
strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true);
CSVStrategy strat = strategy;
String sep = params.get(CSV_SEPARATOR);
if (sep != null) {
if (sep.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid separator:'" + sep + "'");
strat.setDelimiter(sep.charAt(0));
}
String nl = params.get(CSV_NEWLINE);
if (nl != null) {
if (nl.length() == 0)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid newline:'" + nl + "'");
strat.setPrinterNewline(nl);
}
String encapsulator = params.get(CSV_ENCAPSULATOR);
String escape = params.get(CSV_ESCAPE);
if (encapsulator != null) {
if (encapsulator.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid encapsulator:'" + encapsulator + "'");
strat.setEncapsulator(encapsulator.charAt(0));
}
if (escape != null) {
if (escape.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid escape:'" + escape + "'");
strat.setEscape(escape.charAt(0));
if (encapsulator == null) {
strat.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED);
}
}
if (strat.getEscape() == '\\') {
// be escaped.
strat.setUnicodeEscapeInterpretation(true);
}
printer = new CSVPrinter(writer, strategy);
CSVStrategy mvStrategy = new CSVStrategy(strategy.getDelimiter(), CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);
strat = mvStrategy;
sep = params.get(MV_SEPARATOR);
if (sep != null) {
if (sep.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'");
strat.setDelimiter(sep.charAt(0));
}
encapsulator = params.get(MV_ENCAPSULATOR);
escape = params.get(MV_ESCAPE);
if (encapsulator != null) {
if (encapsulator.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'");
strat.setEncapsulator(encapsulator.charAt(0));
if (escape == null) {
strat.setEscape(CSVStrategy.ESCAPE_DISABLED);
}
}
escape = params.get(MV_ESCAPE);
if (escape != null) {
if (escape.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'");
strat.setEscape(escape.charAt(0));
}
returnScore = returnFields != null && returnFields.contains("score");
boolean needListOfFields = returnFields == null || returnFields.size() == 0 || (returnFields.size() == 1 && returnScore) || returnFields.contains("*");
Collection<String> fields = returnFields;
Object responseObj = rsp.getValues().get("response");
if (needListOfFields) {
if (responseObj instanceof SolrDocumentList) {
// get the list of fields from the SolrDocumentList
fields = new LinkedHashSet<String>();
for (SolrDocument sdoc : (SolrDocumentList) responseObj) {
fields.addAll(sdoc.getFieldNames());
}
} else {
// get the list of fields from the index
fields = req.getSearcher().getFieldNames();
}
if (returnScore) {
fields.add("score");
} else {
fields.remove("score");
}
}
CSVSharedBufPrinter csvPrinterMV = new CSVSharedBufPrinter(mvWriter, mvStrategy);
for (String field : fields) {
if (field.equals("score")) {
CSVField csvField = new CSVField();
csvField.name = "score";
csvFields.put("score", csvField);
continue;
}
SchemaField sf = schema.getFieldOrNull(field);
if (sf == null) {
FieldType ft = new StrField();
sf = new SchemaField(field, ft);
}
// if we got the list of fields from the index, only list stored fields
if (returnFields == null && sf != null && !sf.stored()) {
continue;
}
// check for per-field overrides
sep = params.get("f." + field + '.' + CSV_SEPARATOR);
encapsulator = params.get("f." + field + '.' + CSV_ENCAPSULATOR);
escape = params.get("f." + field + '.' + CSV_ESCAPE);
CSVSharedBufPrinter csvPrinter = csvPrinterMV;
if (sep != null || encapsulator != null || escape != null) {
// create a new strategy + printer if there were any per-field overrides
strat = (CSVStrategy) mvStrategy.clone();
if (sep != null) {
if (sep.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'");
strat.setDelimiter(sep.charAt(0));
}
if (encapsulator != null) {
if (encapsulator.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'");
strat.setEncapsulator(encapsulator.charAt(0));
if (escape == null) {
strat.setEscape(CSVStrategy.ESCAPE_DISABLED);
}
}
if (escape != null) {
if (escape.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'");
strat.setEscape(escape.charAt(0));
if (encapsulator == null) {
strat.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED);
}
}
csvPrinter = new CSVSharedBufPrinter(mvWriter, strat);
}
CSVField csvField = new CSVField();
csvField.name = field;
csvField.sf = sf;
csvField.mvPrinter = csvPrinter;
csvFields.put(field, csvField);
}
NullValue = params.get(CSV_NULL, "");
if (params.getBool(CSV_HEADER, true)) {
for (CSVField csvField : csvFields.values()) {
printer.print(csvField.name);
}
printer.println();
}
if (responseObj instanceof DocList) {
writeDocList(null, (DocList) responseObj, null, null);
} else if (responseObj instanceof SolrDocumentList) {
writeSolrDocumentList(null, (SolrDocumentList) responseObj, null, null);
}
}Example 39
| Project: solrcene-master File: CSVResponseWriter.java View source code |
public void writeResponse() throws IOException {
SolrParams params = req.getParams();
strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true);
CSVStrategy strat = strategy;
String sep = params.get(CSV_SEPARATOR);
if (sep != null) {
if (sep.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid separator:'" + sep + "'");
strat.setDelimiter(sep.charAt(0));
}
String nl = params.get(CSV_NEWLINE);
if (nl != null) {
if (nl.length() == 0)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid newline:'" + nl + "'");
strat.setPrinterNewline(nl);
}
String encapsulator = params.get(CSV_ENCAPSULATOR);
String escape = params.get(CSV_ESCAPE);
if (encapsulator != null) {
if (encapsulator.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid encapsulator:'" + encapsulator + "'");
strat.setEncapsulator(encapsulator.charAt(0));
}
if (escape != null) {
if (escape.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid escape:'" + escape + "'");
strat.setEscape(escape.charAt(0));
if (encapsulator == null) {
strat.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED);
}
}
if (strat.getEscape() == '\\') {
// be escaped.
strat.setUnicodeEscapeInterpretation(true);
}
printer = new CSVPrinter(writer, strategy);
CSVStrategy mvStrategy = new CSVStrategy(strategy.getDelimiter(), CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);
strat = mvStrategy;
sep = params.get(MV_SEPARATOR);
if (sep != null) {
if (sep.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'");
strat.setDelimiter(sep.charAt(0));
}
encapsulator = params.get(MV_ENCAPSULATOR);
escape = params.get(MV_ESCAPE);
if (encapsulator != null) {
if (encapsulator.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'");
strat.setEncapsulator(encapsulator.charAt(0));
if (escape == null) {
strat.setEscape(CSVStrategy.ESCAPE_DISABLED);
}
}
escape = params.get(MV_ESCAPE);
if (escape != null) {
if (escape.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'");
strat.setEscape(escape.charAt(0));
}
returnScore = returnFields != null && returnFields.contains("score");
boolean needListOfFields = returnFields == null || returnFields.size() == 0 || (returnFields.size() == 1 && returnScore) || returnFields.contains("*");
Collection<String> fields = returnFields;
Object responseObj = rsp.getValues().get("response");
if (needListOfFields) {
if (responseObj instanceof SolrDocumentList) {
// get the list of fields from the SolrDocumentList
fields = new LinkedHashSet<String>();
for (SolrDocument sdoc : (SolrDocumentList) responseObj) {
fields.addAll(sdoc.getFieldNames());
}
} else {
// get the list of fields from the index
fields = req.getSearcher().getFieldNames();
}
if (returnScore) {
fields.add("score");
} else {
fields.remove("score");
}
}
CSVSharedBufPrinter csvPrinterMV = new CSVSharedBufPrinter(mvWriter, mvStrategy);
for (String field : fields) {
if (field.equals("score")) {
CSVField csvField = new CSVField();
csvField.name = "score";
csvFields.put("score", csvField);
continue;
}
SchemaField sf = schema.getFieldOrNull(field);
if (sf == null) {
FieldType ft = new StrField();
sf = new SchemaField(field, ft);
}
// if we got the list of fields from the index, only list stored fields
if (returnFields == null && sf != null && !sf.stored()) {
continue;
}
// check for per-field overrides
sep = params.get("f." + field + '.' + CSV_SEPARATOR);
encapsulator = params.get("f." + field + '.' + CSV_ENCAPSULATOR);
escape = params.get("f." + field + '.' + CSV_ESCAPE);
CSVSharedBufPrinter csvPrinter = csvPrinterMV;
if (sep != null || encapsulator != null || escape != null) {
// create a new strategy + printer if there were any per-field overrides
strat = (CSVStrategy) mvStrategy.clone();
if (sep != null) {
if (sep.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'");
strat.setDelimiter(sep.charAt(0));
}
if (encapsulator != null) {
if (encapsulator.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'");
strat.setEncapsulator(encapsulator.charAt(0));
if (escape == null) {
strat.setEscape(CSVStrategy.ESCAPE_DISABLED);
}
}
if (escape != null) {
if (escape.length() != 1)
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'");
strat.setEscape(escape.charAt(0));
if (encapsulator == null) {
strat.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED);
}
}
csvPrinter = new CSVSharedBufPrinter(mvWriter, strat);
}
CSVField csvField = new CSVField();
csvField.name = field;
csvField.sf = sf;
csvField.mvPrinter = csvPrinter;
csvFields.put(field, csvField);
}
NullValue = params.get(CSV_NULL, "");
if (params.getBool(CSV_HEADER, true)) {
for (CSVField csvField : csvFields.values()) {
printer.print(csvField.name);
}
printer.println();
}
if (responseObj instanceof DocList) {
writeDocList(null, (DocList) responseObj, null, null);
} else if (responseObj instanceof SolrDocumentList) {
writeSolrDocumentList(null, (SolrDocumentList) responseObj, null, null);
}
}Example 40
| Project: UnitedTweetAnalyzer-master File: Learner.java View source code |
/**
* Classify unseen instances.
* It builds the classifier against the training set
* and then assign a classification to each unlabeled instance.
*
* @param output_path optional path to store a CSV file with the results.
*/
public void buildAndClassify(String output_path) {
try {
this.loadData(false);
} catch (Exception e) {
logger.fatal("Error while loading training and unlabeled data", e);
return;
}
CSVPrinter csvFilePrinter = null;
FileWriter fileWriter = null;
if (output_path != null) {
final CSVFormat csvFileFormat = CSVFormat.EXCEL.withDelimiter(CSV_DELIMITER);
try {
fileWriter = new FileWriter(output_path);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
csvFilePrinter.printRecord(CSV_FILE_HEADER);
} catch (IOException e) {
logger.warn("Error while creating CSV file printer", e);
IOUtils.closeQuietly(fileWriter);
IOUtils.closeQuietly(csvFilePrinter);
}
}
try {
logger.info("Building classifier {}...", this.classifier.getClass().getSimpleName());
this.trainClassifier(this.training_data);
} catch (Exception e) {
logger.fatal("Error while building classifier for new instances.", e);
IOUtils.closeQuietly(fileWriter);
IOUtils.closeQuietly(csvFilePrinter);
return;
}
final Attribute attribute_id = this.classification_data.attribute(Storage.ID);
final Attribute attribute_lang = this.classification_data.attribute(Storage.LANG);
final Attribute attribute_utc_offset = this.classification_data.attribute(Storage.UTC_OFFSET);
final Attribute attribute_timezone = this.classification_data.attribute(Storage.TIMEZONE);
Remove remove;
try {
remove = new Remove();
remove.setAttributeIndices(String.format("%d", attribute_id.index() + 1));
remove.setInputFormat(this.classification_data);
} catch (Exception e) {
logger.error("Error while building remove filter for unlabeled instances", e);
return;
}
for (Instance i : this.classification_data) {
remove.input(i);
Instance trimmedInstance = remove.output();
final long id = Double.valueOf(i.value(attribute_id)).longValue();
double classification;
try {
classification = this.classifier.classifyInstance(trimmedInstance);
} catch (Exception e) {
logger.warn("Classification - id: {}, class: UNAVAILABLE", id);
logger.error("Error while classifying unlabeled instance", e);
return;
}
String location;
if (this.wordsToKeep > 0) {
StringBuilder locationBuilder = new StringBuilder();
for (Attribute attribute : Collections.list(i.enumerateAttributes())) {
if (attribute.name().startsWith(LOCATION_PREFIX) && i.value(attribute) > 0) {
locationBuilder.append(attribute.name().substring(LOCATION_PREFIX.length()));
locationBuilder.append(" ");
}
}
location = locationBuilder.toString();
} else {
location = i.stringValue(this.classification_data.attribute(Storage.LOCATION));
}
final Object[] values = { id, String.format(Constants.twitter_user_intent, id), location, i.stringValue(attribute_lang), i.stringValue(attribute_utc_offset), i.stringValue(attribute_timezone), this.training_data.classAttribute().value((int) classification) };
logger.debug("Classification - {} -> {}", i.toString(), values[6]);
if (csvFilePrinter != null) {
try {
csvFilePrinter.printRecord(values);
} catch (IOException e) {
logger.error("Error while printing CSV records for ID {}", id, e);
}
}
}
IOUtils.closeQuietly(fileWriter);
IOUtils.closeQuietly(csvFilePrinter);
}Example 41
| Project: solr-scale-tk-master File: FusionPipelineClient.java View source code |
public void writeTo(OutputStream outputStream) throws IOException {
try (OutputStreamWriter osw = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
CSVPrinter csvPrinter = new CSVPrinter(osw, CSVFormat.DEFAULT);
for (Object doc : docs) {
if (doc instanceof Map) {
Map<String, Object> map = (Map<String, Object>) doc;
csvPrinter.printRecord(map.values());
} else {
csvPrinter.print(doc);
csvPrinter.println();
}
}
csvPrinter.flush();
csvPrinter.close();
}
}Example 42
| Project: tradelib-master File: StrategyText.java View source code |
public static List<InstrumentText> buildList(Connection con, String strategy, LocalDate date, String csvPath, char csvSep) throws Exception {
// public static List<InstrumentText> buildList(Connection con, String strategy, LocalDate date) throws Exception {
ArrayList<InstrumentText> result = new ArrayList<InstrumentText>();
CSVPrinter printer = null;
if (csvPath != null) {
// Add withHeader for headers
printer = CSVFormat.DEFAULT.withDelimiter(csvSep).print(new BufferedWriter(new FileWriter(csvPath)));
}
int numCsvColumns = 12;
int rollMethod = 2;
DatabaseMetaData dmd = con.getMetaData();
String driverName = dmd.getDriverName();
String query = "";
if (driverName.startsWith("MySQL")) {
query = STRATEGY_QUERY_MYSQL;
} else {
query = STRATEGY_QUERY;
}
String prevCategory = "";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, strategy);
pstmt.setTimestamp(2, Timestamp.valueOf(date.atStartOfDay()));
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String category = rs.getString(2);
if (!category.equals(prevCategory)) {
result.add(InstrumentText.makeSection(category));
prevCategory = category;
if (printer != null) {
printer.print(category);
for (int ii = 1; ii < numCsvColumns; ++ii) {
printer.print("");
}
printer.println();
}
}
String name = rs.getString(3);
String symbol = rs.getString(4);
String contract = "";
if (rollMethod == 1) {
// Uses current_contract and trading_days
int ndays = rs.getInt(12);
if (ndays > 1) {
contract = rs.getString(10);
} else {
contract = "Roll to " + rs.getString(11);
}
} else if (rollMethod == 2) {
// Uses current_contract2 and roll_today
int rollToday = rs.getInt(14);
if (rollToday == 0) {
contract = rs.getString(13);
} else {
contract = "Roll to " + rs.getString(13);
}
}
if (printer != null) {
printer.print(name);
printer.print(symbol);
printer.print(contract);
}
String signal;
long position = (long) rs.getDouble(5);
JsonObject jo = new Gson().fromJson(rs.getString(9), JsonObject.class);
if (position > 0.0) {
BigDecimal entryPrice;
double pnl;
try {
entryPrice = jo.get("entry_price").getAsBigDecimal();
pnl = jo.get("pnl").getAsDouble();
} catch (Exception e) {
entryPrice = BigDecimal.valueOf(Double.MIN_VALUE);
pnl = Double.MIN_VALUE;
}
signal = String.format("Long [%d] since %s [at %s].", position, rs.getString(6), formatBigDecimal(entryPrice));
if (printer != null)
printer.print(signal);
String openProfit = String.format("Open equity profit %,d.", (int) Math.floor(pnl));
signal += " " + openProfit;
if (printer != null)
printer.print(openProfit);
} else if (position < 0.0) {
BigDecimal entryPrice;
double pnl;
try {
entryPrice = jo.get("entry_price").getAsBigDecimal();
pnl = jo.get("pnl").getAsDouble();
} catch (Exception e) {
entryPrice = BigDecimal.valueOf(-1);
pnl = -1;
}
signal = String.format("Short [%d] since %s [at %s].", Math.abs(position), rs.getString(6), formatBigDecimal(entryPrice));
if (printer != null)
printer.print(signal);
String openProfit = String.format("Open equity profit %,d.", (int) Math.floor(pnl));
signal += " " + openProfit;
if (printer != null)
printer.print(openProfit);
} else {
signal = "Out.";
if (printer != null) {
printer.print(signal);
// An empty column follows the status if there is no position - there is no profit.
printer.print("");
}
}
boolean hasOrder = false;
JsonArray ja = jo.get("orders").getAsJsonArray();
double entryRisk;
try {
entryRisk = jo.get("entry_risk").getAsDouble();
} catch (Exception ee) {
entryRisk = Double.NaN;
}
String profitTarget;
Double profitTargetDbl;
try {
profitTarget = formatBigDecimal(jo.get("profit_target").getAsBigDecimal());
profitTargetDbl = jo.get("profit_target").getAsDouble();
} catch (Exception ee) {
profitTarget = null;
profitTargetDbl = null;
}
String stopLoss;
Double stopLossDbl;
try {
stopLoss = formatBigDecimal(jo.get("stop_loss").getAsBigDecimal());
stopLossDbl = jo.get("stop_loss").getAsDouble();
} catch (Exception ee) {
stopLoss = null;
stopLossDbl = null;
}
Double lastClose;
try {
lastClose = jo.get("last_close").getAsDouble();
} catch (Exception ee) {
lastClose = null;
}
// Currently maximum one entry and maximum one exit are supported.
String entryStr = "";
String exitStr = "";
String contractRiskStr = "";
for (int ii = 0; ii < ja.size(); ++ii) {
JsonObject jorder = ja.get(ii).getAsJsonObject();
switch(jorder.get("type").getAsString()) {
case "EXIT_LONG_STOP":
exitStr = "Exit long at stop " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + ".";
signal += " " + exitStr;
break;
case "EXIT_SHORT_STOP":
exitStr = "Exit short at stop " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + ".";
signal += " " + exitStr;
break;
case "ENTER_LONG":
if (!Double.isNaN(entryRisk)) {
entryStr = String.format("Enter long at open. Contract risk is %s.", formatDouble(entryRisk, 0, 0));
signal += " " + entryStr;
} else {
entryStr = "Enter long at open.";
signal += " " + entryStr;
}
break;
case "ENTER_SHORT":
if (!Double.isNaN(entryRisk)) {
entryStr = String.format("Enter short at open. Contract risk is %s.", formatDouble(entryRisk, 0, 0));
signal += " " + entryStr;
} else {
entryStr = "Enter short at open.";
signal += " " + entryStr;
}
break;
case "ENTER_LONG_STOP":
position = jorder.get("quantity").getAsLong();
entryStr = String.format("Enter long [%d] at stop %s [%s%%].", position, formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()), formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
signal += " " + entryStr;
if (!Double.isNaN(entryRisk)) {
contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
signal += " " + contractRiskStr;
}
break;
case "ENTER_LONG_STOP_LIMIT":
position = jorder.get("quantity").getAsLong();
entryStr = String.format("Enter long [%d] at limit %s, stop at %s [%s%%].", position, formatBigDecimal(jorder.get("limit_price").getAsBigDecimal()), formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()), formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
signal += " " + entryStr;
if (!Double.isNaN(entryRisk)) {
contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
signal += contractRiskStr;
}
break;
case "ENTER_SHORT_STOP":
// signal += " Enter short at stop " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + ".";
position = jorder.get("quantity").getAsLong();
entryStr = String.format("Enter short [%d] at stop %s [%s%%].", Math.abs(position), formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()), formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
signal += " " + entryStr;
if (!Double.isNaN(entryRisk)) {
contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
signal += " " + contractRiskStr;
}
break;
case "ENTER_SHORT_STOP_LIMIT":
position = jorder.get("quantity").getAsLong();
entryStr = String.format("Enter short [%d] at limit %s, stop at %s [%s%%].", Math.abs(position), formatBigDecimal(jorder.get("limit_price").getAsBigDecimal()), formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()), formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
signal += " " + entryStr;
if (!Double.isNaN(entryRisk)) {
contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
signal += " " + contractRiskStr;
}
break;
case "EXIT_LONG":
exitStr = "Exit long at open.";
signal += " " + exitStr;
break;
case "EXIT_SHORT":
exitStr = "Exit short at open.";
signal += " " + exitStr;
break;
case "EXIT_SHORT_STOP_LIMIT":
exitStr = "Exit short at limit " + formatBigDecimal(jorder.get("limit_price").getAsBigDecimal()) + ", stop at " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + " [" + formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100) + "%]" + ".";
signal += " " + exitStr;
break;
case "EXIT_LONG_STOP_LIMIT":
exitStr = "Exit long at limit " + formatBigDecimal(jorder.get("limit_price").getAsBigDecimal()) + ", stop at " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + " [" + formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100) + "%]" + ".";
signal += " " + exitStr;
break;
}
hasOrder = true;
}
String lastCloseStr = "Last close at " + formatBigDecimal(jo.get("last_close").getAsBigDecimal()) + ".";
String stopLossStr = "";
String profitTargetStr = "";
if (hasOrder) {
signal += " " + lastCloseStr;
}
if (stopLoss != null) {
stopLossStr = "Stop loss at " + stopLoss;
if (lastClose != null && stopLossDbl != null) {
stopLossStr += " [" + formatPercentage(stopLossDbl / lastClose * 100 - 100) + "%]";
}
stopLossStr += ".";
signal += " " + stopLossStr;
}
if (profitTarget != null) {
profitTargetStr = "Profit target at about " + profitTarget;
if (profitTargetDbl != null && lastClose != null) {
profitTargetStr += " [" + formatPercentage(profitTargetDbl / lastClose * 100 - 100) + "%]";
}
profitTargetStr += ".";
signal += " " + profitTargetStr;
}
if (printer != null) {
printer.print(exitStr);
printer.print(entryStr);
printer.print(contractRiskStr);
printer.print(lastCloseStr);
printer.print(stopLossStr);
printer.print(profitTargetStr);
printer.println();
}
result.add(InstrumentText.make(name, symbol, contract, signal));
}
rs.close();
pstmt.close();
if (printer != null)
printer.flush();
return result;
}Example 43
| Project: webanno-master File: AgreementUtils.java View source code |
public static void toCSV(CSVPrinter aOut, AgreementResult aAgreement) throws IOException {
try {
aOut.printComment(String.format("Category count: %d%n", aAgreement.getStudy().getCategoryCount()));
} catch (Throwable e) {
aOut.printComment(String.format("Category count: %s%n", ExceptionUtils.getRootCauseMessage(e)));
}
try {
aOut.printComment(String.format("Item count: %d%n", aAgreement.getStudy().getItemCount()));
} catch (Throwable e) {
aOut.printComment(String.format("Item count: %s%n", ExceptionUtils.getRootCauseMessage(e)));
}
aOut.printComment(String.format("Relevant position count: %d%n", aAgreement.getRelevantSetCount()));
// aOut.printf("%n== Complete sets: %d ==%n", aAgreement.getCompleteSets().size());
configurationSetsWithItemsToCsv(aOut, aAgreement, aAgreement.getCompleteSets());
//
// aOut.printf("%n== Incomplete sets (by position): %d == %n", aAgreement.getIncompleteSetsByPosition().size());
// dumpAgreementConfigurationSets(aOut, aAgreement, aAgreement.getIncompleteSetsByPosition());
//
// aOut.printf("%n== Incomplete sets (by label): %d ==%n", aAgreement.getIncompleteSetsByLabel().size());
// dumpAgreementConfigurationSets(aOut, aAgreement, aAgreement.getIncompleteSetsByLabel());
//
// aOut.printf("%n== Plurality sets: %d ==%n", aAgreement.getPluralitySets().size());
// dumpAgreementConfigurationSets(aOut, aAgreement, aAgreement.getPluralitySets());
}Example 44
| Project: myria-master File: CsvTupleWriter.java View source code |
@Override
public void open(final OutputStream out) throws IOException {
csvPrinter = new CSVPrinter(new BufferedWriter(new OutputStreamWriter(out)), csvFormat);
}Example 45
| Project: find-master File: CsvExportStrategy.java View source code |
@Override
public void exportRecord(final OutputStream outputStream, final Iterable<String> values) throws IOException {
try (final CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(outputStream), CSVFormat.EXCEL)) {
csvPrinter.printRecord(values);
}
}