15 package com.cloudera.impala.testutil;
17 import java.sql.Connection;
18 import java.sql.DriverManager;
19 import java.sql.ResultSet;
20 import java.sql.ResultSetMetaData;
21 import java.sql.SQLException;
22 import java.sql.Statement;
23 import java.text.DecimalFormat;
24 import java.util.ArrayList;
26 import org.apache.commons.cli.BasicParser;
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.log4j.ConsoleAppender;
31 import org.apache.log4j.Logger;
32 import org.apache.log4j.PatternLayout;
34 import com.google.common.base.Joiner;
35 import com.google.common.base.Preconditions;
36 import com.google.common.collect.Lists;
43 private static final Logger
LOG = Logger.getLogger(ImpalaJdbcClient.class);
49 "org.apache.hive.jdbc.HiveDriver";
61 "jdbc:hive2://localhost:21050/default";
69 this.driverName_ = driverName;
70 this.connString_ = connString;
75 throw new RuntimeException(
"Connection not initialized.");
76 }
else if (
conn_.isClosed()) {
77 throw new RuntimeException(
"Connection not open.");
79 Preconditions.checkNotNull(
stmt_);
82 if (
stmt_.isClosed()) {
83 stmt_ = conn_.createStatement();
87 public void connect() throws ClassNotFoundException, SQLException {
95 stmt_ = conn_.createStatement();
102 public void close() throws SQLException {
116 public ResultSet
execQuery(String query)
throws SQLException {
118 LOG.info(
"Executing: " + query);
119 return stmt_.executeQuery(query);
124 LOG.info(
"Using: " + db_name);
125 stmt_.execute(
"use " + db_name);
170 Options options =
new Options();
171 options.addOption(
"i",
true,
"host:port of target machine impalad is listening on");
172 options.addOption(
"c",
true,
173 "Full connection string to use. Overrides host/port value");
174 options.addOption(
"t",
true,
"SASL/NOSASL, whether to use SASL transport or not");
175 options.addOption(
"q",
true,
"Query String");
176 options.addOption(
"help",
false,
"Help");
178 BasicParser optionParser =
new BasicParser();
179 CommandLine cmdArgs = optionParser.parse(options, args);
181 String transportOption = cmdArgs.getOptionValue(
"t");
182 if (transportOption == null) {
183 LOG.error(
"Must specify '-t' option, whether to use SASL transport or not.");
184 LOG.error(
"Using the wrong type of transport will cause the program to hang.");
185 LOG.error(
"Usage: " + options.toString());
188 if (!transportOption.equalsIgnoreCase(
"SASL") &&
189 !transportOption.equalsIgnoreCase(
"NOSASL")) {
190 LOG.error(
"Invalid argument " + transportOption +
" to '-t' option.");
191 LOG.error(
"Usage: " + options.toString());
194 boolean useSasl = transportOption.equalsIgnoreCase(
"SASL");
196 String connStr = cmdArgs.getOptionValue(
"c", null);
200 if (connStr == null) {
201 String hostPort = cmdArgs.getOptionValue(
"i",
"localhost:21050");
202 connStr =
"jdbc:hive2://" + hostPort +
"/";
211 String query = cmdArgs.getOptionValue(
"q");
213 LOG.error(
"Must specify a query to execute.");
214 LOG.error(
"Usage: " + options.toString());
222 throws NumberFormatException {
223 columnType = columnType.toLowerCase();
224 if (colVal == null) {
225 return columnType.equals(
"string") ?
"'NULL'" :
"NULL";
228 if (columnType.equals(
"string")) {
229 return "'" + colVal +
"'";
230 }
else if (columnType.equals(
"float") || columnType.equals(
"double")) {
233 DecimalFormat df =
new DecimalFormat(
"#.##################################");
234 double doubleVal = Double.parseDouble(colVal);
235 return df.format(doubleVal);
246 throws SQLException, NumberFormatException {
248 String[] queries = queryString.trim().split(
";");
249 for (String query: queries) {
250 query = query.trim();
251 if (query.indexOf(
" ") > -1) {
252 if (query.substring(0, query.indexOf(
" ")).equalsIgnoreCase(
"use")) {
253 String[] split_query = query.split(
" ");
254 String db_name = split_query[split_query.length - 1];
255 client.changeDatabase(db_name);
256 client.getStatement().
close();
260 long startTime = System.currentTimeMillis();
261 ResultSet res = client.execQuery(query);
262 ResultSetMetaData meta = res.getMetaData();
263 ArrayList<String> arrayList = Lists.newArrayList();
267 LOG.info(
"----[START]----");
271 for (
int i = 1; i <= meta.getColumnCount(); ++i) {
274 arrayList.add(colVal);
276 LOG.info(Joiner.on(
",").join(arrayList));
279 LOG.info(
"----[END]----");
280 long endTime = System.currentTimeMillis();
281 float seconds = (endTime - startTime) / 1000F;
282 LOG.info(
"Returned " + rowCount +
" row(s) in " + seconds +
"s");
286 client.getStatement().
close();
294 public static void main(String[] args)
throws SQLException, ClassNotFoundException,
299 PatternLayout layout =
new PatternLayout(
"%m%n");
300 ConsoleAppender consoleAppender =
new ConsoleAppender(layout);
301 LOG.addAppender(consoleAppender);
302 LOG.setAdditivity(
false);
307 ImpalaJdbcClient.createClientUsingHiveJdbcDriver(execOptions.getConnStr());
311 execQuery(client, execOptions.getQuery());
static final String HIVE_SERVER2_DRIVER_NAME
static final String SASL_AUTH_SPEC
static void execQuery(ImpalaJdbcClient client, String queryString)
static String formatColumnValue(String colVal, String columnType)
static final String DEFAULT_CONNECTION_STRING
static ImpalaJdbcClient createClientUsingHiveJdbcDriver(String connString)
static ClientExecOptions parseOptions(String[] args)
static void main(String[] args)
void changeDatabase(String db_name)
Connection getConnection()
ResultSet execQuery(String query)
ImpalaJdbcClient(String driverName, String connString)
ClientExecOptions(String connStr, String query)
static final String NOSASL_AUTH_SPEC
void validateConnection()
static ImpalaJdbcClient createClientUsingHiveJdbcDriver()