2014-12-29 51 views
-1

我正在嘗試將數據從配置單元導出到Teradata。下面是我的代碼有相同的:executeQuery不返回配置單元的結果

/* Code Start */ 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class HiveToTd { 
private final static String tdUser = "**********"; 
private final static String tdPass = "**********"; 

private final static String hiveUser = "**********"; 
private final static String hivePass = "**********"; 

private static String driverName = "org.apache.hive.jdbc.HiveDriver"; 
/** 
* @param args 
* @throws ClassNotFoundException 
* @throws SQLException 
*/ 
public static void main(String[] args) throws ClassNotFoundException, SQLException { 
    // Get the Teradata connection 
    Class.forName("com.teradata.jdbc.TeraDriver"); 
    Connection tdcon = DriverManager.getConnection("jdbc:teradata://database.XXXXX.com/TMODE=ANSI,TYPE=FASTLOAD", tdUser, tdPass); 
    System.out.println("Connected to Teradata."); 

    // Get our hive connection 
    Class.forName(driverName); 
    System.out.println("Connecting to Hive."); 
    Connection hivecon = DriverManager.getConnection("jdbc:hive2://bigdatabase.xxxxxx.com:10000/default", hiveUser, hivePass); 
    System.out.println("Connected to Hive."); 

    // Select our table from Hive 

    Statement hst = hivecon.createStatement(); 
    System.out.println("Executing Statement"); 
    ResultSet hrs = hst.executeQuery("SELECT COL1, COL2, COL3 FROM db.table limit 100"); 

    System.out.println("Get DATA"); 
    int count= 0; 
    if(hrs.next()) 
    { 
     count++; 
    } 
    System.out.println(count); 

計數返回「1」,而不是100.我覈實,我有超過1萬條記錄在蜂巢的表格中。我究竟做錯了什麼? 它只是返回我的標題行,就是這樣。我會認爲這個問題與連接有關,但它會給我正確的標題行。所以它必須是別的東西。

我需要解決這個迫切....感謝堆!

所以看起來像代碼實際上工作。感謝您幫助我解決Thusitha問題。

下一部分更麻煩。這適用於快速加載到TD。

// Empty the staging table 
    tdcon.createStatement().executeUpdate("delete from dbname.staging_table"); 

    // Create prepared statement for Teradata 
    System.out.println("Begin load to Teradata"); 
    tdcon.setAutoCommit(false); 

    PreparedStatement ps = tdcon.prepareStatement("insert into dbname.staging_table values (?,?,?)"); 
    System.out.println("Start Fastload"); 
    int i; 
    for (i = 1; hrs.next(); i++){ 
     ps.setString(1, hrs.getString(1)); 
     ps.setString(2, hrs.getString(2)); 
     ps.setString(3, hrs.getString(3)); 
     ps.addBatch(); 
     System.out.println(i); 
     if (i % 10000 == 0){ 
      ps.executeBatch(); 
     } 
    } 



    if (i % 10000 != 0){ 
     ps.executeBatch(); 
    } 



    tdcon.commit(); 
    tdcon.setAutoCommit(true); 
    ps.close(); 
    hrs.close(); 

    sideLoad("dbname.staging_table", "dbname.final_table", tdcon); 
    tdcon.close(); 
    hivecon.close(); 

} 

public static int sideLoad(String fromTable, String toTable, Connection conn) throws SQLException{ 
    return (conn.createStatement().executeUpdate("INSERT INTO " + toTable + " SELECT * FROM " + fromTable)); 

} 

}

後「開始Fastload」的消息我得到的錯誤是:

Exception in thread "main" java.sql.SQLException: [Teradata JDBC Driver] [TeraJDBC 15.00.00.20] [Error 1103] [SQLState HY000] Cannot add an empty batch of rows to a database table 
    at com.teradata.jdbc.jdbc_4.util.ErrorFactory.makeDriverJDBCException(ErrorFactory.java:94) 
    at com.teradata.jdbc.jdbc_4.util.ErrorFactory.makeDriverJDBCException(ErrorFactory.java:64) 
    at com.teradata.jdbc.jdbc.fastload.FastLoadManagerPreparedStatement.executeBatch(FastLoadManagerPreparedStatement.java:2049) 
    at com.optus.insights.HiveToTd.main(HiveToTd.java:84) 

回答

0

你有一個if條件 所以指望它只會去的第一行結果集和增量計數1

如果要遍歷所有行,請使用while loop而不是if 改變你的代碼如下

while(hrs.next()){ 
    count++; 
} 
+0

然而,當我調試的代碼,結果集中顯示沒有行獲取 –

+0

直接執行查詢和檢查,如果你得到的數據 –

+0

完成,它... –