Index: jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveConnection.java =================================================================== --- jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveConnection.java (revision 1508371) +++ jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveConnection.java (working copy) @@ -86,6 +86,7 @@ throw new SQLException("Invalid URL: " + uri, "08S01"); } + String database = "default"; // remove prefix uri = uri.substring(URI_PREFIX.length()); @@ -101,6 +102,9 @@ // parse uri // form: hostname:port/databasename String[] parts = uri.split("/"); + if(parts.length > 1) { + database = parts[1]; + } String[] hostport = parts[0].split(":"); int port = 10000; String host = hostport[0]; @@ -120,6 +124,7 @@ } isClosed = false; configureConnection(); + configureDatabase(database); } @@ -135,6 +140,12 @@ stmt.close(); } + private void configureDatabase(String database) throws SQLException { + Statement stmt = createStatement(); + stmt.execute("use " + database); + stmt.close(); + } + /* * (non-Javadoc) * Index: jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java =================================================================== --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java (revision 1508337) +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java (working copy) @@ -104,6 +104,7 @@ openSession(uri); configureConnection(connParams); + configureDatabase(connParams.getDbName()); } private void configureConnection(Utils.JdbcConnectionParams connParams) @@ -123,6 +124,12 @@ } } + private void configureDatabase(String database) throws SQLException { + Statement stmt = createStatement(); + stmt.execute("use " + database); + stmt.close(); + } + private void openTransport(String uri, String host, int port, Map sessConf ) throws SQLException { transport = new TSocket(host, port); Index: jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java =================================================================== --- jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java (revision 1508337) +++ jdbc/src/test/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java (working copy) @@ -49,6 +49,8 @@ */ public class TestJdbcDriver extends TestCase { private static final String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; + private static final String databaseName = "test_db"; + private static final String databaseComment = "Test database"; private static final String tableName = "testHiveJdbcDriver_Table"; private static final String tableComment = "Simple table"; private static final String viewName = "testHiveJdbcDriverView"; @@ -101,6 +103,26 @@ fail(ex.toString()); } + // drop database. ignore error. + try { + stmt.executeQuery("drop database " + databaseName); + } catch (Exception ex) { + fail(ex.toString()); + } + + // create database + ResultSet resDb = stmt.executeQuery("create database " + databaseName + + " comment '" + + databaseComment + "'"); + assertFalse(resDb.next()); + + if (standAloneServer) { + // re-open connection + con.close(); + con = DriverManager.getConnection("jdbc:hive://localhost:10000/" + databaseName, + "", ""); + } + // create table ResultSet res = stmt.executeQuery("create table " + tableName + " (under_col int comment 'the under column', value string) comment '" @@ -608,6 +630,24 @@ exceptionFound); } + public void testShowDatabases() throws SQLException { + Statement stmt = con.createStatement(); + assertNotNull("Statement is null", stmt); + + ResultSet res = stmt.executeQuery("show databases"); + + boolean testDatabaseExists = false; + while (res.next()) { + assertNotNull("table name is null in result set", res.getString(1)); + if (databaseName.equalsIgnoreCase(res.getString(1))) { + testDatabaseExists = true; + } + } + + assertTrue("database name " + databaseName + + " not found in SHOW DATABASES result set", testDatabaseExists); + } + public void testShowTables() throws SQLException { Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt); Index: jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java =================================================================== --- jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java (revision 1508337) +++ jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java (working copy) @@ -51,6 +51,8 @@ */ public class TestJdbcDriver2 extends TestCase { private static final String driverName = "org.apache.hive.jdbc.HiveDriver"; + private static final String databaseName = "test_db"; + private static final String databaseComment = "Test database"; private static final String tableName = "testHiveJdbcDriver_Table"; private static final String tableComment = "Simple table"; private static final String viewName = "testHiveJdbcDriverView"; @@ -96,6 +98,26 @@ stmt.execute("set hive.support.concurrency = false"); + // drop database. ignore error. + try { + stmt.executeQuery("drop database " + databaseName); + } catch (Exception ex) { + fail(ex.toString()); + } + + // create database + ResultSet resDb = stmt.executeQuery("create database " + databaseName + + " comment '" + + databaseComment + "'"); + assertFalse(resDb.next()); + + if (standAloneServer) { + // re-open connection + con.close(); + con = DriverManager.getConnection("jdbc:hive2://localhost:10000/" + databaseName, + "", ""); + } + // drop table. ignore error. try { stmt.execute("drop table " + tableName); @@ -645,6 +667,24 @@ exceptionFound); } + public void testShowDatabases() throws SQLException { + Statement stmt = con.createStatement(); + assertNotNull("Statement is null", stmt); + + ResultSet res = stmt.executeQuery("show databases"); + + boolean testDatabaseExists = false; + while (res.next()) { + assertNotNull("table name is null in result set", res.getString(1)); + if (databaseName.equalsIgnoreCase(res.getString(1))) { + testDatabaseExists = true; + } + } + + assertTrue("database name " + databaseName + + " not found in SHOW DATABASES result set", testDatabaseExists); + } + public void testShowTables() throws SQLException { Statement stmt = con.createStatement(); assertNotNull("Statement is null", stmt);