Repository: hive Updated Branches: refs/heads/master ef32c73ac -> d7f043a44
HIVE-14062: Changes from HIVE-13502 overwritten by HIVE-13566 (Naveen Gangam, reviewed by Aihua Xu) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/d7f043a4 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/d7f043a4 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/d7f043a4 Branch: refs/heads/master Commit: d7f043a4404233421a65e07a0acc325347c109e1 Parents: ef32c73 Author: Aihua Xu <aihu...@apache.org> Authored: Wed Jun 22 10:38:17 2016 -0400 Committer: Aihua Xu <aihu...@apache.org> Committed: Wed Jun 22 10:38:17 2016 -0400 ---------------------------------------------------------------------- .../java/org/apache/hive/beeline/BeeLine.java | 14 +++- .../java/org/apache/hive/beeline/Commands.java | 72 +++++++++++++------- .../hive/beeline/TestBeeLineWithArgs.java | 24 ++++++- jdbc/src/java/org/apache/hive/jdbc/Utils.java | 54 +++++++++------ 4 files changed, 116 insertions(+), 48 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/d7f043a4/beeline/src/java/org/apache/hive/beeline/BeeLine.java ---------------------------------------------------------------------- diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index 457a884..d02ea84 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -94,6 +94,9 @@ import org.apache.hadoop.io.IOUtils; import org.apache.hive.beeline.cli.CliOptionsProcessor; import org.apache.thrift.transport.TTransportException; +import org.apache.hive.jdbc.Utils; +import org.apache.hive.jdbc.Utils.JdbcConnectionParams; + /** * A console SQL shell with command completion. * <p> @@ -140,7 +143,6 @@ public class BeeLine implements Closeable { private static final Options options = new Options(); public static final String BEELINE_DEFAULT_JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver"; - public static final String BEELINE_DEFAULT_JDBC_URL = "jdbc:hive2://"; public static final String DEFAULT_DATABASE_NAME = "default"; private static final String SCRIPT_OUTPUT_PREFIX = ">>>"; @@ -775,6 +777,14 @@ public class BeeLine implements Closeable { */ if (url != null) { + if (user == null) { + user = Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_USER); + } + + if (pass == null) { + pass = Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_PASSWD); + } + String com = constructCmd(url, user, pass, driver, false); String comForDebug = constructCmd(url, user, pass, driver, true); debug("issuing: " + comForDebug); @@ -912,7 +922,7 @@ public class BeeLine implements Closeable { } private int embeddedConnect() { - if (!execCommandWithPrefix("!connect " + BEELINE_DEFAULT_JDBC_URL + " '' ''")) { + if (!execCommandWithPrefix("!connect " + Utils.URL_PREFIX + " '' ''")) { return ERRNO_OTHER; } else { return ERRNO_OK; http://git-wip-us.apache.org/repos/asf/hive/blob/d7f043a4/beeline/src/java/org/apache/hive/beeline/Commands.java ---------------------------------------------------------------------- diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java index 80703ff..3a204c0 100644 --- a/beeline/src/java/org/apache/hive/beeline/Commands.java +++ b/beeline/src/java/org/apache/hive/beeline/Commands.java @@ -61,6 +61,8 @@ import java.util.TreeSet; import org.apache.hadoop.hive.common.cli.ShellCmdExecutor; import org.apache.hive.jdbc.HiveStatement; +import org.apache.hive.jdbc.Utils; +import org.apache.hive.jdbc.Utils.JdbcConnectionParams; public class Commands { @@ -1314,18 +1316,41 @@ public class Commands { Properties props = new Properties(); if (url != null) { String saveUrl = getUrlToUse(url); - props.setProperty("url", saveUrl); + props.setProperty(JdbcConnectionParams.PROPERTY_URL, url); } + + String value = null; if (driver != null) { - props.setProperty("driver", driver); + props.setProperty(JdbcConnectionParams.PROPERTY_DRIVER, driver); + } else { + value = Utils.parsePropertyFromUrl(url, JdbcConnectionParams.PROPERTY_DRIVER); + if (value != null) { + props.setProperty(JdbcConnectionParams.PROPERTY_DRIVER, value); + } } + if (user != null) { - props.setProperty("user", user); + props.setProperty(JdbcConnectionParams.AUTH_USER, user); + } else { + value = Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_USER); + if (value != null) { + props.setProperty(JdbcConnectionParams.AUTH_USER, value); + } } + if (pass != null) { - props.setProperty("password", pass); + props.setProperty(JdbcConnectionParams.AUTH_PASSWD, pass); + } else { + value = Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_PASSWD); + if (value != null) { + props.setProperty(JdbcConnectionParams.AUTH_PASSWD, value); + } } + value = Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_TYPE); + if (value != null) { + props.setProperty(JdbcConnectionParams.AUTH_TYPE, value); + } return connect(props); } @@ -1378,26 +1403,25 @@ public class Commands { public boolean connect(Properties props) throws IOException { String url = getProperty(props, new String[] { - "url", + JdbcConnectionParams.PROPERTY_URL, "javax.jdo.option.ConnectionURL", "ConnectionURL", }); String driver = getProperty(props, new String[] { - "driver", + JdbcConnectionParams.PROPERTY_DRIVER, "javax.jdo.option.ConnectionDriverName", "ConnectionDriverName", }); String username = getProperty(props, new String[] { - "user", + JdbcConnectionParams.AUTH_USER, "javax.jdo.option.ConnectionUserName", "ConnectionUserName", }); String password = getProperty(props, new String[] { - "password", + JdbcConnectionParams.AUTH_PASSWD, "javax.jdo.option.ConnectionPassword", "ConnectionPassword", }); - String auth = getProperty(props, new String[] {"auth"}); if (url == null || url.length() == 0) { return beeLine.error("Property \"url\" is required"); @@ -1408,23 +1432,25 @@ public class Commands { } } - beeLine.info("Connecting to " + url); - - if (username == null) { - username = beeLine.getConsoleReader().readLine("Enter username for " + url + ": "); - } - props.setProperty("user", username); - if (password == null) { - password = beeLine.getConsoleReader().readLine("Enter password for " + url + ": ", - new Character('*')); - } - props.setProperty("password", password); - + String auth = getProperty(props, new String[] {JdbcConnectionParams.AUTH_TYPE}); if (auth == null) { auth = beeLine.getOpts().getAuthType(); + if (auth != null) { + props.setProperty(JdbcConnectionParams.AUTH_TYPE, auth); + } } - if (auth != null) { - props.setProperty("auth", auth); + + beeLine.info("Connecting to " + url); + if (Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_PRINCIPAL) == null) { + if (username == null) { + username = beeLine.getConsoleReader().readLine("Enter username for " + url + ": "); + } + props.setProperty(JdbcConnectionParams.AUTH_USER, username); + if (password == null) { + password = beeLine.getConsoleReader().readLine("Enter password for " + url + ": ", + new Character('*')); + } + props.setProperty(JdbcConnectionParams.AUTH_PASSWD, password); } try { http://git-wip-us.apache.org/repos/asf/hive/blob/d7f043a4/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java ---------------------------------------------------------------------- diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java index f9909ad..ecfeddb 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java @@ -39,6 +39,7 @@ import java.util.List; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.jdbc.Utils; import org.apache.hive.jdbc.miniHS2.MiniHS2; import org.junit.AfterClass; import org.junit.Assert; @@ -175,6 +176,7 @@ public class TestBeeLineWithArgs { // Put the script content in a temp file File scriptFile = File.createTempFile(this.getClass().getSimpleName(), "temp"); + System.out.println("script file is " + scriptFile.getAbsolutePath()); scriptFile.deleteOnExit(); PrintStream os = new PrintStream(new FileOutputStream(scriptFile)); os.print(scriptText); @@ -655,7 +657,7 @@ public class TestBeeLineWithArgs { @Test public void testEmbeddedBeelineConnection() throws Throwable{ - String embeddedJdbcURL = BeeLine.BEELINE_DEFAULT_JDBC_URL+"/Default"; + String embeddedJdbcURL = Utils.URL_PREFIX+"/Default"; List<String> argList = getBaseArgs(embeddedJdbcURL); argList.add("--hivevar"); argList.add("DUMMY_TBL=embedded_table"); @@ -770,7 +772,7 @@ public class TestBeeLineWithArgs { @Test public void testEmbeddedBeelineOutputs() throws Throwable{ - String embeddedJdbcURL = BeeLine.BEELINE_DEFAULT_JDBC_URL+"/Default"; + String embeddedJdbcURL = Utils.URL_PREFIX+"/Default"; List<String> argList = getBaseArgs(embeddedJdbcURL); // Set to non-zk lock manager to avoid trying to connect to zookeeper final String SCRIPT_TEXT = @@ -843,4 +845,22 @@ public class TestBeeLineWithArgs { } + /** + * Attempt to execute a simple script file with the usage of user & password variables in URL. + * Test for presence of an expected pattern + * in the output (stdout or stderr), fail if not found + * Print PASSED or FAILED + */ + @Test + public void testConnectionWithURLParams() throws Throwable { + final String EXPECTED_PATTERN = " hivetest "; + List<String> argList = new ArrayList<String>(); + argList.add("-d"); + argList.add(BeeLine.BEELINE_DEFAULT_JDBC_DRIVER); + argList.add("-u"); + argList.add(miniHS2.getBaseJdbcURL() + ";user=hivetest;password=hive"); + String SCRIPT_TEXT = "select current_user();"; + + testScriptFile( SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } } http://git-wip-us.apache.org/repos/asf/hive/blob/d7f043a4/jdbc/src/java/org/apache/hive/jdbc/Utils.java ---------------------------------------------------------------------- diff --git a/jdbc/src/java/org/apache/hive/jdbc/Utils.java b/jdbc/src/java/org/apache/hive/jdbc/Utils.java index 42181d7..7ea6309 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/Utils.java +++ b/jdbc/src/java/org/apache/hive/jdbc/Utils.java @@ -37,12 +37,12 @@ import org.apache.http.cookie.Cookie; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class Utils { +public class Utils { static final Logger LOG = LoggerFactory.getLogger(Utils.class.getName()); /** * The required prefix for the connection URL. */ - static final String URL_PREFIX = "jdbc:hive2://"; + public static final String URL_PREFIX = "jdbc:hive2://"; /** * If host is provided, without a port. @@ -63,7 +63,7 @@ class Utils { static final String HIVE_SERVER2_RETRY_TRUE = "true"; static final String HIVE_SERVER2_RETRY_FALSE = "false"; - static class JdbcConnectionParams { + public static class JdbcConnectionParams { // Note on client side parameter naming convention: // Prefer using a shorter camelCase param name instead of using the same name as the // corresponding @@ -76,31 +76,33 @@ class Utils { // Retry setting static final String RETRIES = "retries"; - static final String AUTH_TYPE = "auth"; + public static final String AUTH_TYPE = "auth"; // We're deprecating this variable's name. - static final String AUTH_QOP_DEPRECATED = "sasl.qop"; - static final String AUTH_QOP = "saslQop"; - static final String AUTH_SIMPLE = "noSasl"; - static final String AUTH_TOKEN = "delegationToken"; - static final String AUTH_USER = "user"; - static final String AUTH_PRINCIPAL = "principal"; - static final String AUTH_PASSWD = "password"; - static final String AUTH_KERBEROS_AUTH_TYPE = "kerberosAuthType"; - static final String AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT = "fromSubject"; - static final String ANONYMOUS_USER = "anonymous"; - static final String ANONYMOUS_PASSWD = "anonymous"; - static final String USE_SSL = "ssl"; - static final String SSL_TRUST_STORE = "sslTrustStore"; - static final String SSL_TRUST_STORE_PASSWORD = "trustStorePassword"; + public static final String AUTH_QOP_DEPRECATED = "sasl.qop"; + public static final String AUTH_QOP = "saslQop"; + public static final String AUTH_SIMPLE = "noSasl"; + public static final String AUTH_TOKEN = "delegationToken"; + public static final String AUTH_USER = "user"; + public static final String AUTH_PRINCIPAL = "principal"; + public static final String AUTH_PASSWD = "password"; + public static final String AUTH_KERBEROS_AUTH_TYPE = "kerberosAuthType"; + public static final String AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT = "fromSubject"; + public static final String ANONYMOUS_USER = "anonymous"; + public static final String ANONYMOUS_PASSWD = "anonymous"; + public static final String USE_SSL = "ssl"; + public static final String SSL_TRUST_STORE = "sslTrustStore"; + public static final String SSL_TRUST_STORE_PASSWORD = "trustStorePassword"; // We're deprecating the name and placement of this in the parsed map (from hive conf vars to // hive session vars). static final String TRANSPORT_MODE_DEPRECATED = "hive.server2.transport.mode"; - static final String TRANSPORT_MODE = "transportMode"; + public static final String TRANSPORT_MODE = "transportMode"; // We're deprecating the name and placement of this in the parsed map (from hive conf vars to // hive session vars). static final String HTTP_PATH_DEPRECATED = "hive.server2.thrift.http.path"; - static final String HTTP_PATH = "httpPath"; - static final String SERVICE_DISCOVERY_MODE = "serviceDiscoveryMode"; + public static final String HTTP_PATH = "httpPath"; + public static final String SERVICE_DISCOVERY_MODE = "serviceDiscoveryMode"; + public static final String PROPERTY_DRIVER = "driver"; + public static final String PROPERTY_URL = "url"; // Don't use dynamic service discovery static final String SERVICE_DISCOVERY_MODE_NONE = "none"; // Use ZooKeeper for indirection while using dynamic service discovery @@ -631,4 +633,14 @@ class Utils { } return true; } + + public static String parsePropertyFromUrl(final String url, final String key) { + String[] tokens = url.split(";"); + for (String token : tokens) { + if (token.trim().startsWith(key.trim() + "=")) { + return token.trim().substring((key.trim() + "=").length()); + } + } + return null; + } }