- 新建类
package test01;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class TestUnion {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment(new Configuration());
executionEnvironment.setParallelism(1);
//Union合并流的流数据类型必须一致,如果不一致,需要在合并前先转换类型,使其一致
//创建要合并的流
DataStreamSource<String> stream1 = executionEnvironment.fromElements("a", "b", "c");
DataStreamSource<String> stream2 = executionEnvironment.fromElements("aa", "bb", "cc");
DataStreamSource<String> stream3 = executionEnvironment.fromElements("aaa", "bbb", "ccc");
//方式一:多次调用union
//DataStream<String> union = stream1.union(stream2).union(stream3);
//方式二:一次调用,中间用逗号隔开
DataStream<String> union = stream1.union(stream2, stream3);
union.print();
executionEnvironment.execute();
}
}
- 运行程序
方式一:
方式二:文章来源:https://www.toymoban.com/news/detail-560249.html
文章来源地址https://www.toymoban.com/news/detail-560249.html
到了这里,关于Flink DataStream之Union合并流的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!