Apache Flink 定義
Apache Flink是一個(gè)框架和分布式處理引擎,用于對(duì)無界和有界數(shù)據(jù)流進(jìn)行狀態(tài)計(jì)算。Flink設(shè)計(jì)為在所有常見的集群環(huán)境中運(yùn)行,以內(nèi)存速度和任何規(guī)模執(zhí)行計(jì)算。
設(shè)置:下載并啟動(dòng)Flink
Flink可在Linux,Mac OS X和Windows上運(yùn)行。為了能夠運(yùn)行Flink,唯一的要求是安裝Java 8.x。Windows用戶,請(qǐng)查看Windows上的Flink指南,該指南介紹了如何在Windows上運(yùn)行Flink以進(jìn)行本地設(shè)置。
您可以通過發(fā)出以下命令來檢查Java的正確安裝:
java -version
如果你有Java 8,輸出將如下所示:
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
下載和解壓縮
- 從下載頁(yè)面下載二進(jìn)制文件。您可以選擇任何您喜歡的Hadoop / Scala組合。如果您打算只使用本地文件系統(tǒng),任何Hadoop版本都可以正常工作。
- 轉(zhuǎn)到下載目錄。
- 解壓縮下載的存檔。
$ cd ~/Downloads # Go to download directory
$ tar xzf flink-*.tgz # Unpack the downloaded archive
$ cd flink-1.8.0
對(duì)于MacOS X用戶,可以通過Homebrew安裝Flink 。
$ brew install apache-flink ...
$ flink --version Version: 1.8.0, Commit ID: 4caec0d
啟動(dòng)本地Flink群集
$ ./bin/start-cluster.sh # Start Flink
檢查分派器的web前端在HTTP://本地主機(jī):8081,并確保一切都正常運(yùn)行。Web前端應(yīng)報(bào)告單個(gè)可用的TaskManager實(shí)例。
您還可以通過檢查logs目錄中的日志文件來驗(yàn)證系統(tǒng)是否正在運(yùn)行:
$ tail log/flink-*-standalonesession-*.log
INFO ... - Rest endpoint listening at localhost:8081
INFO ... - http://localhost:8081 was granted leadership ...
INFO ... - Web frontend listening at http://localhost:8081.
INFO ... - Starting RPC endpoint for StandaloneResourceManager at akka://flink/user/resourcemanager .
INFO ... - Starting RPC endpoint for StandaloneDispatcher at akka://flink/user/dispatcher .
INFO ... - ResourceManager akka.tcp://flink@localhost:6123/user/resourcemanager was granted leadership ...
INFO ... - Starting the SlotManager.
INFO ... - Dispatcher akka.tcp://flink@localhost:6123/user/dispatcher was granted leadership ...
INFO ... - Recovering all persisted jobs.
INFO ... - Registering TaskManager ... under ... at the SlotManager.
閱讀代碼
您可以在Scala中找到此SocketWindowWordCount示例的完整源代碼,并在GitHub上找到Java。
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 <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)
}
JAVA
public class SocketWindowWordCount {
public static void main(String[] args) throws Exception {
// the port to connect to
final int port;
try {
final ParameterTool params = ParameterTool.fromArgs(args);
port = params.getInt("port");
} catch (Exception e) {
System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>'");
return;
}
// get the execution environment
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// get input data by connecting to the socket
DataStream<String> text = env.socketTextStream("localhost", port, "\n");
// parse the data, group it, window it, and aggregate the counts
DataStream<WordWithCount> windowCounts = text
.flatMap(new FlatMapFunction<String, WordWithCount>() {
@Override
public void flatMap(String value, Collector<WordWithCount> out) {
for (String word : value.split("\\s")) {
out.collect(new WordWithCount(word, 1L));
}
}
})
.keyBy("word")
.timeWindow(Time.seconds(5), Time.seconds(1))
.reduce(new ReduceFunction<WordWithCount>() {
@Override
public WordWithCount reduce(WordWithCount a, WordWithCount b) {
return new WordWithCount(a.word, a.count + b.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
public static class WordWithCount {
public String word;
public long count;
public WordWithCount() {}
public WordWithCount(String word, long count) {
this.word = word;
this.count = count;
}
@Override
public String toString() {
return word + " : " + count;
}
}
}
運(yùn)行示例
現(xiàn)在,我們將運(yùn)行此Flink應(yīng)用程序。它將從套接字讀取文本,并且每5秒打印一次前5秒內(nèi)每個(gè)不同單詞的出現(xiàn)次數(shù),即處理時(shí)間的翻滾窗口,只要文字漂浮在其中。
- 首先,我們使用netcat來啟動(dòng)本地服務(wù)器
$ nc -l 9000
- 提交Flink計(jì)劃:
$ ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000 Starting execution of program
程序連接到套接字并等待輸入。您可以檢查Web界面以驗(yàn)證作業(yè)是否按預(yù)期運(yùn)行:
- 單詞在5秒的時(shí)間窗口(處理時(shí)間,翻滾窗口)中計(jì)算并打印到stdout。監(jiān)視TaskManager的輸出文件并寫入一些文本nc(輸入在點(diǎn)擊后逐行發(fā)送到Flink):
$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
該.out文件將在每個(gè)時(shí)間窗口結(jié)束時(shí),只要打印算作字浮在,例如:
$ tail -f log/flink-*-taskexecutor-*.out
lorem : 1
bye : 1
ipsum : 4
要停止Flink 所要做的操作:
$ ./bin/stop-cluster.sh