Updated on 2023-09-01 GMT+08:00

Rules

Load the Hive JDBC Driver

A client program connects to Impalad using JDBC. You need to load the JDBC driver class org.apache.hive.jdbc.HiveDriver of Hive first.

Therefore, you must load the driver class using the current class loader before starting the client program.

If there is no JAR file in classpath, the client program throws "Class Not Found" and exits.

Example:

Class.forName("org.apache.hive.jdbc.HiveDriver").newInstance();

Obtain a Database Connection

Use the driver management class java.sql.DriverManager of the JDK to obtain an Impalad database connection.

The URL of the Impalad database is as follows: url="jdbc:hive2://xxx.xxx.xxx.xxx:21050;auth=noSasl;principal=impala/hadoop.hadoop.com@HADOOP.COM;user.principal=impala/hadoop.hadoop.com;user.keytab=conf/impala.keytab";

The username and password are null or empty because authentication has been performed successfully.

//Set up a connection.

connection = DriverManager.getConnection(url, "", "");

// Execute Impala SQL

Execute Impala SQL. Note that Impala SQL cannot end with a semicolon (;).

Correct example:

String sql = "SELECT COUNT(*) FROM employees_info";
Connection connection = DriverManager.getConnection(url, "", "");
PreparedStatement statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();

Incorrect example:

String sql = "SELECT COUNT(*) FROM employees_info;";
Connection connection = DriverManager.getConnection(url, "", "");
PreparedStatement statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();

Use Impala SQL Syntax to Check Whether a Field is Null

Use is null to check whether a field is empty, that is, the field has no value. Use is not null to check whether a field is not null, that is, the field has a value.

If you use is null to check a field whose type is String and length is 0, False is returned. In this case, you need to use col = '' to check null character strings, and col != '' to check non-null character strings.

Correct example:

select * from default.tbl_src where id is null;
select * from default.tbl_src where id is not null;
select * from default.tbl_src where name = '';
select * from default.tbl_src where name != '';

Incorrect example:

select * from default.tbl_src where id = null;
select * from default.tbl_src where id != null;
select * from default.tbl_src where name is null;
select * from default.tbl_src where name is not null; Note: The id field in the tbl_src table is of the Int type, and the name field is of the String type.

Multithread Security Login Mode

If multiple threads are performing login operations, the relogin mode must be used for the subsequent logins of all threads after the first successful login of an application.

Login sample code:

  private Boolean login(Configuration conf){
    boolean flag = false;
    UserGroupInformation.setConfiguration(conf);
    
    try {
      UserGroupInformation.loginUserFromKeytab(conf.get(PRINCIPAL), conf.get(KEYTAB));
      System.out.println("UserGroupInformation.isLoginKeytabBased(): " +UserGroupInformation.isLoginKeytabBased());
      flag = true;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return flag;
  }

Relogin sample code:

public Boolean relogin(){
        boolean flag = false;
        try {
            
          UserGroupInformation.getLoginUser().reloginFromKeytab();
          System.out.println("UserGroupInformation.isLoginKeytabBased(): " +UserGroupInformation.isLoginKeytabBased());
          flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

Do Not Perform Read and Write Operations on the Same Table at the Same Time

In the current version, Hive does not support concurrent operations. Therefore, do not perform read and write operations on the same table at the same time. Otherwise, query results may be inaccurate or even tasks fail.