赞
踩
从上到下包含有界无界流 支持状态 特点 与spark对比 应用场景 架构分层
了解了后就整个demo吧
数据源准备 这里直接用的文本文件
gradle中的主要配置
- group = 'com.example'
- version = '0.0.1-SNAPSHOT'
-
- java {
- sourceCompatibility = '11'
- }
-
- repositories {
- mavenCentral()
- }
-
- dependencies {
- implementation group: 'org.apache.flink', name: 'flink-streaming-java', version: '1.17.0'
- implementation group: 'org.apache.flink', name: 'flink-clients', version: '1.17.0'
-
- }
代码
- package com.example.flinktest.test;
-
- import org.apache.flink.api.common.functions.FlatMapFunction;
- import org.apache.flink.api.java.tuple.Tuple2;
- import org.apache.flink.streaming.api.datastream.DataStreamSource;
- import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
- import org.apache.flink.util.Collector;
-
- public class FlinkTurotial1_17 {
-
- public static void main(String[] args) throws Exception {
-
- //todo 1.创建执行环境
- StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
- env.setParallelism(1);
-
- //todo 2.读取数据
- DataStreamSource<String> stringDataStreamSource = env.readTextFile("D:\\juege\\code\\hope-backend\\opentech\\src\\main\\resources\\flinkTextSource.txt");
-
- //todo 3.进行数据处理 先 flatmap 再 keyby 再 sum 再打印输出
- stringDataStreamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
- @Override
- public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
- String[] words = s.split(" ");
- for (String word : words) {
- if ("".equals(word)) {
- continue;
- }
- collector.collect(new Tuple2<>(word, 1));
- }
- }
- }).keyBy(0).sum(1).print();
-
- //todo 4.执行任务
- env.execute("pantouyu");
- }
-
- }
运行后控制台效果如下
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。