Apache Flink 是一个框架和分布式处理引擎,用于处理无界和有界的数据流进行状态计算。
数据报表、广告投放、业务流程需要
传感器实时数据采集和显示、实时报警、交通运输业
基站流量调配
实时结算和通知推送,实时监测异常行为
https://www.apache.org/dyn/closer.lua/flink/flink-1.10.1/flink-1.10.1-bin-scala_2.11.tgz
maven依赖
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-scala_2.11</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_2.11</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.4.6</version>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在resource目录下创建一个hello.txt文件
hello word
hello scala
hello fink
hello spark
/** * 批处理 word count 程序 */ object WordCount { def main(args: Array[String]): Unit = { // 创建一个执行环境 val env = ExecutionEnvironment.getExecutionEnvironment // 从文件中读取数据 val inputPath = "/Users/qiunan/IdeaProjects/FlinkTutorial/src/main/resources/hello.txt" val inputDataSet = env.readTextFile(inputPath) // 切分数据得到word 然后再按word做分组聚合 val wordCountDataSet = inputDataSet.flatMap(_.split(" ")) .map((_,1)) .groupBy(0) .sum(1) wordCountDataSet.print() } }
打印结果
(scala,1) (spark,1) (word,1) (fink,1) (hello,4)
/** * 流处理word count 程序 */ object StreamWordCount { def main(args: Array[String]): Unit = { // 创建流处理的执行环境 val env = StreamExecutionEnvironment.getExecutionEnvironment // 接受一个socket文本流 val dataStream = env.socketTextStream("localhost", 777); // 对每条数据进行处理 val wordCountDataStream = dataStream.flatMap(_.split(" ")) .filter(_.nonEmpty) .map((_, 1)) .keyBy(0) .sum(1) wordCountDataStream.print() // 启动executor env.execute(); } }
启动程序 在终端窗口输入 nc -lk 777
qiunan@qiunandeiMac ~ % nc -lk 777
hello word
hello flink
hello spark
打印结果
5> (word,1) 2> (hello,1) 5> (flink,1) 2> (hello,2) 2> (hello,3) 1> (spark,1)
原文:https://www.cnblogs.com/south-pigeon/p/13127015.html