Examples
Sample Code of JDBC Secondary Development
The following sample code provides the following functions:
- In normal (non-Kerberos) mode, uses the username and password for login. If no user is specified, anonymous login is used.
- Provides the principal for logging in to the Kerberos user in the JDBC URL so that programs can automatically perform secure logins and create Impala connections.
- Runs Impala SQL statements for creating, querying, and deleting tables.
package com.huawei.bigdata.impala.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* Simple example for hive jdbc.
*/
public class JDBCExample {
private static final String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
private ClientInfo clientInfo;
private boolean isSecurityMode;
public JDBCExample(ClientInfo clientInfo, boolean isSecurityMode){
this.clientInfo = clientInfo;
this.isSecurityMode = isSecurityMode;
}
/**
*
* @throws ClassNotFoundException
* @throws SQLException
*/
public void run() throws ClassNotFoundException, SQLException {
//Define hive sql, the sql can not include ";"
String[] sqls = {"CREATE TABLE IF NOT EXISTS employees_info(id INT,name STRING)",
"SELECT COUNT(*) FROM employees_info", "DROP TABLE employees_info"};
StringBuilder sBuilder = new StringBuilder(
"jdbc:hive2://").append(clientInfo.getImpalaServer()).append("/");
if (isSecurityMode) {
sBuilder.append(";auth=")
.append(clientInfo.getAuth())
.append(";principal=")
.append(clientInfo.getPrincipal())
.append(";");
} else {
sBuilder.append(";auth=noSasl");
}
String url = sBuilder.toString();
Class.forName(HIVE_DRIVER);
Connection connection = null;
try {
/**
* Get JDBC connection, If not use security mode, need input correct username,
* otherwise, wil login as "anonymous" user
*/
//connection = DriverManager.getConnection(url, "", "");
connection = DriverManager.getConnection(url);
/**
* Run the create table sql, then can load the data if needed. eg.
* "load data inpath '/tmp/employees.txt' overwrite into table employees_info;"
*/
execDDL(connection,sqls[0]);
System.out.println("Create table success!");
execDML(connection,sqls[1]);
execDDL(connection,sqls[2]);
System.out.println("Delete table success!");
}
finally {
if (null != connection) {
connection.close();
}
}
}
public static void execDDL(Connection connection, String sql)
throws SQLException {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
statement.execute();
}
finally {
if (null != statement) {
statement.close();
}
}
}
public static void execDML(Connection connection, String sql) throws SQLException {
PreparedStatement statement = null;
ResultSet resultSet = null;
ResultSetMetaData resultMetaData = null;
try {
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
/**
* Print the column name to console
*/
resultMetaData = resultSet.getMetaData();
int columnCount = resultMetaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
System.out.print(resultMetaData.getColumnLabel(i) + '\t');
}
System.out.println();
/**
* Print the query result to console
*/
while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(resultSet.getString(i) + '\t');
}
System.out.println();
}
}
finally {
if (null != resultSet) {
resultSet.close();
}
if (null != statement) {
statement.close();
}
}
}
} Last Article: Suggestions
Next Article: Alluxio Application Development
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.