flink2-有界流计算wordcount

流式处理分为有界流和无界流
1)有界流
有界流就是读取的数据是有界限的,在流不关闭的情况下,能读取到数据的尽头
2)无界流
在人为不干预的情况下,流一直在,不会停,数据没有边界

package day01;


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

public class Flink02_wordCount_Bounded {
    public static void main(String[] args) throws Exception {
        //流式处理,有界流
        //获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //读取文件
        DataStream<String> DS = env.readTextFile("input");
        //对每行数据进行flatMap
        SingleOutputStreamOperator<String> words = DS.flatMap(new Flink01_wordcount_batch.MyFlatMap());
        //改变每个单词的结构:word->(word,1)
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOne = words.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s) throws Exception {
                return new Tuple2<>(s,1);
            }
        });
        //按照key进行分组,但是在流处理中,没有groupBy,有keyBY,需要传入KeySelector对象,输入参数是tuple2,输出只要key进行分组就行
        KeyedStream<Tuple2<String, Integer>, String> wordGroup = wordToOne.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
            @Override
            public String getKey(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
                return stringIntegerTuple2.f0;
            }
        });
        //将分组好的数据进行聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> result = wordGroup.sum(1);
        //打印结果
        result.print();
        //开启流
        env.execute("wordcount");


    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flink WordCountFlink 的一个经典示例,用于演示 Flink 流处理的基本功能。它是一个简单的单词计数程序,可以从输入文本中读取单词,并计算每个单词在文本中出现的次数。 以下是一个 Flink WordCount 的示例代码: ```java import org.apache.flink.api.java.utils.ParameterTool; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.source.FileSource; import org.apache.flink.streaming.api.functions.source.SourceFunction; import org.apache.flink.streaming.api.windowing.time.Time; public class WordCount { public static void main(String[] args) throws Exception { final ParameterTool params = ParameterTool.fromArgs(args); // 设置执行环境 final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 设置数据源 DataStream<String> text; if (params.has("input")) { text = env.readTextFile(params.get("input")); } else { System.out.println("Executing WordCount example with default input data set."); System.out.println("Use --input to specify file input."); text = env.fromElements(WordCountData.WORDS); } // 转换数据流 DataStream<WordWithCount> counts = text.flatMap(new Tokenizer()) .keyBy("word") .timeWindow(Time.seconds(5)) .sum("count"); // 输出结果 if (params.has("output")) { counts.writeAsText(params.get("output")); } else { System.out.println("Printing result to stdout. Use --output to specify output path."); counts.print(); } // 执行任务 env.execute("Streaming WordCount"); } // 单词拆分函数 public static final class Tokenizer implements FlatMapFunction<String, WordWithCount> { @Override public void flatMap(String value, Collector<WordWithCount> out) { String[] tokens = value.toLowerCase().split("\\W+"); for (String token : tokens) { if (token.length() > 0) { out.collect(new WordWithCount(token, 1L)); } } } } // 单词计数类 public static final class WordWithCount { public String word; public long count; public WordWithCount() {} public WordWithCount(String word, long count) { this.word = word; this.count = count; } @Override public String toString() { return word + " : " + count; } } } ``` 该程序使用 Flink 流处理 API 来读取输入文本、拆分单词、计数单词并输出结果。程序的具体执行流程如下: 1. 读取命令行参数或默认数据源。 2. 创建 Flink 执行环境。 3. 读取数据源。 4. 转换数据流,拆分单词并计数。 5. 输出结果到文件或标准输出。 6. 执行任务。 如果你想要运行 Flink WordCount 示例程序,可以按照以下步骤进行: 1. 下载 Flink 并解压。 2. 打开终端并进入 Flink 的安装目录。 3. 运行 `./bin/start-cluster.sh` 启动 Flink 集群。 4. 运行 `./bin/flink run examples/streaming/WordCount.jar --input /path/to/input/file --output /path/to/output/file`。 5. 等待程序执行完成,查看输出结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值