diff --git a/jdbc/src/java/org/apache/hive/jdbc/Utils.java b/jdbc/src/java/org/apache/hive/jdbc/Utils.java index e6b1a36..8961e2b 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/Utils.java +++ b/jdbc/src/java/org/apache/hive/jdbc/Utils.java @@ -22,6 +22,7 @@ import java.net.URISyntaxException; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -86,6 +87,9 @@ // Use ZooKeeper for indirection while using dynamic service discovery static final String SERVICE_DISCOVERY_MODE_ZOOKEEPER = "zooKeeper"; static final String ZOOKEEPER_NAMESPACE = "zooKeeperNamespace"; + // Default namespace value on ZooKeeper. + // This value is used if the param "zooKeeperNamespace" is not specified in the JDBC Uri. + static final String ZOOKEEPER_DEFAULT_NAMESPACE = "hiveserver2"; // Non-configurable params: // ZOOKEEPER_SESSION_TIMEOUT is not exposed as client configurable @@ -367,14 +371,30 @@ public static JdbcConnectionParams parseURL(String uri) throws JdbcUriParseExcep private static String getAuthorities(String uri, JdbcConnectionParams connParams) throws JdbcUriParseException { String authorities; - // For a jdbc uri like: jdbc:hive2://host1:port1,host2:port2,host3:port3/ - // Extract the uri host:port list starting after "jdbc:hive2://", till the 1st "/" or EOL + /** + * For a jdbc uri like: + * jdbc:hive2://:,:/dbName;sess_var_list?conf_list#var_list + * Extract the uri host:port list starting after "jdbc:hive2://", + * till the 1st "/" or "?" or "#" whichever comes first & in the given order + * Examples: + * jdbc:hive2://host1:port1,host2:port2,host3:port3/db;k1=v1?k2=v2#k3=v3 + * jdbc:hive2://host1:port1,host2:port2,host3:port3/;k1=v1?k2=v2#k3=v3 + * jdbc:hive2://host1:port1,host2:port2,host3:port3?k2=v2#k3=v3 + * jdbc:hive2://host1:port1,host2:port2,host3:port3#k3=v3 + */ int fromIndex = Utils.URL_PREFIX.length(); - int toIndex = uri.indexOf("/", fromIndex); + int toIndex = -1; + ArrayList toIndexChars = new ArrayList(Arrays.asList("/", "?", "#")); + for (String toIndexChar : toIndexChars) { + toIndex = uri.indexOf(toIndexChar, fromIndex); + if (toIndex > 0) { + break; + } + } if (toIndex < 0) { authorities = uri.substring(fromIndex); } else { - authorities = uri.substring(fromIndex, uri.indexOf("/", fromIndex)); + authorities = uri.substring(fromIndex, toIndex); } return authorities; } diff --git a/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java b/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java index 06795a5..55524b3 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java +++ b/jdbc/src/java/org/apache/hive/jdbc/ZooKeeperHiveClientHelper.java @@ -53,6 +53,9 @@ static String getNextServerUriFromZooKeeper(JdbcConnectionParams connParams) String zooKeeperEnsemble = connParams.getZooKeeperEnsemble(); String zooKeeperNamespace = connParams.getSessionVars().get(JdbcConnectionParams.ZOOKEEPER_NAMESPACE); + if ((zooKeeperNamespace == null) || (zooKeeperNamespace.isEmpty())) { + zooKeeperNamespace = JdbcConnectionParams.ZOOKEEPER_DEFAULT_NAMESPACE; + } List serverHosts; Random randomizer = new Random(); String serverNode;