参考:flink中idea配置pom.xml_flink项目pom文件_hzp666的博客-CSDN博客
win10安装idea
IntelliJ IDEA 2021.1 (Ultimate Edition)
Build #IU-211.6693.111, built on April 6, 2021
Licensed to IntelliJ IDEA Evaluator
Expiration date: March 29, 2023
Runtime version: 11.0.10+9-b1341.35 amd64
VM: Dynamic Code Evolution 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 3024M
Cores: 6
Registry: scala.erase.compiler.process.jdk.once=false
Non-Bundled Plugins: org.example.BetterIntelliJ (1.16), org.intellij.scala (2021.1.18)
Kotlin: 211-1.4.32-release-IJ6693.72
java1.8 maven项目
WordCount.scala
package wordcount
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
object WordCount {
/** Main program method */
def main(args: Array[String]) : Unit = {
// get the execution environment
val env:StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
import org.apache.flink.streaming.api.scala._
val text:DataStream[String] = env.socketTextStream("192.168.0.1",9000,'\n')
// parse the data, group it, window it, and aggregate the counts
val windowCounts = text.flatMap (_.split("\\s") )
.map((_,1))
.keyBy(0)
.sum(1)
// print the results with a single thread, rather than in parallel
windowCounts
.print()
.setParallelism(1)
env.execute("Socket Window WordCount")
}
/** Data type for words with count */
case class WordWithCount(word: String, count: Long)
}
pom.xml文件
scala 2.12.2
flink1.14.4
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ddyii.flink</groupId>
<artifactId>flinkScipt</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-scala -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-scala_2.12</artifactId>
<version>1.14.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-scala -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_2.12</artifactId>
<version>1.14.4</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_2.12</artifactId>
<version>1.14.4</version>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
project structure
自己下载的scala安装包
运行输入
nc -l 9000
hello world hahaha
this is a flink test
who am i
ok stream api ok
this is a flink test
who is this