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

被折叠的 条评论
为什么被折叠?



