PHOENIX-2931 Phoenix client asks users to provide configs in cli that are 
present on the machine in hbase conf (aliciashu)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/4a728fb5
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/4a728fb5
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/4a728fb5

Branch: refs/heads/4.x-HBase-1.0
Commit: 4a728fb57f633fbfa0f01fc8036f9588ee5b3ee9
Parents: 6bfcfe5
Author: Josh Elser <els...@apache.org>
Authored: Tue Jun 28 23:29:52 2016 -0700
Committer: Josh Elser <els...@apache.org>
Committed: Tue Jun 28 23:34:26 2016 -0700

----------------------------------------------------------------------
 bin/sqlline.py                                  | 37 +++++++++++++++-----
 .../phoenix/jdbc/PhoenixEmbeddedDriver.java     | 28 +++++++++++++++
 .../org/apache/phoenix/util/PhoenixRuntime.java | 16 +++++++--
 .../phoenix/jdbc/PhoenixEmbeddedDriverTest.java |  6 ++--
 4 files changed, 72 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/4a728fb5/bin/sqlline.py
----------------------------------------------------------------------
diff --git a/bin/sqlline.py b/bin/sqlline.py
index 6d537e3..474968f 100755
--- a/bin/sqlline.py
+++ b/bin/sqlline.py
@@ -37,21 +37,40 @@ atexit.register(kill_child)
 
 phoenix_utils.setPath()
 
-if len(sys.argv) < 2:
-    print "Zookeeper not specified. \nUsage: sqlline.py <zookeeper> \
-<optional_sql_file> \nExample: \n 1. sqlline.py localhost:2181:/hbase \n 2. 
sqlline.py \
-localhost:2181:/hbase ../examples/stock_symbol.sql"
-    sys.exit()
+def printUsage():
+    print "\nUsage: sqlline.py [zookeeper] \
+[optional_sql_file] \nExample: \n 1. sqlline.py \n \
+2. sqlline.py localhost:2181:/hbase \n 3. sqlline.py \
+localhost:2181:/hbase ../examples/stock_symbol.sql \n \
+4. sqlline.py ../examples/stock_symbol.sql"
+    sys.exit(-1)
 
-sqlfile = ""
+if len(sys.argv) > 3:
+    printUsage()
 
-if len(sys.argv) > 2:
-    sqlfile = "--run=" + phoenix_utils.shell_quote([sys.argv[2]])
+sqlfile = ""
+zookeeper = ""
 
 # HBase configuration folder path (where hbase-site.xml reside) for
 # HBase/Phoenix client side property override
 hbase_config_path = os.getenv('HBASE_CONF_DIR', phoenix_utils.current_dir)
 
+if len(sys.argv) == 2:
+    if os.path.isfile(sys.argv[1]):
+        sqlfile = sys.argv[1]
+    else:
+        zookeeper = sys.argv[1]
+
+if len(sys.argv) == 3:
+    if os.path.isfile(sys.argv[1]):
+        printUsage()
+    else:
+        zookeeper = sys.argv[1]
+        sqlfile = sys.argv[2]
+
+if sqlfile:
+    sqlfile = "--run=" + phoenix_utils.shell_quote([sqlfile])
+
 java_home = os.getenv('JAVA_HOME')
 
 # load hbase-env.??? to extract JAVA_HOME, HBASE_PID_DIR, HBASE_LOG_DIR
@@ -92,7 +111,7 @@ java_cmd = java + ' $PHOENIX_OPTS ' + \
     os.pathsep + phoenix_utils.hadoop_conf + os.pathsep + 
phoenix_utils.hadoop_classpath + '" -Dlog4j.configuration=file:' + \
     os.path.join(phoenix_utils.current_dir, "log4j.properties") + \
     " sqlline.SqlLine -d org.apache.phoenix.jdbc.PhoenixDriver \
--u jdbc:phoenix:" + phoenix_utils.shell_quote([sys.argv[1]]) + \
+-u jdbc:phoenix:" + phoenix_utils.shell_quote([zookeeper]) + \
     " -n none -p none --color=" + colorSetting + " --fastConnect=false 
--verbose=true \
 --incremental=false --isolation=TRANSACTION_READ_COMMITTED " + sqlfile
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/4a728fb5/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriver.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriver.java 
b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriver.java
index 7943559..40e03b9 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriver.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriver.java
@@ -31,13 +31,19 @@ import java.util.logging.Logger;
 
 import javax.annotation.concurrent.Immutable;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HConstants;
 import org.apache.phoenix.coprocessor.MetaDataProtocol;
 import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.exception.SQLExceptionInfo;
 import org.apache.phoenix.query.ConnectionQueryServices;
+import org.apache.phoenix.query.HBaseFactoryProvider;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.util.PhoenixRuntime;
 import org.apache.phoenix.util.PropertiesUtil;
+import org.apache.phoenix.util.QueryUtil;
 import org.apache.phoenix.util.ReadOnlyProps;
 import org.apache.phoenix.util.SQLCloseable;
 
