2014-08-27 92 views
1

我正在使用豬0.12版本,並且我想使用參考生成動態 IN條件。在豬中動態生成IN條件

在我的豬文件

我有「m_master」關係,當我說DESCRIBE m_master它給了我下面的

m_master: {m_id: chararray,m_name: chararray,in_dx: chararray,rolled_up_name: chararray,match_code: chararray,match0: chararray,flag_ind: chararray} 

現在我要像

UPDATE M_Master SET flag_ind='SE' WHERE Rolled_Up_Name IN (SELECT DISTINCT Rolled_Up_Name FROM M_Master WHERE flag_ind='SE') AND flag_ind='Non SE' 

等於執行一些操作RDBMS查詢。

我已經產生了從m_master不同roll_up_names這是refered作爲distinct_rollup_names

m_master = FOREACH m_master GENERATE m_id, m_name, in_dx, rolled_up_name, match_code, match0, 
    (
     (
      flag_ind='Non SE' AND rolled_up_name IN (<b>distinct_rollup_names</b>) 
     ) ? 'SE' : flag_ind 
    ) as flag_ind; 

如何,有什麼建議請

回答

0

豬不IN子句支持,你是使用IN條件中所產生的關係值期待。自加入對rolled_up_name列m_master,然後更新左側flag_ind到SE如果非SE,如果右側flag_ind是SE

--Original m_master 
m_master: {m_id: chararray,m_name: chararray,in_dx: chararray,rolled_up_name: chararray,match_code: chararray,match0: chararray,flag_ind: chararray} 

-- Clone m_master into m_master2 
m_master2 = FOREACH m_master GENERATE m_id, m_name, in_dx, rolled_up_name, match_code, match0, flag_ind; 

-- We are interested only in SE flag_ind (this works as inner query in your question) 
m_master2 = filter m_master2 by flag_ind == 'SE'; 

-- Now join m_master and m_master2 
m_master_self_joined = JOIN m_master BY rolled_up_name LEFT OUTER, m_master2 BY rolled_up_name; 

-- Now pick fields from m_master 
-- When there is a match with m_master2, set flag_ind to SE 
m_master_self_joined2 = FOREACH m_master_self_joined 
         GENERATE 
          m_master::m_id, 
          m_master::m_name, 
          m_master::in_dx, 
          m_master::rolled_up_name, 
          m_master::match_code, 
          m_master::match0, 
          (m_master::m_id == null ? 'Non SE' : 'SE'); 

-- Its possible to have duplicates (if rolled_up_name is not unique), so take uniques 
m_master_self_joined3 = DISTINCT m_master_self_joined2; 

希望這有助於

+1

爲您的信息PIG 0.12支持,我已經用它在其他一些場景 – 2014-09-06 07:09:26

+0

謝謝@PrasadKhode,我不知道在0.12版本支持IN。 – 2014-09-06 11:26:34