I need to compute the number of rows in a hive table, for that I am using the query:
ANALYZE TABLE p_7 COMPUTE STATISTICS noscan
I want to fetch the results through java, I am trying with the below code and have no luck. the error I get is :
Exception in thread "main" java.sql.SQLException: The query did not generate a result set!
at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:393)
at HiveJdbcClient.main(HiveJdbcClient.java:22)
code I am using is :
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
System.out.println("connected");
Statement statement = con.createStatement();
String query = "ANALYZE TABLE p_7 COMPUTE STATISTICS noscan";
ResultSet res = statement.executeQuery(query);
}
}
I dont know how to execute a query such as:
ANALYZE TABLE p_7 COMPUTE STATISTICS noscan
through java. Any help on this would be of great help to me. Thanks.
Try the below code for getting number of rows of a table:
Hope it helps :)
Use the ANALYZE TABLE statement without 'NOSCAN' to compute the number of rows. Note: This statement does not produce resultSet object.
To fetch the stored stats, use the following statement.
In the output, the number of rows is listed in parameters array. Use regex to extract it.
Here is the sample code:
Refer https://cwiki.apache.org/confluence/display/Hive/StatsDev for details on Table and Partition statistics.