diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2CustomCatalog.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2CustomCatalog.java index e69de29..6b7f001 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2CustomCatalog.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2CustomCatalog.java @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.jdbc; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.jdbc.miniHS2.MiniHS2; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.Test; + + public class TestJdbcWithMiniHS2CustomCatalog { + private static MiniHS2 miniHS2 = null; + private static final String defaultTableName = "defTable"; + private static final String customDbName = "customDb"; + + private static Connection hs2Conn = null; + + @BeforeClass + public static void setUpClass() throws Exception { + Class.forName(MiniHS2.getJdbcDriverName()); + HiveConf conf = new HiveConf(); + miniHS2 = new MiniHS2(conf); + miniHS2.start(); + + Connection beforeTestConn = DriverManager.getConnection(miniHS2.getJdbcURL(), System.getProperty("user.name"), "bar"); + beforeTestConn.createStatement().execute("set hive.support.concurrency = false"); + + beforeTestConn.createStatement().execute("DROP TABLE IF EXISTS " + defaultTableName); + beforeTestConn.createStatement().execute("CREATE TABLE "+ defaultTableName +" (a INT)"); + beforeTestConn.createStatement().execute("DROP DATABASE IF EXISTS " + customDbName + " CASCADE"); + beforeTestConn.createStatement().execute("CREATE DATABASE " + customDbName); + beforeTestConn.close(); + + String customJdbcURL = miniHS2.getJdbcURL().replace("/default", "/" + customDbName); + hs2Conn = DriverManager.getConnection(customJdbcURL, System.getProperty("user.name"), "bar"); + hs2Conn.createStatement().execute("set hive.support.concurrency = false"); + } + + @AfterClass + public static void tearDownClass() throws Exception { + hs2Conn.close(); + miniHS2.stop(); + } + + @Test + public void listTables() throws Exception { + String customTableName = "custTable"; + Statement stmt = hs2Conn.createStatement(); + + stmt.execute("DROP TABLE IF EXISTS " + customTableName); + stmt.execute("CREATE TABLE " + customTableName + " (b INT)"); + ResultSet tableList = stmt.executeQuery("SHOW TABLES"); + + int rows = 0; + boolean foundCustomTableName = false; + while (tableList.next()) { + rows++; + + assertFalse("Found default table in custom database", + defaultTableName.equalsIgnoreCase(tableList.getString(1))); + if(customTableName.equalsIgnoreCase(tableList.getString(1))) { + foundCustomTableName = true; + } + } + tableList.close(); + stmt.close(); + + assertTrue("More tables than expected in " + customDbName, rows == 1); + assertTrue("Did not find custom table", foundCustomTableName); + } +} diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/AbstractHiveService.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/AbstractHiveService.java index e69de29..e786c97 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/AbstractHiveService.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/AbstractHiveService.java @@ -0,0 +1,129 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.jdbc.miniHS2; + +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; + +/*** + * Base class for Hive service + * AbstarctHiveService. + * + */ +public abstract class AbstractHiveService { + private HiveConf hiveConf = null; + private String hostname; + private int port; + private boolean startedHiveService = false; + + public AbstractHiveService(HiveConf hiveConf, String hostname, int port) { + this.hiveConf = hiveConf; + this.hostname = hostname; + this.port = port; + } + + /** + * Get Hive conf + * @return + */ + public HiveConf getHiveConf() { + return hiveConf; + } + + /** + * Get config property + * @param propertyKey + * @return + */ + public String getConfProperty(String propertyKey) { + return hiveConf.get(propertyKey); + } + + /** + * Set config property + * @param propertyKey + * @param propertyValue + */ + public void setConfProperty(String propertyKey, String propertyValue) { + System.setProperty(propertyKey, propertyValue); + hiveConf.set(propertyKey, propertyValue); + } + + /** + * Retrieve warehouse directory + * @return + */ + public Path getWareHouseDir() { + return new Path(hiveConf.getVar(ConfVars.METASTOREWAREHOUSE)); + } + + public void setWareHouseDir(String wareHouseURI) { + verifyNotStarted(); + System.setProperty(ConfVars.METASTOREWAREHOUSE.varname, wareHouseURI); + hiveConf.setVar(ConfVars.METASTOREWAREHOUSE, wareHouseURI); + } + + /** + * Set service host + * @param hostName + */ + public void setHost(String hostName) { + this.hostname = hostName; + } + + // get service host + protected String getHost() { + return hostname; + } + + /** + * Set service port # + * @param portNum + */ + public void setPort(int portNum) { + this.port = portNum; + } + + // get service port# + protected int getPort() { + return port; + } + + public boolean isStarted() { + return startedHiveService; + } + + protected void setStarted(boolean hiveServiceStatus) { + this.startedHiveService = hiveServiceStatus; + } + + protected void verifyStarted() { + if (!isStarted()) { + throw new IllegalStateException("HS2 is not running"); + } + } + + protected void verifyNotStarted() { + if (isStarted()) { + throw new IllegalStateException("HS2 alreadyrunning"); + } + } + +} diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/MiniHS2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/MiniHS2.java index a65e678..f6404e5 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/MiniHS2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/miniHS2/MiniHS2.java @@ -37,7 +37,7 @@ import com.google.common.io.Files; -public class MiniHS2 extends AbstarctHiveService { +public class MiniHS2 extends AbstractHiveService { private static final String driverName = "org.apache.hive.jdbc.HiveDriver"; private HiveServer2 hiveServer2 = null; private final File baseDir; diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index ef39573..5996f5f 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -86,6 +86,7 @@ private final String jdbcURI; private final String host; private final int port; + private String dbName; private final Map sessConfMap; private final Map hiveConfMap; private final Map hiveVarMap; @@ -111,6 +112,7 @@ public HiveConnection(String uri, Properties info) throws SQLException { // hive_var_list -> hiveVarMap host = connParams.getHost(); port = connParams.getPort(); + dbName = connParams.getDbName(); sessConfMap = connParams.getSessionVars(); hiveConfMap = connParams.getHiveConfs(); hiveVarMap = connParams.getHiveVars(); @@ -119,7 +121,10 @@ public HiveConnection(String uri, Properties info) throws SQLException { if (isEmbeddedMode) { client = new EmbeddedThriftBinaryCLIService(); } else { - // extract user/password from JDBC connection properties if its not supplied in the connection URL + /* + * extract user/password from JDBC connection properties, + * if its not supplied in the connection URL + */ if (info.containsKey(HIVE_AUTH_USER)) { sessConfMap.put(HIVE_AUTH_USER, info.getProperty(HIVE_AUTH_USER)); if (info.containsKey(HIVE_AUTH_PASSWD)) { @@ -140,6 +145,7 @@ public HiveConnection(String uri, Properties info) throws SQLException { openSession(); configureConnection(); + setCatalog(dbName); } private void openTransport() throws SQLException { @@ -175,8 +181,7 @@ private TTransport createHttpTransport() throws SQLException { ); try { transport = new THttpClient(httpUrl, httpClient); - } - catch (TTransportException e) { + } catch (TTransportException e) { String msg = "Could not create http connection to " + jdbcURI + ". " + e.getMessage(); throw new SQLException(msg, " 08S01", e); @@ -246,8 +251,8 @@ private TTransport createBinaryTransport() throws SQLException { private boolean isHttpTransportMode() { String transportMode = hiveConfMap.get(HiveConf.ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname); - if(transportMode != null && (transportMode.equalsIgnoreCase("http") || - transportMode.equalsIgnoreCase("https"))) { + if(transportMode != null && ("http".equalsIgnoreCase(transportMode) || + "https".equalsIgnoreCase(transportMode))) { return true; } return false; @@ -311,7 +316,7 @@ private String getPasswd() { } /** - * Lookup varName in sessConfMap, if its null or empty return the default + * Lookup varName in sessConfMap, if its null or empty return the default. * value varDefault * @param varName * @param varDefault @@ -497,7 +502,7 @@ public boolean getAutoCommit() throws SQLException { */ public String getCatalog() throws SQLException { - return ""; + return dbName; } /* @@ -576,7 +581,7 @@ public int getTransactionIsolation() throws SQLException { } /* - * (non-Javadoc) + * (non-Javadojdbc/src/java/org/apache/hive/jdbc/HiveConnection.javac) * * @see java.sql.Connection#getWarnings() */ @@ -783,8 +788,10 @@ public void setAutoCommit(boolean autoCommit) throws SQLException { */ public void setCatalog(String catalog) throws SQLException { - // TODO Auto-generated method stub - throw new SQLException("Method not supported"); + Statement stmt = createStatement(); + stmt.execute("use " + catalog); + dbName = catalog; + stmt.close(); } /* @@ -872,7 +879,7 @@ public void setSchema(String schema) throws SQLException { */ public void setTransactionIsolation(int level) throws SQLException { - // TODO: throw an exception? + // TODO throw an exception? } /* diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java b/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java index c447d44..b9c0df2 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java @@ -625,21 +625,23 @@ public ResultSet getTableTypes() throws SQLException { public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException { TGetTablesResp getTableResp; + TGetTablesReq getTableReq = new TGetTablesReq(sessHandle); + + if (catalog == null) { + catalog = connection.getCatalog(); + } + getTableReq.setCatalogName(catalog); + if (schemaPattern == null) { // if schemaPattern is null it means that the schemaPattern value should not be used to narrow the search schemaPattern = "%"; } - TGetTablesReq getTableReq = new TGetTablesReq(sessHandle); + getTableReq.setSchemaName(schemaPattern); getTableReq.setTableName(tableNamePattern); - // TODO: need to set catalog parameter - if (types != null) { getTableReq.setTableTypes(Arrays.asList(types)); } - if (schemaPattern != null) { - getTableReq.setSchemaName(schemaPattern); - } try { getTableResp = client.GetTables(getTableReq);