Help Center/
MapReduce Service/
Developer Guide (Normal_3.x)/
Impala Development Guide (Normal Mode)/
Development Specifications/
Examples
Updated on 2022-08-16 GMT+08:00
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(); } } } }
Parent topic: Development Specifications
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot