use of herddb.jdbc.HerdDBDataSource in project herddb by diennea.
the class HerdDBCLI method main.
public static void main(String... args) throws IOException {
try {
DefaultParser parser = new DefaultParser();
Options options = new Options();
options.addOption("x", "url", true, "JDBC URL");
options.addOption("u", "username", true, "JDBC Username");
options.addOption("pwd", "password", true, "JDBC Password");
options.addOption("q", "query", true, "Execute inline query");
options.addOption("v", "verbose", false, "Verbose output");
options.addOption("a", "async", false, "Use (experimental) executeBatchAsync for sending DML");
options.addOption("s", "schema", true, "Default tablespace (SQL schema)");
options.addOption("fi", "filter", true, "SQL filter mode: all|ddl|dml");
options.addOption("f", "file", true, "SQL Script to execute (statement separated by 'GO' lines)");
options.addOption("at", "autotransaction", false, "Execute scripts in autocommit=false mode and commit automatically");
options.addOption("atbs", "autotransactionbatchsize", true, "Batch size for 'autotransaction' mode");
options.addOption("g", "script", true, "Groovy Script to execute");
options.addOption("i", "ignoreerrors", false, "Ignore SQL Errors during file execution");
options.addOption("sc", "sqlconsole", false, "Execute SQL console in interactive mode");
options.addOption("nsch", "nosqlconsolehistory", false, "Disable SQL console history");
options.addOption("fmd", "mysql", false, "Intruct the parser that the script is coming from a MySQL Dump");
options.addOption("rwst", "rewritestatements", false, "Rewrite all statements to use JDBC parameters");
options.addOption("b", "backup", false, "Backup one or more tablespaces (selected with --schema)");
options.addOption("r", "restore", false, "Restore tablespace");
options.addOption("nl", "newleader", true, "Leader for new restored tablespace");
options.addOption("ns", "newschema", true, "Name for new restored tablespace");
options.addOption("tsm", "tablespacemapper", true, "Path to groovy script with a custom functin to map table names to tablespaces");
options.addOption("dfs", "dumpfetchsize", true, "Fetch size for dump operations. Defaults to chunks of 100000 records");
options.addOption("n", "nodeid", true, "Node id");
options.addOption("t", "table", true, "Table name");
options.addOption("p", "param", true, "Parameter name");
options.addOption("val", "values", true, "Parameter values");
options.addOption("lts", "list-tablespaces", false, "List available tablespaces");
options.addOption("ln", "list-nodes", false, "List available nodes");
options.addOption("sts", "show-tablespace", false, "Show full informations about a tablespace (needs -s option)");
options.addOption("lt", "list-tables", false, "List tablespace tables (needs -s option)");
options.addOption("st", "show-table", false, "Show full informations about a table (needs -s and -t options)");
options.addOption("sl", "set-leader", false, "Set the leader for a tablespace (needs -s and -nl options)");
options.addOption("ar", "add-replica", false, "Add a replica to the tablespace (needs -s and -r options)");
options.addOption("rr", "remove-replica", false, "Remove a replica from the tablespace (needs -s and -r options)");
options.addOption("adt", "create-tablespace", false, "Create a tablespace (needs -ns and -nl options)");
options.addOption("at", "alter-tablespace", false, "Alter a tablespace (needs -s, -param and --values options)");
options.addOption("d", "describe", false, "Checks and describes a raw file");
options.addOption("ft", "filetype", true, "Checks and describes a raw file (valid options are txlog, datapage, tablecheckpoint, indexcheckpoint, tablesmetadata, bkledger");
options.addOption("mdf", "metadatafile", true, "Tables metadata file, required for 'datapage' filetype");
options.addOption("tsui", "tablespaceuuid", true, "Tablespace UUID, used for describing raw files");
options.addOption("lid", "ledgerid", true, "Ledger ID on BookKeeper");
options.addOption("fromid", "fromid", true, "Starting entry Id for filetype=bkledger, default to 0");
options.addOption("toid", "toid", true, "Starting entry Id for filetype=bkledger, default to LastAddConfirmed");
org.apache.commons.cli.CommandLine commandLine;
try {
commandLine = parser.parse(options, args);
} catch (ParseException error) {
println("Syntax error: " + error);
failAndPrintHelp(options);
return;
}
if (args.length == 0) {
failAndPrintHelp(options);
return;
}
String schema = commandLine.getOptionValue("schema", TableSpace.DEFAULT);
String tablespaceuuid = commandLine.getOptionValue("tablespaceuuid", "");
final boolean verbose = commandLine.hasOption("verbose");
final boolean async = commandLine.hasOption("async");
final String filter = commandLine.getOptionValue("filter", "all");
if (!verbose) {
LogManager.getLogManager().reset();
}
String file = commandLine.getOptionValue("file", "");
String tablesmetadatafile = commandLine.getOptionValue("metadatafile", "");
String table = commandLine.getOptionValue("table", "");
boolean describe = commandLine.hasOption("describe");
String filetype = commandLine.getOptionValue("filetype", "");
long ledgerId = Long.parseLong(commandLine.getOptionValue("ledgerid", "0"));
long fromId = Long.parseLong(commandLine.getOptionValue("fromid", "0"));
// -1 = LAC
long toId = Long.parseLong(commandLine.getOptionValue("toid", "-1"));
String url = commandLine.getOptionValue("url", "jdbc:herddb:server:localhost:7000");
String username = commandLine.getOptionValue("username", ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT);
String password = commandLine.getOptionValue("password", ClientConfiguration.PROPERTY_CLIENT_PASSWORD_DEFAULT);
if (describe) {
try {
if (filetype.equals("bkledger")) {
try (HerdDBDataSource datasource = new HerdDBDataSource()) {
datasource.setUrl(url);
describeRawLedger(ledgerId, fromId, toId, datasource);
}
} else {
if (file.isEmpty()) {
throw new IllegalArgumentException("file option is required");
}
describeRawFile(tablespaceuuid, table, tablesmetadatafile, file, filetype);
}
} catch (Exception error) {
prettyPrintException(verbose, error);
exitCode = 1;
}
return;
}
String query = commandLine.getOptionValue("query", "");
boolean backup = commandLine.hasOption("backup");
boolean restore = commandLine.hasOption("restore");
String newschema = commandLine.getOptionValue("newschema", "");
String leader = commandLine.getOptionValue("newleader", "");
String script = commandLine.getOptionValue("script", "");
String tablespacemapperfile = commandLine.getOptionValue("tablespacemapper", "");
int dumpfetchsize = Integer.parseInt(commandLine.getOptionValue("dumpfetchsize", 100000 + ""));
final boolean ignoreerrors = commandLine.hasOption("ignoreerrors");
final boolean sqlconsole = commandLine.hasOption("sqlconsole");
final boolean persistSqlConsoleHistory = !commandLine.hasOption("nosqlconsolehistory");
final boolean frommysqldump = commandLine.hasOption("mysql");
final boolean rewritestatements = commandLine.hasOption("rewritestatements") || !tablespacemapperfile.isEmpty() || frommysqldump;
boolean autotransaction = commandLine.hasOption("autotransaction") || frommysqldump;
int autotransactionbatchsize = Integer.parseInt(commandLine.getOptionValue("autotransactionbatchsize", 100000 + ""));
if (!autotransaction) {
autotransactionbatchsize = 0;
}
String nodeId = commandLine.getOptionValue("nodeid", "");
String param = commandLine.getOptionValue("param", "");
String values = commandLine.getOptionValue("values", "");
boolean listTablespaces = commandLine.hasOption("list-tablespaces");
boolean listNodes = commandLine.hasOption("list-nodes");
boolean showTablespace = commandLine.hasOption("show-tablespace");
boolean listTables = commandLine.hasOption("list-tables");
boolean showTable = commandLine.hasOption("show-table");
if (showTable) {
if (table.equals("")) {
println("Specify the table (-t <table>)");
exitCode = 1;
System.exit(exitCode);
}
}
boolean createTablespace = commandLine.hasOption("create-tablespace");
if (createTablespace) {
if (newschema.equals("")) {
println("Specify the tablespace name (--newschema <schema>)");
exitCode = 1;
System.exit(exitCode);
}
if (leader.equals("")) {
println("Specify the leader node (--newleader <nodeid>)");
exitCode = 1;
System.exit(exitCode);
}
}
boolean alterTablespace = commandLine.hasOption("alter-tablespace");
if (alterTablespace) {
if (commandLine.getOptionValue("schema", null) == null) {
println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
exitCode = 1;
System.exit(exitCode);
}
if (param.equals("")) {
println("Specify the parameter (--param <par>)");
exitCode = 1;
System.exit(exitCode);
}
if (values.equals("")) {
println("Specify values (--values <vals>)");
exitCode = 1;
System.exit(exitCode);
}
}
boolean setLeader = commandLine.hasOption("set-leader");
if (setLeader) {
if (leader.isEmpty()) {
println("Specify the node (-nl <nodeid>)");
exitCode = 1;
System.exit(exitCode);
}
if (commandLine.getOptionValue("schema", null) == null) {
println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
exitCode = 1;
System.exit(exitCode);
}
}
boolean addReplica = commandLine.hasOption("add-replica");
if (addReplica) {
if (commandLine.getOptionValue("schema", null) == null) {
println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
exitCode = 1;
System.exit(exitCode);
}
if (nodeId.equals("")) {
println("Specify the node (-n <nodeid>)");
exitCode = 1;
System.exit(exitCode);
}
}
boolean removeReplica = commandLine.hasOption("remove-replica");
if (removeReplica) {
if (commandLine.getOptionValue("schema", null) == null) {
println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s " + TableSpace.DEFAULT + "\" instead");
exitCode = 1;
System.exit(exitCode);
}
if (nodeId.equals("")) {
println("Specify the node (-n <nodeid>)");
exitCode = 1;
System.exit(exitCode);
}
}
TableSpaceMapper tableSpaceMapper = buildTableSpaceMapper(tablespacemapperfile);
ZookeeperMetadataStorageManager metadataStorageManager = null;
try (HerdDBDataSource datasource = new HerdDBDataSource()) {
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
try (Connection connection = datasource.getConnection();
Statement statement = connection.createStatement()) {
metadataStorageManager = buildMetadataStorageManager(datasource);
connection.setSchema(schema);
if (sqlconsole) {
runSqlConsole(statement, PRETTY_PRINT, verbose, persistSqlConsoleHistory);
} else if (backup) {
performBackup(statement, schema, file, options, connection, dumpfetchsize);
} else if (restore) {
performRestore(file, leader, newschema, options, statement, connection);
} else if (!query.isEmpty()) {
executeStatement(verbose, ignoreerrors, false, false, query, statement, tableSpaceMapper, false, PRETTY_PRINT);
} else if (!file.isEmpty()) {
executeSqlFile(autotransactionbatchsize, connection, file, verbose, async, ignoreerrors, frommysqldump, rewritestatements, statement, tableSpaceMapper, PRETTY_PRINT, filter, datasource);
} else if (!script.isEmpty()) {
executeScript(connection, datasource, statement, script);
} else if (listTablespaces) {
printTableSpaces(verbose, ignoreerrors, statement, tableSpaceMapper, metadataStorageManager);
} else if (listNodes) {
printNodes(verbose, ignoreerrors, statement, tableSpaceMapper, metadataStorageManager);
} else if (showTablespace) {
printTableSpaceInfos(verbose, ignoreerrors, statement, tableSpaceMapper, schema, metadataStorageManager);
} else if (listTables) {
listTables(verbose, ignoreerrors, statement, tableSpaceMapper, schema);
} else if (showTable) {
printTableInfos(verbose, ignoreerrors, statement, tableSpaceMapper, schema, table);
} else if (setLeader) {
setLeader(metadataStorageManager, schema, leader);
} else if (addReplica) {
changeReplica(metadataStorageManager, schema, nodeId, ChangeReplicaAction.ADD);
} else if (removeReplica) {
changeReplica(metadataStorageManager, schema, nodeId, ChangeReplicaAction.REMOVE);
} else if (createTablespace) {
createTablespace(verbose, ignoreerrors, statement, tableSpaceMapper, newschema, leader);
} else if (alterTablespace) {
alterTablespace(metadataStorageManager, schema, param, values);
} else {
failAndPrintHelp(options);
return;
}
}
exitCode = 0;
} catch (Exception error) {
prettyPrintException(verbose, error);
exitCode = 1;
} finally {
if (metadataStorageManager != null) {
try {
metadataStorageManager.close();
} catch (MetadataStorageManagerException ex) {
}
}
}
} finally {
System.exit(exitCode);
}
}
use of herddb.jdbc.HerdDBDataSource in project herddb by diennea.
the class APIResource method login.
@SuppressFBWarnings({ "J2EE_STORE_OF_NON_SERIALIZABLE_OBJECT_INTO_SESSION" })
@POST
@Path("/login")
public Map<String, Object> login(@FormParam("datasource") String ds, @FormParam("username") String username, @FormParam("password") String pwd, @FormParam("defaultts") String defaultts) {
Map<String, Object> res = new HashMap<>();
res.put("defaultts", defaultts);
HerdDBDataSource da = new HerdDBDataSource();
da.setUrl(ds);
da.setUsername(username);
da.setPassword(pwd);
try (Connection conn = da.getConnection();
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT count(*) from " + defaultts + ".systables")) {
res.put("ok", true);
HttpSession session = servletRequest.getSession(true);
session.setMaxInactiveInterval(60 * 5);
session.setAttribute("datasource", da);
session.setAttribute("defaultts", defaultts);
session.setAttribute("jdbcurl", ds);
} catch (SQLException | IllegalArgumentException e) {
LOG.log(Level.SEVERE, "error", e);
res.put("ok", false);
if (e.getMessage().contains(UnknownHostException.class.getName()) || e.getMessage().contains("invalid url")) {
res.put("errormessage", "JDBC URL is not correct. The host " + ds + " is unreachable.");
} else if (e.getMessage().contains("Authentication failed")) {
res.put("errormessage", "Username and password are not correct.");
} else if (e.getMessage().contains(TableSpaceDoesNotExistException.class.getName())) {
res.put("errormessage", "Tablespace " + defaultts + " doesn't exist.");
}
res.put("sqlerror", e.getMessage());
}
return res;
}
Aggregations