• 【Flink、java】


    依赖 

    1. <dependency>
    2. <groupId>org.apache.flink</groupId>
    3. <artifactId>flink-streaming-java_2.11</artifactId>
    4. <version>1.14.6</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.flink</groupId>
    8. <artifactId>flink-clients</artifactId>
    9. <version>${flink.version}</version>
    10. </dependency>

     快速上手

    1.增添依赖

    2.在根目录,添加input文件

     DataSet API实现wordcount(已经不能用了)

    1. package org.example;
    2. /*
    3. * @Auther:huangzhiyang
    4. * @Date:2023/9/26
    5. * @Description:wc
    6. */
    7. import org.apache.flink.api.common.functions.FlatMapFunction;
    8. import org.apache.flink.api.java.ExecutionEnvironment;
    9. import org.apache.flink.api.java.operators.AggregateOperator;
    10. import org.apache.flink.api.java.operators.DataSource;
    11. import org.apache.flink.api.java.operators.FlatMapOperator;
    12. import org.apache.flink.api.java.operators.UnsortedGrouping;
    13. import org.apache.flink.api.java.tuple.Tuple2;
    14. import org.apache.flink.util.Collector;
    15. public class wordCountBatchDemo {
    16. public static void main(String[] args) throws Exception {
    17. // TODO: 2023/9/26 创建执行环境
    18. ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    19. // TODO: 2023/9/26 读取数据
    20. DataSource<String> lineDS = env.readTextFile("input/word.txt");
    21. // TODO: 2023/9/26 切分转换
    22. FlatMapOperator<String, Tuple2<String, Integer>> wordAndOne = lineDS.flatMap(new FlatMapFunction>() {
    23. @Override
    24. public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
    25. // TODO: 2023/9/26 按照空格切分单词
    26. String[] words = s.split(" ");
    27. // TODO: 2023/9/26 将单词转为tuple2
    28. for (String word : words) {
    29. Tuple2<String, Integer> tuple2 = Tuple2.of(word, 1);
    30. // TODO: 2023/9/26 使用collector向下游发送数据
    31. collector.collect(tuple2);
    32. }
    33. }
    34. });
    35. // TODO: 2023/9/26 按照word分组
    36. UnsortedGrouping<Tuple2<String, Integer>> wordAndOneGroupBY = wordAndOne.groupBy(0);
    37. // TODO: 2023/9/26 各分组内聚合
    38. AggregateOperator<Tuple2<String, Integer>> sum = wordAndOneGroupBY.sum(1);//1是位置,表示第二个元素
    39. // TODO: 2023/9/26 输出
    40. sum.print();
    41. }
    42. }

  • 相关阅读:
    uniapp——上传图片获取到file对象而非临时地址——基础积累
    【第八章 IDEA中常用快捷键】
    综合案例:学成在线案例
    计算机网络笔记2 物理层
    2022年Q2加密市场投融资报告:GameFi成为投资关键词
    【PS-6】视图操作
    『现学现忘』Docker相关概念 — 8、虚拟化技术和容器技术的关系
    【矩阵论】4. 矩阵运算——广义逆——减号逆
    Unity自定义属性面板Inspector面板和userData数据保存
    尚品汇项目2
  • 原文地址:https://blog.csdn.net/David_Hzy/article/details/133322235