2015-06-20 73 views
1

我已經作出了Java腳本,將連接用Hiveserver2蜂巢,將創建表和管理表,對於簡單的創建,刪除設置蜂巢配置屬性hive.exec.dynamic.partition,插入數據工作正常。如何從Java代碼

我想創建一個分區外部表,爲了這個,我需要改變值以下蜂巢財產,

hive.exec.dynamic.partition = true 
hive.exec.dynamic.partition.mode = nonstrict 

在蜂巢CLI我可以使用SET和屬性名做,但如何這可以在java代碼中完成。

這裏是我的Java代碼:

public class HiveJdbcClient { 

    private static String strDriverName = "org.apache.hive.jdbc.HiveDriver"; 
    public static void main(String[] args) throws SQLException { 
     try{ 
     Class.forName(strDriverName); 
     } catch (ClassNotFoundException e){ 
      e.printStackTrace(); 
      System.out.println("No class found"); 
      System.exit(1); 
     } 
     Connection con = DriverManager.getConnection("jdbc:hive2://172.11.1.11:10000/default","root","root123"); 
     Statement stmt = con.createStatement(); 
     String strTableName = "testtable"; 

     //stmt.execute("drop table " + strTableName); 
     //creating staging table that will load the data to partition data 

     String strStagingTableSql = "create table if not exists "+strTableName+"_staging "+ " (SEQUENCE_NO DECIMAL, DATE_KEY INT, ACTIVITY_TIME_KEY INT, Ds_KEY INT, Ds_VALUE DECIMAL, TL_DATE_KEY INT) ROW FORMAT DELIMITED FIELDS TERIMANTED BY '~'"; 
     String strMainTableSql = "create external table if not exists "+strTableName+" (SEQUENCE_NO DECIMAL, ACTIVITY_TIME_KEY INT, Ds_KEY INT, Ds_VALUE DECIMAL, TL_DATE_KEY INT) PARTITIONED BY (DATE_KEY INT) ROW FORMAT DELIMITED FIELDS TERMINATED BY '~' LOCATION '/informatica/dwh/teradata/testtable'"; 
     String strCreateSql = "create external table if not exists "+ strTableName + " (key int, value string) row format delimited fields terminated by ','"; 
     boolean res = stmt.execute(strCreateSql); 
     //show tables 
     String sql = "show tables '" + strTableName + "'"; 
     ResultSet res1 = stmt.executeQuery(sql); 

     if (res1.next()){ 
      System.out.println(res1.getString(1)); 
     } 

     sql = "describe "+ strTableName; 
     System.out.println("Running: "+ sql); 
     res1 = stmt.executeQuery(sql); 
     while (res1.next()){ 
      System.out.println(res1.getString(1) + "\t" + res1.getString(2)); 
     } 

     // load data into table 
     // NOTE: filepath has to be local to the hive server 
     // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line 
     String strFilepath = "/informatica/testing_hive_client_java.txt"; 
     sql = "load data inpath '" + strFilepath + "' into table " + strTableName; 
     System.out.println("Running: " + sql); 
     res = stmt.execute(sql); 

     sql = "select count(1) from "+ strTableName; 
     System.out.println("Running: "+ sql); 
     res1 = stmt.executeQuery(sql); 
     while(res1.next()){ 
      System.out.println(res1.getString(1)); 
     } 



    }// end of main 

}// end of class 

請各位高手倒在你的看法。

回答

1

我能夠通過以下代碼解決我的問題。

boolean resHivePropertyTest = stmt 
       .execute("SET hive.exec.dynamic.partition = true"); 
     resHivePropertyTest = stmt 
       .execute("SET hive.exec.dynamic.partition.mode = nonstrict"); 

正如代碼爲JDBC客戶端代碼,所以執行將只是去和蜂巢等爲我工作執行此。

+0

我面臨同樣的問題,這個解決方案非常棘手。在這裏,我們只需要使用'java SQL API'提交配置單元語句,hive將負責執行那些語句/查詢,並且由於java連接API,我們會使用我們設置的整個屬性來保存配置單元會話。 –

+0

嘿,這不適合我。它執行得很好,但沒有效果。你可以打印返回值,它返回false – narush