Index: jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveConnection.java =================================================================== --- jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveConnection.java (revision 1508694) +++ 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 1508694) +++ 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 1508694) +++ 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 if exists " + databaseName); + } catch (Exception ex) { + fail(ex.toString()); + } + + // create database + ResultSet resDb = stmt.executeQuery("create database if not exists " + 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 '" @@ -189,6 +211,10 @@ res = stmt.executeQuery("drop table " + dataTypeTableName); assertFalse(res.next()); + // drop database + res = stmt.executeQuery("drop database if exists " + databaseName); + assertFalse(res.next()); + con.close(); assertTrue("Connection should be closed", con.isClosed()); @@ -608,6 +634,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 1508694) +++ 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,25 @@ stmt.execute("set hive.support.concurrency = false"); + // drop database. ignore error. + try { + stmt.execute("drop database if exists " + databaseName); + } catch (Exception ex) { + fail(ex.toString()); + } + + // create database + stmt.execute("create database if not exists " + databaseName + + " comment '" + + databaseComment + "'"); + + 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); @@ -181,6 +202,9 @@ stmt.execute("drop table " + partitionedTableName); stmt.execute("drop table " + dataTypeTableName); + // drop database + stmt.execute("drop database if exists " + databaseName); + con.close(); assertTrue("Connection should be closed", con.isClosed()); @@ -645,6 +669,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("database 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); @@ -733,11 +775,16 @@ assertEquals("TABLE_SCHEM", resMeta.getColumnName(1)); assertEquals("TABLE_CATALOG", resMeta.getColumnName(2)); - assertTrue(rs.next()); - assertEquals("default", rs.getString(1)); -// assertNull(rs.getString(2)); + boolean testDatabaseExists = false; + while (rs.next()) { + assertNotNull("database name is null in result set", rs.getString(1)); + if (databaseName.equalsIgnoreCase(rs.getString(1))) { + testDatabaseExists = true; + } + } + assertTrue("database name " + databaseName + + " not found in SCHEMAS result set", testDatabaseExists); - assertFalse(rs.next()); rs.close(); }