Search in sources :

Example 1 with CSVParserWriter

use of com.opencsv.CSVParserWriter in project tribuo by oracle.

the class CSVSaver method save.

/**
 * Saves the dataset to the specified path.
 * @param csvPath The path to save to.
 * @param dataset The dataset to save.
 * @param responseNames The response names set.
 * @param <T> The output type.
 * @throws IOException If the disk write failed.
 */
public <T extends Output<T>> void save(Path csvPath, Dataset<T> dataset, Set<String> responseNames) throws IOException {
    boolean isMultiOutput = responseNames.size() > 1;
    ImmutableFeatureMap features = dataset.getFeatureIDMap();
    int ncols = features.size() + responseNames.size();
    // 
    // Initialize the CSV header row.
    String[] headerLine = new String[ncols];
    Map<String, Integer> responseToColumn = new HashMap<>();
    int col = 0;
    for (String response : responseNames) {
        headerLine[col] = response;
        responseToColumn.put(response, col);
        col++;
    }
    for (int i = 0; i < features.size(); i++) {
        headerLine[col++] = features.get(i).getName();
    }
    // Write the CSV
    try (ICSVWriter writer = new CSVParserWriter(Files.newBufferedWriter(csvPath, StandardCharsets.UTF_8), new RFC4180ParserBuilder().withSeparator(separator).withQuoteChar(quote).build(), "\n")) {
        writer.writeNext(headerLine);
        for (Example<T> e : dataset) {
            String[] denseOutput = (isMultiOutput) ? densifyMultiOutput(e, responseToColumn) : densifySingleOutput(e);
            String[] featureArr = generateFeatureArray(e, features);
            if (featureArr.length != features.size()) {
                throw new IllegalStateException(String.format("Invalid example: had %d features, expected %d.", featureArr.length, features.size()));
            }
            // 
            // Copy responses and features into a single array
            String[] line = new String[ncols];
            System.arraycopy(denseOutput, 0, line, 0, denseOutput.length);
            System.arraycopy(featureArr, 0, line, denseOutput.length, featureArr.length);
            writer.writeNext(line);
        }
    }
}
Also used : HashMap(java.util.HashMap) RFC4180ParserBuilder(com.opencsv.RFC4180ParserBuilder) ICSVWriter(com.opencsv.ICSVWriter) ImmutableFeatureMap(org.tribuo.ImmutableFeatureMap) CSVParserWriter(com.opencsv.CSVParserWriter)

Example 2 with CSVParserWriter

use of com.opencsv.CSVParserWriter in project tribuo by oracle.

the class SQLToCSV method main.

/**
 * Reads an SQL query from the standard input and writes the results of the
 * query to the standard output.
 *
 * @param args Single arg is the JDBC connection string.
 */
public static void main(String[] args) {
    LabsLogFormatter.setAllLogFormatters();
    SQLToCSVOptions opts = new SQLToCSVOptions();
    ConfigurationManager cm;
    try {
        cm = new ConfigurationManager(args, opts);
    } catch (UsageException e) {
        logger.info(e.getUsage());
        System.exit(1);
    }
    if (opts.dbConfig == null) {
        if (opts.connString == null) {
            logger.log(Level.SEVERE, "Must specify connection string with -n");
            System.exit(1);
        }
        if (opts.username != null || opts.password != null) {
            if (opts.username == null || opts.password == null) {
                logger.log(Level.SEVERE, "Must specify both of user and password with -u, -p if one is specified!");
                System.exit(1);
            }
        }
    } else if (opts.username != null || opts.password != null || opts.connString != null) {
        logger.warning("dbConfig provided but username/password/connstring also provided. Options from -u, -p, -n being ignored");
    }
    String query;
    try (BufferedReader br = opts.inputPath != null ? Files.newBufferedReader(opts.inputPath) : new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
        StringBuilder qsb = new StringBuilder();
        String l;
        while ((l = br.readLine()) != null) {
            qsb.append(l);
            qsb.append("\n");
        }
        query = qsb.toString().trim();
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "Error reading query: " + ex);
        System.exit(1);
        return;
    }
    if (query.isEmpty()) {
        logger.log(Level.SEVERE, "Query is empty string");
        System.exit(1);
    }
    Connection conn = null;
    try {
        if (opts.dbConfig != null) {
            conn = opts.dbConfig.getConnection();
        } else if (opts.username != null) {
            conn = DriverManager.getConnection(opts.connString, opts.username, opts.password);
        } else {
            conn = DriverManager.getConnection(opts.connString);
        }
    } catch (SQLException ex) {
        logger.log(Level.SEVERE, "Can't connect to database: " + opts.connString, ex);
        System.exit(1);
    }
    try (Statement stmt = conn.createStatement()) {
        stmt.setFetchSize(1000);
        stmt.setFetchDirection(ResultSet.FETCH_FORWARD);
        ResultSet results;
        try {
            results = stmt.executeQuery(query);
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "Error running query", ex);
            try {
                conn.close();
            } catch (SQLException ex1) {
                logger.log(Level.SEVERE, "Failed to close connection", ex1);
            }
            return;
        }
        try (ICSVWriter writer = new CSVParserWriter(opts.outputPath != null ? Files.newBufferedWriter(opts.outputPath) : new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8), 1024 * 1024), new RFC4180Parser(), "\n")) {
            writer.writeAll(results, true);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, "Error writing CSV", ex);
            System.exit(1);
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "Error retrieving results", ex);
            System.exit(1);
        }
    } catch (SQLException ex) {
        logger.log(Level.SEVERE, "Couldn't create statement", ex);
        try {
            conn.close();
        } catch (SQLException ex1) {
            logger.log(Level.SEVERE, "Failed to close connection", ex1);
        }
        System.exit(1);
        return;
    }
    try {
        conn.close();
    } catch (SQLException ex1) {
        logger.log(Level.SEVERE, "Failed to close connection", ex1);
    }
}
Also used : UsageException(com.oracle.labs.mlrg.olcut.config.UsageException) InputStreamReader(java.io.InputStreamReader) RFC4180Parser(com.opencsv.RFC4180Parser) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ICSVWriter(com.opencsv.ICSVWriter) BufferedReader(java.io.BufferedReader) ResultSet(java.sql.ResultSet) CSVParserWriter(com.opencsv.CSVParserWriter) OutputStreamWriter(java.io.OutputStreamWriter) ConfigurationManager(com.oracle.labs.mlrg.olcut.config.ConfigurationManager)

Aggregations

CSVParserWriter (com.opencsv.CSVParserWriter)2 ICSVWriter (com.opencsv.ICSVWriter)2 RFC4180Parser (com.opencsv.RFC4180Parser)1 RFC4180ParserBuilder (com.opencsv.RFC4180ParserBuilder)1 ConfigurationManager (com.oracle.labs.mlrg.olcut.config.ConfigurationManager)1 UsageException (com.oracle.labs.mlrg.olcut.config.UsageException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 ImmutableFeatureMap (org.tribuo.ImmutableFeatureMap)1