Home > database >  1, how to read in the UDF Hive table data, this can be done?
1, how to read in the UDF Hive table data, this can be done?

Time:10-13

 
Package com. Inspur. Udf;

import java.io.IOException;
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;

Import org, apache hadoop. Conf. Configuration;
Import org, apache hadoop. Security. UserGroupInformation;

Public class start_prior {

Public static String the evaluate (String table, a String column, String column_sj, String para) throws ClassNotFoundException {
Boolean check_re=true;
The StringBuilder sb=new StringBuilder ();
The StringBuilder sb_tmp=new StringBuilder ();
Sb. Append (para);

String driverName="org. Apache. Hive. JDBC. HiveDriver";
The String url="JDBC: hive2://10.111.30.41:10000/default; principal=hive/[email protected] ";

Configuration conf=new Configuration();
The conf. Set (" hadoop. Security. Authentication ", "Kerberos");

Try {
System. SetProperty (" Java. Security. Krb5. Conf ", "D: \ \ workspace \ \ UDF - MD5 \ \ SRC \ \ the main \ \ kerberos \ \ krb5 conf");
UserGroupInformation. SetConfiguration (conf);
hive/[email protected] UserGroupInformation. LoginUserFromKeytab (" ", "D: \ \ workspace \ \ UDF - MD5 \ \ SRC \ \ the main \ \ kerberos \ \ hive service. Keytab");
} the catch (IOException e) {
e.printStackTrace();
}

Try {
Class.forname (driverName);
The Connection conn=DriverManager. GetConnection (url);
PreparedStatement STMT=null;
The ResultSet ResultSet=null;

Do {
String SQL="select" + column + "from" + "where" + + table column_sj + "in (para" + + ") ";
System. The out. Println (SQL);
STMT=conn. PrepareStatement (SQL);
The resultSet=STMT. ExecuteQuery ();

If (a resultSet. Next ()) {
Do {
Sb_tmp. Append (resultSet. Get string (1) + ", ");
Sb. Append (", "+ resultSet. Get string (1));
} while (resultSet. Next ());
Para.=sb_tmp deleteCharAt (sb_tmp length () - 1). The toString ();
Sb_tmp. Delete (0, sb_tmp. Length ());
} else {
Check_re=false;
}

} while (check_re);

The resultSet. Close ();
conn.close();
} the catch (Exception e) {
e.printStackTrace();
}
Return sb. ToString ();
}
}