3. Flink Map operator "In-depth understanding of flink series"

1. Author of the book "In-depth Understanding of Flink Core Design and Practice Principles" 2.
Developer of the popular GitHub project fink-boot (800+), dedicated to the ecological integration of flink and spring 3. Former technical manager of a listed top consulting company, now a senior developer of a large factory 4. Certificate: software designer, Apache kylin administrator 5. Direction: java server development, distributed development, real-time computing, big data
development
engineer

The Map transformation operation applies a user-defined Map function to each element in the data stream. Each element in the data stream will enter the user-defined Map function as an input element, and the Map function will transform the input element and generate a result element to output to the new data stream. The Map conversion operation implements a one-to-one mapping, which means that the user-defined Map function must return exactly one element.

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//创建一个包含指定数字序列的新数据流。
DataStream<Long> streamSource = env.generateSequence(1, 5);

//第一个泛型类型为输入参数的类型,第二个泛型类型为返回结果的类型
DataStream<Tuple2<Long, Integer>> mapStream = streamSource.map(new MapFunction<Long, Tuple2<Long, Integer>>() {
    @Override
    public Tuple2<Long, Integer> map(Long values) throws Exception {
        return new Tuple2<>(values * 100, values.hashCode());
    }
});

mapStream.print("输出结果");
env.execute("Map Template");

See com.intsmaze.flink.streaming.operator.base.MapTemplate for complete code.
output result:

输出结果:5> (500,5)
输出结果:4> (400,4)
输出结果:1> (100,1)
输出结果:2> (200,2)
输出结果:3> (300,3)

Guess you like

Origin blog.csdn.net/hbly979222969/article/details/125035767