Impala Development Guidelines
Description
This section describes how to complete an analysis task using a sample program. It specifically uses the JDBC API as an example to illustrate how to submit a data analysis task.
Sample Code
- Change the value of the following variable to false. This specifies that the authentication mode for the connected clusters is normal mode.
// Indicates whether the authentication mode of the connected cluster is security mode. boolean isSecureVer = false;
- Define Impala SQL. Impala SQL must be a single statement and cannot contain semicolons (;).
// Define HQL, which cannot contain the semicolon (;). String[] sqls = {"CREATE TABLE IF NOT EXISTS employees_info(id INT,name STRING)", "SELECT COUNT(*) FROM employees_info", "DROP TABLE employees_info"}; - Build JDBC URL.
// Build JDBC URL. StringBuilder sBuilder = new StringBuilder( "jdbc:hive2://").append("impalad_ip").append("/"); if (isSecurityMode) { // Security mode sBuilder.append(";auth=") .append(clientInfo.getAuth()) .append(";principal=") .append(clientInfo.getPrincipal()) .append(";"); } else { // Normal mode sBuilder.append(";auth=noSasl"); } String url = sBuilder.toString();
If an Impalad instance is directly connected, a fault on that specific instance will result in Impala access failures.
- Load the Hive JDBC driver.
// Load the Hive JDBC driver. Class.forName(HIVE_DRIVER);
- Enter a valid username, obtain the JDBC connection, and identify the Impala SQL type (DDL or DML). Call the appropriate API to execute the Impala SQL statement, output the column names and query results to the console, and finally, close the JDBC connection.
Connection connection = null; try { // Obtain the JDBC connection. // If an invalid username is provided as the second parameter, the system defaults to using an anonymous user for the login. connection = DriverManager.getConnection(url, "userName", ""); // Create a table. // To import data to a table after the table is created, you can use the LOAD statement. For example, you can import data from HDFS to the table. //load data inpath '/tmp/employees.txt' overwrite into table employees_info; execDDL(connection,sqls[0]); System.out.println("Create table success!"); // Run a query. execDML(connection,sqls[1]); // Delete the table. execDDL(connection,sqls[2]); System.out.println("Delete table success!"); } finally { // Close the JDBC connection. 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 { // Run the Impala SQL statement. statement = connection.prepareStatement(sql); resultSet = statement.executeQuery(); // Output the queried column name to the 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(); // Output the query result to the 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(); } } }
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.
For any further questions, feel free to contact us through the chatbot.
Chatbot