原文鏈接:https://ci.apache.org/projects/flink/flink-docs-release-1.3/quickstart/setup_quickstart.html
Setup: Download and Start Flink
Flink可以運(yùn)行在Linux狰右、Mac OS X以及Windows中,F(xiàn)link運(yùn)行的唯一條件就是安裝Java
7.X以上的版本的jdk抓半。Windows用戶請(qǐng)查看一下Flink on Windows文檔递礼,這個(gè)文檔描述了如何在window運(yùn)行單機(jī)的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的話垒手,會(huì)返回下面的信息:
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前臺(tái)单刁,確保每一個(gè)進(jìn)程都起來了。在這個(gè)Web前臺(tái)中應(yīng)該只有一個(gè)TaskManager實(shí)例猜拾。
還可以通過檢查日志目錄中的日志文件來判斷系統(tǒng)是否正常運(yùn)行
$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中查看到這個(gè)SocketWindowWordCount實(shí)例完整的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
現(xiàn)在我們將去執(zhí)行這個(gè)Flink程序,這個(gè)程序?qū)⑷プx取socket中產(chǎn)生的文本挎袜,并且每隔5秒打印一下前5秒內(nèi)產(chǎn)生的不同的單次產(chǎn)生的次數(shù)顽聂。
首先,我們通過netcat來打開一個(gè)本地的服務(wù):
$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
程序?qū)⑴csocket連接并等待輸入盯仪,你可以通過web前臺(tái)來查看作業(yè)是否如預(yù)期執(zhí)行紊搪。
單詞在一個(gè)間隔5秒的window(窗口)中執(zhí)行并且打印到stdout中。監(jiān)控JobManager的輸出文件并寫些文檔到nc中全景。
$nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
只要單詞源源不斷的流入的話耀石,.out文件將在時(shí)間窗口的最后截止時(shí)間打印出單詞的計(jì)數(shù):例如:
$tail -f log/flink-*-jobmanager-*.out
lorem : 1
bye : 1
ipsum : 4
運(yùn)行結(jié)束后可以停掉Flink:
$./bin/stop-local.sh