Search in sources :

Example 6 with ICSVWriter

use of com.opencsv.ICSVWriter 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)

Example 7 with ICSVWriter

use of com.opencsv.ICSVWriter in project zachtronics-leaderboard-bot by F43nd1r.

the class SzManualTest method bootstrapPsv.

@Test
public void bootstrapPsv() throws IOException {
    Path repoPath = Paths.get("../shenzhenIO/leaderboard");
    for (SzPuzzle puzzle : SzPuzzle.values()) {
        if (puzzle.getType() != SzType.STANDARD)
            continue;
        Path indexPath = repoPath.resolve(puzzle.getGroup().getRepoFolder()).resolve(puzzle.getId()).resolve("solutions.psv");
        try (ICSVWriter writer = new CSVWriterBuilder(Files.newBufferedWriter(indexPath)).withSeparator('|').build()) {
            Map<SzScore, CategoryRecord<SzRecord, SzCategory>> scoreMap = repository.findCategoryHolders(puzzle, true).stream().collect(Collectors.toMap(cr -> cr.getRecord().getScore(), Function.identity(), (cr1, cr2) -> {
                cr1.getCategories().addAll(cr2.getCategories());
                return cr1;
            }, () -> new TreeMap<>(SzCategory.CP.getScoreComparator())));
            for (CategoryRecord<SzRecord, SzCategory> cr : scoreMap.values()) {
                SzRecord record = cr.getRecord();
                String author = record.getAuthor();
                String categories = cr.getCategories().stream().map(SzCategory::name).sorted().collect(Collectors.joining(","));
                String[] csvRecord = new String[] { record.getScore().toDisplayString(), author, categories };
                writer.writeNext(csvRecord, false);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DisplayContext(com.faendir.zachtronics.bot.model.DisplayContext) Subreddit(com.faendir.zachtronics.bot.reddit.Subreddit) Files(java.nio.file.Files) BotTest(com.faendir.zachtronics.bot.BotTest) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) com.faendir.zachtronics.bot.sz.model(com.faendir.zachtronics.bot.sz.model) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) TreeMap(java.util.TreeMap) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) Paths(java.nio.file.Paths) Map(java.util.Map) RedditService(com.faendir.zachtronics.bot.reddit.RedditService) ICSVWriter(com.opencsv.ICSVWriter) Path(java.nio.file.Path) CSVWriterBuilder(com.opencsv.CSVWriterBuilder) CSVWriterBuilder(com.opencsv.CSVWriterBuilder) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) TreeMap(java.util.TreeMap) ICSVWriter(com.opencsv.ICSVWriter) BotTest(com.faendir.zachtronics.bot.BotTest) Test(org.junit.jupiter.api.Test)

Aggregations

ICSVWriter (com.opencsv.ICSVWriter)7 CSVWriterBuilder (com.opencsv.CSVWriterBuilder)5 IOException (java.io.IOException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 BotTest (com.faendir.zachtronics.bot.BotTest)2 CSVParserWriter (com.opencsv.CSVParserWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 Test (org.junit.jupiter.api.Test)2 DisplayContext (com.faendir.zachtronics.bot.model.DisplayContext)1 RedditService (com.faendir.zachtronics.bot.reddit.RedditService)1 Subreddit (com.faendir.zachtronics.bot.reddit.Subreddit)1 CategoryRecord (com.faendir.zachtronics.bot.repository.CategoryRecord)1 com.faendir.zachtronics.bot.sz.model (com.faendir.zachtronics.bot.sz.model)1 CSVWriter (com.opencsv.CSVWriter)1 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