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);
}
}
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);
}
}
}
}
Aggregations