Apache Flink是一个框架和分布式处理引擎,用于对无界和有界数据流进行状态计算。Flink设计为在全部常见的集群环境中运行,之内存速度和任何规模执行计算。html
Flink可在Linux,Mac OS X和Windows上运行。为了可以运行Flink,惟一的要求是安装Java 8.x。Windows用户,请查看Windows上的Flink指南,该指南介绍了如何在Windows上运行Flink以进行本地设置。前端
您能够经过发出如下命令来检查Java的正确安装:java
java -version
git
若是你有Java 8,输出将以下所示:github
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)
复制代码
$ cd ~/Downloads # Go to download directory
$ tar xzf flink-*.tgz # Unpack the downloaded archive
$ cd flink-1.8.0
复制代码
对于MacOS X用户,能够经过Homebrew安装Flink 。web
$ brew install apache-flink ...
$ flink --version Version: 1.8.0, Commit ID: 4caec0d
复制代码
$ ./bin/start-cluster.sh # Start Flink
复制代码
检查分派器的web前端在HTTP://本地主机:8081,并确保一切都正常运行。Web前端应报告单个可用的TaskManager实例。 apache
您还能够经过检查logs目录中的日志文件来验证系统是否正在运行:windows
$ 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。bash
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;
}
}
}
复制代码
如今,咱们将运行此Flink应用程序。它将从套接字读取文本,而且每5秒打印一次前5秒内每一个不一样单词的出现次数,即处理时间的翻滚窗口,只要文字漂浮在其中。
$ nc -l 9000
复制代码
$ ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000 Starting execution of program
复制代码
程序链接到套接字并等待输入。您能够检查Web界面以验证做业是否按预期运行:
$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
复制代码
该.out文件将在每一个时间窗口结束时,只要打印算做字浮在,例如:
$ tail -f log/flink-*-taskexecutor-*.out
lorem : 1
bye : 1
ipsum : 4
复制代码
要中止Flink 所要作的操做:
$ ./bin/stop-cluster.sh
复制代码