原文链接:https://ci.apache.org/projects/flink/flink-docs-release-1.3/quickstart/setup_quickstart.html
Setup: Download and Start Flink
Flink可以运行在Linux、Mac OS X以及Windows中,Flink运行的唯一条件就是安装Java
7.X以上的版本的jdk。Windows用户请查看一下Flink on Windows文档,这个文档描述了如何在window运行单机的Flink。Flink on Windows:https://ci.apache.org/projects/flink/flink-docs-release-1.3/setup/flink_on_windows.html
你可以通过下面的命令行来查看安装的Java版本是否正确:
java -version
如果你安装的是Java 8的话,会返回下面的信息:
java version"1.8.0_111"
Java(TM)SE Runtime Environment(build 1.8.0_111-b14)
Java HotSpot(TM)64-Bit Server VM(build 25.111-b14, mixed mode)
Downloadand Compile
从Flink的代码库中clone代码,如下:
$git clone https://github.com/apache/flink.git
$cdflink
$mvn clean package -DskipTests# this will take up to 10 minutes
$cdbuild-target# this is where Flink is installed to
Starta Local Flink Cluster
$./bin/start-local.sh# Start Flink
通过http://localhost:8081来检查JobManager的Web前台,确保每一个进程都起来了。在这个Web前台中应该只有一个TaskManager实例。
还可以通过检查日志目录中的日志文件来判断系统是否正常运行
$tail log/flink-*-jobmanager-*.log
INFO ... - Starting JobManager
INFO ... - Starting JobManager web frontend
INFO ... - Web frontend listening at 127.0.0.1:8081
INFO ... - Registered TaskManager at 127.0.0.1(akka://flink/user/taskmanager)
Readthe Code
你可以在GitHub中查看到这个SocketWindowWordCount实例完整的Java代码和Scala代码。
Scala:
object SocketWindowWordCount {
def main(args: Array[String]) : Unit = { // the port to connect to
val port: Int = try {
ParameterTool.fromArgs(args).getInt("port")
} catch {
case e: Exception => {
System.err.println("No port specified. Please run 'SocketWindowWordCount --port'")
return
}
}
// get the execution environment
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
// get input data by connecting to the socket
val text = env.socketTextStream("localhost", port, '\n')
// parse the data, group it, window it, and aggregate the counts
val windowCounts = text.flatMap { w => w.split("\\s") }
.map { w => WordWithCount(w, 1) }
.keyBy("word")
.timeWindow(Time.seconds(5), Time.seconds(1))
.sum("count")
// 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)
}
Runthe Example
现在我们将去执行这个Flink程序,这个程序将去读取socket中产生的文本,并且每隔5秒打印一下前5秒内产生的不同的单次产生的次数。
首先,我们通过netcat来打开一个本地的服务:
$nc -l 9000
提交Flink程序
$./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000
Cluster configuration: Standalone cluster with JobManager at /127.0.0.1:6123
Using address 127.0.0.1:6123 to connect to JobManager.
JobManager web interface address http://127.0.0.1:8081
Starting execution of program
Submitting job with JobID: 574a10c8debda3dccd0c78a3bde55e1b. Waitingforjob completion.
Connected to JobManager at Actor[akka.tcp://flink@127.0.0.1:6123/user/jobmanager#297388688]
11/04/2016 14:04:50Job execution switched to status RUNNING.
11/04/2016 14:04:50Source: Socket Stream -> Flat Map(1/1)switched to SCHEDULED
11/04/2016 14:04:50Source: Socket Stream -> Flat Map(1/1)switched to DEPLOYING
11/04/2016 14:04:50Fast TumblingProcessingTimeWindows(5000)of WindowedStream.main(SocketWindowWordCount.java:79)-> Sink: Unnamed(1/1)switched to SCHEDULED
11/04/2016 14:04:51Fast TumblingProcessingTimeWindows(5000)of WindowedStream.main(SocketWindowWordCount.java:79)-> Sink: Unnamed(1/1)switched to DEPLOYING
11/04/2016 14:04:51Fast TumblingProcessingTimeWindows(5000)of WindowedStream.main(SocketWindowWordCount.java:79)-> Sink: Unnamed(1/1)switched to RUNNING
11/04/2016 14:04:51Source: Socket Stream -> Flat Map(1/1)switched to RUNNING
程序将与socket连接并等待输入,你可以通过web前台来查看作业是否如预期执行。
单词在一个间隔5秒的window(窗口)中执行并且打印到stdout中。监控JobManager的输出文件并写些文档到nc中。
$nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
只要单词源源不断的流入的话,.out文件将在时间窗口的最后截止时间打印出单词的计数:例如:
$tail -f log/flink-*-jobmanager-*.out
lorem : 1
bye : 1
ipsum : 4
运行结束后可以停掉Flink:
$./bin/stop-local.sh