2015-11-05 80 views
0

執行我需要計算在蜂巢表中的行數,對於 我使用的查詢:蜂巢「分析表」如何從Java

ANALYZE TABLE p_7 COMPUTE STATISTICS noscan 

我想獲取通過Java結果,我想用下面的 代碼,並沒有運氣。我得到的錯誤是:我使用

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) 

代碼:

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); 
} 
} 

我不知道如何執行查詢,例如:

ANALYZE TABLE p_7 COMPUTE STATISTICS noscan 

通過Java。任何幫助對我來說都是很有幫助的。謝謝。

+0

確實從p_7'工作'SELECT COUNT(*)? –

+0

是的,選擇數量(*)會起作用,但是它會花費太多時間來完成。 – whitetiger

回答

1

使用不帶'NOSCAN'的ANALYZE TABLE語句來計算行數。 注意:此語句不會生成resultSet對象。

要獲取存儲的統計信息,請使用以下語句。

DESCRIBE FORMATTED tableName 

在輸出中,行數在參數數組中列出。使用正則表達式來提取它。

下面是示例代碼:

String analyzeQuery = "ANALYZE TABLE p_7 COMPUTE STATISTICS"; 
String describeQuery = "DESCRIBE FORMATTED p_7"; 

stmt.execute(analyzeQuery); 
StringBuilder sb = new StringBuilder(); 
try (ResultSet rs = stmt.executeQuery(describeQuery)) { 
    while (rs.next()) { 
     int count = rs.getMetaData().getColumnCount(); 
     for (int j = 1; j <= count; j++) { 
      sb.append(rs.getString(j)); 
     } 
    } 
} 
System.out.println("Output: "+ sb.toString()); 

參考https://cwiki.apache.org/confluence/display/Hive/StatsDev關於表的細節和分區統計信息。

0

嘗試爲得到一個表的行數下面的代碼:

public static Connection createConnection(String hive_ip) 
     { 
      String hive_url="jdbc:hive2://"+hive_ip; 
      Connection con=null; 
      try { 
       Class.forName("org.apache.hive.jdbc.HiveDriver"); 
       System.out.println(hive_url+"/"); 
       con = DriverManager.getConnection(
         hive_url+"/", 
         hive_username,hive_password); 

      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return con; 
     } 



     public static int getHiveColumnRowCount(String tablename,String db_name) 
      { 
       int count=0; 

       Connection con=createConnection(); 

       try { 
        Statement st=con.createStatement(); 

        int i=0; 
        String count_query="show tblproperties "+db_name+"."+tablename; 
        ResultSet rs=st.executeQuery(count_query); 
        while(rs.next()) 
        { 
         i++; 
         if(i==3) 
         { 
         count=Integer.parseInt(rs.getString(2)); 
         } 
        } 
        System.out.println("COUNT:"+count); 
        rs.close(); 
        st.close(); 
        con.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


       return count; 

      } 

希望它能幫助:)