@@ -58,6 +64,7 @@ public abstract class PhoenixEmbeddedDriver implements 
Driver, SQLCloseable {
     /**
      * The protocol for Phoenix Network Client 
      */ 
+    private static final Log LOG = 
LogFactory.getLog(PhoenixEmbeddedDriver.class);
     private final static String DNC_JDBC_PROTOCOL_SUFFIX = "//";
     private final static String DRIVER_NAME = "PhoenixEmbeddedDriver";
     private static final String TERMINATOR = "" + 
PhoenixRuntime.JDBC_PROTOCOL_TERMINATOR;
@@ -214,6 +221,10 @@ public abstract class PhoenixEmbeddedDriver implements 
Driver, SQLCloseable {
         
         public static ConnectionInfo create(String url) throws SQLException {
             url = url == null ? "" : url;
+            if (url.isEmpty() || url.equalsIgnoreCase("jdbc:phoenix:")
+                    || url.equalsIgnoreCase("jdbc:phoenix")) {
+                return defaultConnectionInfo(url);
+            }
             url = url.startsWith(PhoenixRuntime.JDBC_PROTOCOL)
                     ? url.substring(PhoenixRuntime.JDBC_PROTOCOL.length())
                     : PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + url;
@@ -434,6 +445,23 @@ public abstract class PhoenixEmbeddedDriver implements 
Driver, SQLCloseable {
             return PhoenixRuntime.JDBC_PROTOCOL + 
PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR
                     + toString();
         }
+
+        private static ConnectionInfo defaultConnectionInfo(String url) throws 
SQLException {
+            Configuration config =
+                    
HBaseFactoryProvider.getConfigurationFactory().getConfiguration();
+            String quorum = config.get(HConstants.ZOOKEEPER_QUORUM);
+            if (quorum == null || quorum.isEmpty()) {
+                throw getMalFormedUrlException(url);
+            }
+            String clientPort = config.get(HConstants.ZOOKEEPER_CLIENT_PORT);
+            Integer port = clientPort==null ? null : 
Integer.parseInt(clientPort);
+            if (port == null || port < 0) {
+                throw getMalFormedUrlException(url);
+            }
+            String znodeParent = config.get(HConstants.ZOOKEEPER_ZNODE_PARENT);
+            LOG.debug("Getting default jdbc connection url " + quorum + ":" + 
port + ":" + znodeParent);
+            return new ConnectionInfo(quorum, port, znodeParent);
+        }
     }
 
     public static boolean isTestUrl(String url) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/4a728fb5/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java 
b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
index ed0ea3e..ff3326d 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
@@ -50,12 +50,14 @@ import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
 import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.client.Mutation;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Pair;
+import org.apache.hadoop.util.StringUtils;
 import org.apache.phoenix.compile.QueryPlan;
 import org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult;
 import org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode;
@@ -68,6 +70,7 @@ import org.apache.phoenix.jdbc.PhoenixPreparedStatement;
 import org.apache.phoenix.jdbc.PhoenixResultSet;
 import org.apache.phoenix.monitoring.GlobalClientMetrics;
 import org.apache.phoenix.monitoring.GlobalMetric;
+import org.apache.phoenix.query.HBaseFactoryProvider;
 import org.apache.phoenix.query.QueryConstants;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.schema.AmbiguousColumnException;
@@ -611,6 +614,7 @@ public class PhoenixRuntime {
             }
 
             ExecutionCommand execCmd = new ExecutionCommand();
+            execCmd.connectionString = "";
             if(cmdLine.hasOption(mapNamespaceOption.getOpt())){
                 execCmd.mapNamespace = true;
                 execCmd.srcTable = 
validateTableName(cmdLine.getOptionValue(mapNamespaceOption.getOpt()));
@@ -660,16 +664,21 @@ public class PhoenixRuntime {
 
             List<String> argList = Lists.newArrayList(cmdLine.getArgList());
             if (argList.isEmpty()) {
-                usageError("Connection string to HBase must be supplied", 
options);
+                usageError("At least one input file must be supplied", 
options);
             }
-            execCmd.connectionString = argList.remove(0);
             List<String> inputFiles = Lists.newArrayList();
+            int i = 0;
             for (String arg : argList) {
                 if (execCmd.isUpgrade || arg.endsWith(CSV_FILE_EXT) || 
arg.endsWith(SQL_FILE_EXT)) {
                     inputFiles.add(arg);
                 } else {
-                    usageError("Don't know how to interpret argument '" + arg 
+ "'", options);
+                    if (i == 0) {
+                        execCmd.connectionString = arg;
+                    } else {
+                        usageError("Don't know how to interpret argument '" + 
arg + "'", options);
+                    }
                 }
+                i++;
             }
 
             if (inputFiles.isEmpty() && !execCmd.isUpgrade && 
!execCmd.isMapNamespace() && !execCmd.isLocalIndexUpgrade()) {
@@ -712,6 +721,7 @@ public class PhoenixRuntime {
                             "<path-to-sql-or-csv-file>...",
                     options);
             System.out.println("Examples:\n" +
+                    "  psql my_ddl.sql\n" +
                     "  psql localhost my_ddl.sql\n" +
                     "  psql localhost my_ddl.sql my_table.csv\n" +
                     "  psql -t MY_TABLE my_cluster:1825 my_table2012-Q3.csv\n" 
+

http://git-wip-us.apache.org/repos/asf/phoenix/blob/4a728fb5/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriverTest.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriverTest.java
 
b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriverTest.java
index 4eda825..98a028c 100644
--- 
a/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriverTest.java
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriverTest.java
@@ -71,9 +71,9 @@ public class PhoenixEmbeddedDriverTest {
             "jdbc:phoenix:v1,v2,v3:345:LongRunningQueries;test=false",
         };
         ConnectionInfo[] infos = new ConnectionInfo[] {
-            new ConnectionInfo(null,null,null),
-            new ConnectionInfo(null,null,null),
-            new ConnectionInfo(null,null,null),
+            new ConnectionInfo("localhost",2181,"/hbase"),
+            new ConnectionInfo("localhost",2181,"/hbase"),
+            new ConnectionInfo("localhost",2181,"/hbase"),
             new ConnectionInfo(null,null,null),
             new ConnectionInfo("localhost",null,null),
             new ConnectionInfo("localhost",null,null),

Reply via email to