richfunction 广播变量 发表于 2017-02-21 | 分类于 flink 1234567891011121314151617181920212223242526272829303132333435package com.examples.infoworld.helloword;import org.apache.flink.api.common.functions.RichMapFunction;import org.apache.flink.api.java.DataSet;import org.apache.flink.api.java.ExecutionEnvironment;import org.apache.flink.configuration.Configuration;import java.util.Collection;public class RichfunctionTest2 { public static void main(String[] args) throws Exception { ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); DataSet<Integer> ds1 = env.fromElements(1,2,3 ); DataSet<String> ds2 = env.fromElements("a","b" ); DataSet<String> ds3 = ds2.map(new RichMapFunction<String, String>() { private Collection<Integer> broadcastSet; @Override public void open(Configuration conf) throws Exception { broadcastSet = getRuntimeContext().getBroadcastVariable("ds1"); } @Override public String map(String s) throws Exception { for (Integer i:broadcastSet){ System.out.println(i); } return s; } }).withBroadcastSet(ds1,"ds1"); ds3.print(); }}