Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. ANALYZE TABLE p_7 COMPUTE STATISTICS noscan
  2.  
  3. Exception in thread "main" java.sql.SQLException: The query did not generate a result set!
  4. at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:393)
  5. at HiveJdbcClient.main(HiveJdbcClient.java:22)
  6.  
  7. import java.sql.SQLException;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.Statement;
  11.  
  12. import java.sql.DriverManager;
  13.  
  14. public class HiveJdbcClient {
  15. private static String driverName = "org.apache.hive.jdbc.HiveDriver";
  16. public static void main(String[] args) throws SQLException {
  17. try {
  18. Class.forName(driverName);
  19. } catch (ClassNotFoundException e) {
  20.  
  21. e.printStackTrace();
  22. System.exit(1);
  23. }
  24. Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
  25. System.out.println("connected");
  26. Statement statement = con.createStatement();
  27. String query = "ANALYZE TABLE p_7 COMPUTE STATISTICS noscan";
  28. ResultSet res = statement.executeQuery(query);
  29. }
  30. }
  31.  
  32. ANALYZE TABLE p_7 COMPUTE STATISTICS noscan
  33.  
  34. DESCRIBE FORMATTED tableName
  35.  
  36. String analyzeQuery = "ANALYZE TABLE p_7 COMPUTE STATISTICS";
  37. String describeQuery = "DESCRIBE FORMATTED p_7";
  38.  
  39. stmt.execute(analyzeQuery);
  40. StringBuilder sb = new StringBuilder();
  41. try (ResultSet rs = stmt.executeQuery(describeQuery)) {
  42. while (rs.next()) {
  43. int count = rs.getMetaData().getColumnCount();
  44. for (int j = 1; j <= count; j++) {
  45. sb.append(rs.getString(j));
  46. }
  47. }
  48. }
  49. System.out.println("Output: "+ sb.toString());
  50.  
  51. public static Connection createConnection(String hive_ip)
  52. {
  53. String hive_url="jdbc:hive2://"+hive_ip;
  54. Connection con=null;
  55. try {
  56. Class.forName("org.apache.hive.jdbc.HiveDriver");
  57. System.out.println(hive_url+"/");
  58. con = DriverManager.getConnection(
  59. hive_url+"/",
  60. hive_username,hive_password);
  61.  
  62. } catch (ClassNotFoundException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. } catch (SQLException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69.  
  70. return con;
  71. }
  72.  
  73.  
  74.  
  75. public static int getHiveColumnRowCount(String tablename,String db_name)
  76. {
  77. int count=0;
  78.  
  79. Connection con=createConnection();
  80.  
  81. try {
  82. Statement st=con.createStatement();
  83.  
  84. int i=0;
  85. String count_query="show tblproperties "+db_name+"."+tablename;
  86. ResultSet rs=st.executeQuery(count_query);
  87. while(rs.next())
  88. {
  89. i++;
  90. if(i==3)
  91. {
  92. count=Integer.parseInt(rs.getString(2));
  93. }
  94. }
  95. System.out.println("COUNT:"+count);
  96. rs.close();
  97. st.close();
  98. con.close();
  99. } catch (SQLException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103.  
  104.  
  105. return count;
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement