diff --git a/common/src/main/java/com/cloudera/fce/exampleclients/common/JdbcClient.java b/common/src/main/java/com/cloudera/fce/exampleclients/common/JdbcClient.java index 7b08266..54312ee 100644 --- a/common/src/main/java/com/cloudera/fce/exampleclients/common/JdbcClient.java +++ b/common/src/main/java/com/cloudera/fce/exampleclients/common/JdbcClient.java @@ -1,5 +1,10 @@ package com.cloudera.fce.exampleclients.common; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.PatternLayout; + import java.sql.*; public abstract class JdbcClient { @@ -16,6 +21,17 @@ public void closeConnection() throws SQLException { } } + public static void configureLogger(Boolean debug) { + ConsoleAppender console = new ConsoleAppender(); + console.setLayout(new PatternLayout("%d [%p] %m%n")); + console.setThreshold(Level.INFO); + if (debug) { + console.setThreshold(Level.DEBUG); + } + console.activateOptions(); + LogManager.getRootLogger().addAppender(console); + } + /** * Simple method to run a single query via JDBC. Uses "default" database. * @param query diff --git a/hive/README.md b/hive/README.md index 064ae35..9610625 100644 --- a/hive/README.md +++ b/hive/README.md @@ -1,4 +1,4 @@ -# Example Impala clients +# Example Hive clients Simple example Hive clients showing how to access secure Hive programmatically via JDBC/ODBC. @@ -7,6 +7,20 @@ Simple example Hive clients showing how to access secure Hive programmatically v Java clients require either the supplied Apache driver or the Cloudera Simba driver to communicate over JDBC with Hive. +## Usage + +``` +Usage:com.cloudera.fce.exampleclients.hive.java.HiveJdbcClient + -h HOST + -q QUERY + [-d {APACHE|SIMBA}] + [-db DATABASE] [-p PORT] + [-s SERVER_PRINC] [-k] [-t KEYTAB] [-u USER_PRINC] [-r REALM] + [-j JAAS_FILE] + [-St SSL_TRUSTSTORE] [-Sp SSL_TRUSTSTORE_PASS] + [-debug] +``` + ### Apache Driver This example is a simple command line query runner with a sample invocation to a Kerberised @@ -18,7 +32,7 @@ in CDH <= 5.5. export APACHE_HIVE_CP=$(for j in /opt/cloudera/parcels/CDH/lib/hive/lib/*.jar; do echo -n "$j:"; done | sed 's/:$//') # Run simple command line client -java -cp $SIMBA_IMPALA_CP:example-clients-common-1.0.0-SNAPSHOT.jar:example-clients-hive-1.0.0-SNAPSHOT.jar \ +java -cp APACHE_HIVE_CP:example-clients-common-1.0.0-SNAPSHOT.jar:example-clients-hive-1.0.0-SNAPSHOT.jar \ com.cloudera.fce.exampleclients.hive.java.HiveJdbcClient \ -h cs-vanilla.ib.dev \ -q "select * from sample_08 limit 10" \ @@ -43,3 +57,4 @@ java -cp $SIMBA_HIVE_CP:example-clients-common-1.0.0-SNAPSHOT.jar:example-client -St /opt/cloudera/security/public/truststore.jks -Sp hadoop ``` +**Tip:** use the `-debug` option to print the full JDBC connection string used to establish the connection to Hive \ No newline at end of file diff --git a/hive/src/main/java/com/cloudera/fce/exampleclients/hive/java/HiveJdbcClient.java b/hive/src/main/java/com/cloudera/fce/exampleclients/hive/java/HiveJdbcClient.java index 6c01596..2844295 100644 --- a/hive/src/main/java/com/cloudera/fce/exampleclients/hive/java/HiveJdbcClient.java +++ b/hive/src/main/java/com/cloudera/fce/exampleclients/hive/java/HiveJdbcClient.java @@ -2,11 +2,13 @@ import com.cloudera.fce.exampleclients.common.JdbcClient; import com.cloudera.fce.exampleclients.common.JdbcDriver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Properties; public class HiveJdbcClient extends JdbcClient { - + private static final Logger LOG = LoggerFactory.getLogger(HiveJdbcClient.class); private static final int DEFAULT_HS2_PORT = 10000; private static final String DEFAULT_HS2_PRINCIPAL = "hive"; @@ -18,6 +20,7 @@ public HiveJdbcClient(Properties properties) { public JdbcDriver loadDriver() throws ClassNotFoundException { String driverName = properties.getProperty("driver"); + LOG.debug("driverName: %s", driverName.toUpperCase()); JdbcDriver driver = new ApacheHiveDriver(); switch(driverName.toUpperCase()) { case "APACHE": break; @@ -39,6 +42,7 @@ public void run() throws Exception { properties.getProperty("password", null), properties.getProperty("ssltruststore", null), properties.getProperty("ssltruststorepassword", null)); + LOG.debug("URL: %s", url); // Set up security boolean secure = Boolean.parseBoolean(properties.getProperty("secure")); @@ -78,11 +82,17 @@ private static String getNextArg(String[] args, String opt, int next) { private static void exitWithUsage(String msg, int exit) { System.err.println(msg); - System.err.printf( - "Usage: %s -h HOST -q QUERY [-db DATABASE] [-p PORT] [-s SERVER_PRINC] [-k] [-t KEYTAB] " + - "[-u USER_PRINC] [-d {APACHE|SIMBA}] [-r REALM] [-j JAAS_FILE] [-St SSL_TRUSTSTORE] " + - "[-Sp SSL_TRUSTSTORE_PASSWORD]\n", - HiveJdbcClient.class.getName()); + StringBuilder sb = new StringBuilder(); + sb.append("Usage:" + HiveJdbcClient.class.getName() + "\n"); + sb.append("\t-h HOST\n"); + sb.append("\t-q QUERY\n"); + sb.append("\t[-d {APACHE|SIMBA}]\n"); + sb.append("\t[-db DATABASE] [-p PORT]\n"); + sb.append("\t[-s SERVER_PRINC] [-k] [-t KEYTAB] [-u USER_PRINC] [-r REALM]\n"); + sb.append("\t[-j JAAS_FILE]\n"); + sb.append("\t[-St SSL_TRUSTSTORE] [-Sp SSL_TRUSTSTORE_PASS]\n"); + sb.append("\t[-debug]\n"); + System.err.printf(sb.toString()); System.exit(exit); } @@ -92,7 +102,6 @@ private static Properties getProperties() { properties.setProperty("port", Integer.toString(DEFAULT_HS2_PORT)); properties.setProperty("secure", "false"); properties.setProperty("driver", "APACHE"); - properties.setProperty("driver", "default"); return properties; } @@ -128,6 +137,8 @@ public static void main(String[] args) throws Exception { properties.setProperty("ssltruststorepassword", getNextArg(args, "-Sp", ++i)); break; case "-db": properties.setProperty("db", getNextArg(args, "-db", ++i)); break; + case "-debug": + properties.setProperty("debug", "true"); break; default: exitWithUsage("Unrecognised option: " + arg, -1); } @@ -141,6 +152,8 @@ public static void main(String[] args) throws Exception { exitWithUsage("Server hostname cannot be null", -1); } + configureLogger(Boolean.parseBoolean(properties.getProperty("debug"))); + HiveJdbcClient client = new HiveJdbcClient(properties); client.run(); } diff --git a/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/ImpalaJdbcClient.java b/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/ImpalaJdbcClient.java index 9b1ae89..400c6e0 100644 --- a/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/ImpalaJdbcClient.java +++ b/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/ImpalaJdbcClient.java @@ -2,15 +2,13 @@ import com.cloudera.fce.exampleclients.common.JdbcClient; import com.cloudera.fce.exampleclients.common.JdbcDriver; -import org.apache.log4j.ConsoleAppender; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.apache.log4j.PatternLayout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Properties; public class ImpalaJdbcClient extends JdbcClient { - + private static final Logger LOG = LoggerFactory.getLogger(ImpalaJdbcClient.class); private static final int DEFAULT_HS2_PORT = 21050; private static final String DEFAULT_HS2_PRINCIPAL = "impala"; @@ -37,6 +35,7 @@ public void run() throws Exception { properties.getProperty("password", null), properties.getProperty("ssltruststore", null), properties.getProperty("ssltruststorepassword", null)); + LOG.debug("URL: %s", url); // Set up security boolean secure = Boolean.parseBoolean(properties.getProperty("secure")); @@ -146,18 +145,9 @@ public static void main(String[] args) throws Exception { exitWithUsage("Server hostname cannot be null", -1); } - ConsoleAppender console = new ConsoleAppender(); - console.setLayout(new PatternLayout("%d [%p] %m%n")); - if (Boolean.parseBoolean(properties.getProperty("debug"))) { - console.setThreshold(Level.DEBUG); - } else { - console.setThreshold(Level.INFO); - } - console.activateOptions(); - Logger.getRootLogger().addAppender(console); + configureLogger(Boolean.parseBoolean(properties.getProperty("debug"))); ImpalaJdbcClient client = new ImpalaJdbcClient(properties); client.run(); } - } diff --git a/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/SimbaImpalaDriver.java b/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/SimbaImpalaDriver.java index c88b0c5..070ff85 100644 --- a/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/SimbaImpalaDriver.java +++ b/impala/src/main/java/com/cloudera/fce/exampleclients/impala/java/SimbaImpalaDriver.java @@ -9,7 +9,6 @@ public class SimbaImpalaDriver implements JdbcDriver { - private static final Logger LOG = LoggerFactory.getLogger(SimbaImpalaDriver.class); private String jaasFile = null; @Override @@ -38,7 +37,6 @@ public String constructJdbcUrl(String host, int port, url += String.format(";SSL=1;SSLTrustStore=%s;SSLTrustStorePwd=%s", sslTrustStore, sslTrustStorePassword); } - LOG.debug(url); return url; }