原文鏈接: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