2016-08-11 159 views
0

我有一個被定義爲Chararray的列(名爲Product),它有三個值:OT,AT和HP。我想創建一個新的列和整數變換此值:Apache PIG - 基於另一個值創建一個新列

  1. OT = 1
  2. AT = 2
  3. HP = 3

對於我創建一個foreach statment:

REGISTER '/usr/lib/pig/piggybank.jar'; 

File = load '/user/cloudera/file.csv' 
    USING org.apache.pig.piggybank.storage.CSVExcelStorage(',') 
     as (ID:Long, 
      Chain:Int, 
      Dept:Int, 
      Product_Measure:Chararray, 
      Price:Double); 


Values = FOREACH File Generate 
          ID, 
              Chain, 
              Dept, 
              ((Chararray)Product_Measure=='OT'?'1':(Chararray)Product_Measure=='AT'?'2':(Chararray)Product_Measure=='HP'?'3':'0') as Product_Measure, 
              (Price<0.1?0:Price) as Price; 

Filter_Values = FILTER Values BY Price > 0; 

DUMP Filter_Values; 

如果刪除第三行它工作正常,所以我認爲問題是當我嘗試轉換int的chararray。

任何人都可以幫助我嗎?

謝謝!

回答

0
Values = FOREACH Source Generate 
         ID, 
         Date, 
         ((Chararray)Product == 'OT' ? (int)1 : (Chararray)Product_Measure == 'AT' ? (int)2 : (Chararray)Product_Measure == 'HP' ? (int)3 : 0) as Product_Value, 
        (Quantity<0?0:Quantity) as Quantity, 
        (Price<0.1?0:Price) as Price; 

,或者如果你想NULL,則

Values = FOREACH Source Generate 
          ID, 
          Date, 
          ((Chararray)Product == 'OT' ? '1' : (Chararray)Product_Measure == 'AT' ? '2' : (Chararray)Product_Measure == 'HP' ? '3' : 'NULL') as Product_Value, 
         (Quantity<0?0:Quantity) as Quantity, 
         (Price<0.1?0:Price) as Price; 

兩個修改u需要在你的豬腳本來執行。 月1日到位的=只是把== 2日如果u想null價值將其轉換爲其他chararray全部換成價值int

+0

嗨ANKUR, 我」仍然得到錯誤:(我已經更新了我的代碼 – SaCvP