您的位置:首页 > 运维架构 > Apache

【云星数据---Apache Flink实战系列(精品版)】:Apache Flink高级特性与高级应用019-Flink中参数传递和容错设定003

2017-11-20 10:44 996 查看

3.通过ExecutionConfig向Function传递参数

执行程序

package code.book.batch.sinksource.scala

import org.apache.flink.api.common.functions.RichMapFunction
import org.apache.flink.api.scala.{DataSet, ExecutionEnvironment, _}
import org.apache.flink.configuration.Configuration

/**
* Globally via the ExecutionConfig
* *
* Flink also allows to pass custom configuration values to the ExecutionConfig
* interface of the environment. Since the execution config is accessible in all
* (rich) user functions, the custom configuration will be available globally in all functions.
*/
object Parameters003scala {
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.准备工作月份数据,作为参数用Configuration传递出去
val conf = new Configuration()
conf.setString("month", "4")
env.getConfig.setGlobalJobParameters(conf)

//3.接受参数进行计算(如果要用Configuration传参,需要用RichFunction接受)
workers.map(new RichMapFunction[Worker, Worker] {
private var m = 0

override def open(parameters: Configuration): Unit = {
super.open(parameters)
//3.1获取Configuration传递过来的参数
val globalParams = this.getRuntimeContext.getExecutionConfig.getGlobalJobParameters
m = globalParams.toMap.get("month").trim.toInt
}

override def map(w: Worker): Worker = {
//3.2计算最新工人工资信息
Worker(w.name, w.salaryPerMonth * m)
}
}).print
}
}


执行效果

Worker(zhagnsan,5426.68)
Worker(lisi,5906.68)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