Flink练习第三天:转换算子练习--map、flatmap、filter、reduce、simpleagg

目录

map

flatmap

filter

reduce

simpleagg

map

map

package com.atguigu.chapter05;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author psl
 * @create 2022/5/3 10:44
 * @desc  map算子的用法
 */
public class TransformMapTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> stream = env.fromElements(new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L), new Event("Tom", "./prod", 3000L));

        //匿名类实现mapfunction接口
        SingleOutputStreamOperator<String> map1 = stream.map(new MapFunction<Event, String>() {
            @Override
            public String map(Event event) throws Exception {
                return event.user;
            }
        });

        map1.print();


        //lambda表达式
        SingleOutputStreamOperator<String> map2 = stream.map(data -> data.user);
        map2.print();

        env.execute();


    }
}

flatmap

package com.atguigu.chapter05;

import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author psl
 * @create 2022/5/3 11:31
 * @desc flatmap算子的用法
 */
public class TransformFlatMapTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> stream = env.fromElements(new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L), new Event("Tom", "./prod", 3000L));


        stream.flatMap((Event value, Collector<String> out) ->{
            if(value.user.equals("Mary")){
                out.collect(value.url);
            }
            else if(value.user.equals("Bob")){
                out.collect(value.user);
                out.collect(value.url);
                out.collect(value.timestamp.toString());
            }
        }).returns(new TypeHint<String>() {

        }).print();

        env.execute ();


    }
}

filter

package com.atguigu.chapter05;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author psl
 * @create 2022/5/3 11:25
 * @desc filter算子的用法
 */
public class TransformFilterTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> stream = env.fromElements(new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L), new Event("Tom", "./prod", 3000L));

            stream.filter(data -> data.user.equals("Mary")).print("Mary click");

            env.execute();


    }
}

reduce

package com.atguigu.chapter05;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
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;

/**
 * @author psl
 * @create 2022/5/3 13:46
 * @desc reduce算子的使用
 */
public class TransformReduceTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> stream = env.fromElements(new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 3000L),
                new Event("Tom", "./home", 3000L),
                new Event("Bob", "./home", 1000L),
                new Event("Tom", "./home", 3000L),
                new Event("Bob", "./prof", 4400L),
                new Event("Tom", "./prod", 5000L));

        //统计每个用户的访问频次
        SingleOutputStreamOperator<Tuple2<String, Long>> clickByUser = stream.map(new MapFunction<Event, Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> map(Event value) throws Exception {
                        return Tuple2.of(value.user, 1L);
                    }
                }).keyBy(data -> data.f0)
                .reduce(new ReduceFunction<Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> reduce(Tuple2<String, Long> value1, Tuple2<String, Long> value2) throws Exception {
                        return Tuple2.of(value1.f0, value1.f1 + value2.f1);
                    }
                });

        //根据个数选取最活跃用户
        SingleOutputStreamOperator<Tuple2<String, Long>> result = clickByUser.keyBy(data -> "key")
                .reduce(new ReduceFunction<Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> reduce(Tuple2<String, Long> value1, Tuple2<String, Long> value2) throws Exception {
                        return value1.f1 > value2.f1 ? value1 : value2;
                    }
                });

        result.print();
        env.execute();


    }
}

simpleagg

package com.atguigu.chapter05;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author psl
 * @create 2022/5/3 13:13
 * @desc Aggregation算子的用法(keyBy+max+maxBy)
 */
public class TransformSimpleAggTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> stream = env.fromElements(new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 3000L),
                new Event("Bob", "./home", 1000L),
                new Event("Bob", "./prof", 4400L),
                new Event("Tom", "./home", 3000L),
                new Event("Tom", "./prod", 5000L));


        stream.keyBy(data ->data.user)
                .maxBy("timestamp")
                .print();

        stream.keyBy(data ->data.user)
                .max("timestamp")
                .print();



        env.execute();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flink中的算子可以分为三类:转换算子、聚合算子和窗口算子。下面对它们进行详细介绍。 1. 转换算子 转换算子用于将一个数据流转换为另一个数据流,常用的转换算子有: - Map:将每个输入元素应用到一个函数上,输出一个新元素。 - FlatMap:将每个输入元素应用到一个函数上,输出零个、一个或多个新元素。 - Filter:将每个输入元素应用到一个谓词上,输出满足谓词条件的元素。 - KeyBy:根据指定的键将流分组。 - Reduce:对分组后的流中的元素进行归约操作。 2. 聚合算子 聚合算子用于对数据流进行聚合操作,常用的聚合算子有: - Sum:对输入元素进行求和操作。 - Min:对输入元素进行求最小值操作。 - Max:对输入元素进行求最大值操作。 - Count:对输入元素进行计数操作。 3. 窗口算子 窗口算子用于将数据流分割为有限大小的窗口,并对窗口中的元素进行操作,常用的窗口算子有: - Tumbling Window:将数据流分成不重叠的固定大小的窗口。 - Sliding Window:将数据流分成固定大小的窗口,并且这些窗口可以重叠。 - Session Window:将数据流根据一定的时间间隔将数据流分成不固定长度的窗口。 除了以上算子Flink还提供了一些其他的算子,例如: - Union:将两个或多个数据流合并为一个数据流。 - Connect和CoMap:用于将两个数据流连接在一起,并在连接后对两个数据流进行不同的转换操作。 - Iterate:允许在数据流上进行迭代操作。 总结:Flink中的算子非常丰富,可以满足各种需求,通过合理使用这些算子,可以轻松构建出高效、可扩展的实时数据处理系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据架构师Pony

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值