【雲星資料---Apache Flink實戰系列(精品版)】:Apache Flink高階特性與高階應用018-Flink中引數傳遞和容錯設定002

雲星資料-大資料團隊發表於2017-11-20

2.通過構造方法向Function傳遞引數(複合資料)

執行程式

package code.book.batch.sinksource.scala
import org.apache.flink.api.common.functions.MapFunction
import org.apache.flink.api.scala.{DataSet, ExecutionEnvironment, _}
object Parameters002 {
  def main(args: Array[String]): Unit = {
    val env = ExecutionEnvironment.getExecutionEnvironment

    //1.準備工人資料
    case class Worker(name: String, salaryPerMonth: Double)
    val workers: DataSet[Worker] = env.fromElements(
      Worker("zhagnsan", 1356.67),
      Worker("lisi", 1476.67)
    )
    //2.準備工作月份資料,作為引數傳遞出去
    val month = 4

    //3.接受引數進行計算
    workers.map(new SalarySumMap(month)).print()
    class SalarySumMap(m: Int) extends MapFunction[Worker, Worker] {
      override def map(w: Worker): Worker = {
        //取出引數,取出worker進行計算
        Worker(w.name, w.salaryPerMonth * m)
      }
    }
  }
}

執行效果

Worker(zhagnsan,5426.68)
Worker(lisi,5906.68)

相關文章