Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
19 changes: 17 additions & 2 deletions hive/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Example Impala clients
# Example Hive clients

Simple example Hive clients showing how to access secure Hive programmatically via JDBC/ODBC.

Expand All @@ -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
Expand All @@ -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" \
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
Expand All @@ -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"));
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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"));
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

public class SimbaImpalaDriver implements JdbcDriver {

private static final Logger LOG = LoggerFactory.getLogger(SimbaImpalaDriver.class);
private String jaasFile = null;

@Override
Expand Down Expand Up @@ -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;
}

Expand Down