2014-03-12 24 views
2

我想對我的管道的所有字段應用操作。我在https://github.com/twitter/scalding/wiki/Fields-based-API-Reference 上看到, 「您可以使用'*(這裏和其他地方)來表示所有字段。」 但不知何故,我沒有成功,使其工作。有人會友好地向我展示一個例子嗎?燙傷如何使用'*關鍵字映射所有字段?

起初我有一些像

mySource.map('field1 -> 'field1){ number: String => number.trim } 

我現在想申請到喜歡的東西

mySource.map('* -> '*){ numbers: List[String] => numbers.map(_.trim) } 

所有領域?

回答

1

在滾燙的領域API,以便從'*映射到'*,我能想到的最好的辦法是級聯TupleEntrycascading.tuple.TupleEntry

import com.twitter.scalding._ 
import cascading.tuple.TupleEntry 

// Notice I do not specify the scheme when reading. 
// I only know first column is 'user_id', the rest is some value and I want 
// to double the values. You can use 'map' or 'mapTo'. 
Tsv(args("input")) 
    .read 
    .map('* -> '*) { 
    fields: TupleEntry => 
    val sz: Int = fields.size() 
    for (i <- from 1 until sz) fields.setDouble(i, fields.getDouble(i) * 2.0) 
    fields.getTuple() 
    } 
    .write(Tsv(args("output"))) 
+0

謝謝,太棒了! –

0

'*運營商似乎只使用mapTo和完整類型的註釋。

mySource 
    .mapTo[(String,String,String),(String,String,String)]('* -> '*) { case (a: String, b: String, c: String) => 
    (a.trim, b.trim, c.trim) 
    } 
0

例如,這可與滾燙的0.11.0(既不擔任他們目前的答案)的:

mySource 
    .mapTo('* -> '*) { 
     entry: TupleEntry => 
     for (i <- 0 until entry.size) { 
      if (entry.getObject(i) == null) entry.setRaw(i, "\\N") 
     } 
     entry.getTuple 
    } 

所以基本上mapTo('* -> '*) - >entry.getTuple