Hive uses JDBC encoding to obtain external partition table data

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.