Search in sources :

Example 1 with CsvEncoder

use of org.spf4j.avro.csv.CsvEncoder in project spf4j by zolyfarkas.

the class MStoreViewJInternalFrame method exportButtonActionPerformed.

// GEN-LAST:event_plotButtonActionPerformed
@SuppressFBWarnings("UP_UNUSED_PARAMETER")
private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_exportButtonActionPerformed
    TreePath[] selectionPaths = measurementTree.getSelectionPaths();
    Set<Schema> selectedTables = getSelectedTables(selectionPaths);
    if (!selectedTables.isEmpty()) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            for (Schema metric : selectedTables) {
                try (AvroCloseableIterable<TimeSeriesRecord> obs = reader.getMeasurementData(metric, Instant.EPOCH, Instant.now())) {
                    CsvWriter writer = Csv.CSV.writer(Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8));
                    CsvEncoder encoder = new CsvEncoder(writer, Schema.createArray(metric));
                    encoder.writeHeader();
                    GenericDatumWriter dw = new GenericDatumWriter(metric);
                    for (TimeSeriesRecord o : obs) {
                        dw.write(o, encoder);
                    }
                    encoder.flush();
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
        }
    }
}
Also used : CsvWriter(org.spf4j.io.csv.CsvWriter) Schema(org.apache.avro.Schema) UncheckedIOException(java.io.UncheckedIOException) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TreePath(javax.swing.tree.TreePath) JFileChooser(javax.swing.JFileChooser) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) File(java.io.File) CsvEncoder(org.spf4j.avro.csv.CsvEncoder) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 JFileChooser (javax.swing.JFileChooser)1 TreePath (javax.swing.tree.TreePath)1 Schema (org.apache.avro.Schema)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 CsvEncoder (org.spf4j.avro.csv.CsvEncoder)1 CsvWriter (org.spf4j.io.csv.CsvWriter)1 TimeSeriesRecord (org.spf4j.perf.TimeSeriesRecord)1