Flink流处理广播变量-java版

本文介绍了在Java中使用Flink进行流处理时如何操作广播变量,通过实例展示了如何将广播变量与数据集结合,实现数据整合操作。
摘要由CSDN通过智能技术生成

总结:其实还是两个数据集的整合操作

package waterChuli.flinkDetil;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple5;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 
 *
 * https://www.pianshen.com/article/60011255975/
 * nc -lp 9999
 */
public class Gongxiang {
   
    private static final Logger LOG = LoggerFactory.getLogger(Gongxiang.class);

    public static void main(String[] args) {
   
        try {
   
            StreamExecutionEnvironment fsEnv = StreamExecutionEnvironment.getExecutionEnvironment();
            DataStreamSource<Tuple2<Integer, String>> ds1 = fsEnv.fromElements(Tuple2.of(1, "男"), Tuple2.of(2, "女"));

            //自定义状态描述器
            MapStateDescriptor<Integer, String> genderInfo= new MapStateDescriptor(
                    "genderInfo",
                    BasicTypeInfo.INT_TYPE_INFO,
                    BasicTypeInfo.CHAR_TYPE_INFO
            );
            //通过将ds1将自己进行广播
            BroadcastStream<Tuple2<Integer,String>> bcStream=ds1.broadcast(genderInfo);


            SingleOutputStreamOperator<Tuple5<Integer, String, Integer, String, Double>> ds2 = fsEnv.socketTextStream("localhost", 9999)
                    .map(new MapFunction<String, Tuple5<Integer, String, Integer, String, Double>>() {
   
                        @Override
                        public Tuple5<Integer, String, Integer, String, Double> map(String s) throws Exception {
   
                            String[] strings=s.split(",");
                            int id= Integer.valueOf(strings[0]);
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值