更新时间:2024-06-27 GMT+08:00
Impala开发示例
JDBC二次开发示例代码
以下示例代码主要功能如下。
- 普通(非Kerberos)模式下,使用用户名和密码进行登录,如不指定用户,则匿名登录;
- 在JDBC URL地址中提供登录Kerberos用户的principal,程序自动完成安全登录、建立Impala连接。
- 执行创建表、查询和删除三类Impala SQL语句。
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(); } } } }
父主题: Impala开发规范