Flink 对算子的Function接口实现

该博客介绍了如何在 Apache Flink 中使用自定义类和匿名类来实现 MapFunction,以处理数据流中的字符串并将其转换为大写。示例代码展示了如何创建DataStreamSource,应用map操作,并执行流处理任务。

1.创建类实现Function接口

通过新建类去实现MapFunction,并自定义数据处理逻辑。

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
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.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
import java.time.Duration;
import org.apache.flink.streaming.api.datastream.DataStream;
//用类去实现function
class MyMapFunction implements MapFunction<String,String>
{
    @Override
    public String map(String value) throws Exception
    {
        return value.toUpperCase();
    }
};
public class test {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.创建Class实现Function接口
        DataStreamSource<String> inputStream = env.socketTextStream("localhost", 7777);
        DataStream<String> map_result=inputStream.map(new MyMapFunction());
        map_result.print();
        env.execute("run map");

    }
}
;

2.利用匿名类去实现function

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
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.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
import java.time.Duration;
import org.apache.flink.streaming.api.datastream.DataStream;
//创建匿名类去实现function
public class test2{
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.创建Class实现Function接口
        DataStreamSource<String> inputStream = env.socketTextStream("localhost", 7777);
        DataStream<String> map_result=inputStream.map(
                new MapFunction<String,String>()
                {
            @Override
            public String map(String value) throws Exception
            {
                return value.toUpperCase();
            }
                }
        );
        map_result.print();
        env.execute("run map");

    }
}
;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值