Due to the requirements of recent projects, the function of implementing HQL statements in command line mode using hive JDBC encoding is studied carefully. During the period, we encountered a lot of problems, and analyzed and solved them. But time was in a hurry, and I was unable to record the problems I had encountered. With 00 sparse memories, these questions and experiences are expected to be summed up for later use.
A requirement in the project is to query the data in the Hive External partition table through the HQL conditional query statement and save the data to a local file. The main similar code for directly pasting a query is as follows:
String drivername = "Org.apache.hadoop.hive.jdbc.HiveDriver";
try {
Class.forName (drivername);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.exit (1);
}
Connection con = drivermanager.getconnection ("Jdbc:hive://localhost:10000/default", "", "" ");
Statement stmt = Con.createstatement ();
ResultSet rs=stmt.executequery ("select * FROM MyTable limit 10");
while (Rs.next ()) {
System.out.println (rs.getstring (0));//First column as String type data
}
However, when you run the code, an exception appears, prompting the following:
Java.sql.SQLException:java.lang.ArrayIndexOutOfBoundsException:-1
The error of the report is an array of cross-border anomalies (strange! This exception did not occur when the column values were previously obtained in the normal hive table in this way. ), the solution is baffled. Try to get the value of the first column by using the Get method of other points of resultset, still the datagram group is out of bounds or the method does not support the error, it seems that the JDBC mode of hive does not implement all the ResultSet methods.
Finally, the line with the exception is modified, using the GetString method of Rs to invoke the column name parameter, the result is successful. The modified code looks like this:
String drivername = "Org.apache.hadoop.hive.jdbc.HiveDriver";
try {
Class.forName (drivername);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.exit (1);
}
Connection con = drivermanager.getconnection ("Jdbc:hive://localhost:10000/default", "", "" ");
Statement stmt = Con.createstatement ();
ResultSet rs=stmt.executequery ("select * FROM MyTable limit 10");
ResultSetMetaData RSMD = Rs.getmetadata ();
int Count=rsmd.getcolumncount ();
String[] Name=new String[count];
for (int i=0;i<count;i++)
Name[i]=rsmd.getcolumnname (i+1);
while (Rs.next ()) {
System.out.println (rs.getstring (0));
for (int i=0;i<count;i++)
System.out.print (Rs.getstring (name[i]) + ",");
System.out.println ();
}
Summary: In the JDBC encoding of hive implementation of the HQL statement function, the ResultSet get+ type method may not always support the transfer of index parameters, we have to consider a different way of thinking, using the method of passing the column name to get the value.