本地安装教程

只需几个简单的步骤即可启动并运行Flink示例程序。

Flink可在Linux,Mac OSX和Windows上运行为了能够运行Flink,唯一的要求是安装一个有效的Java8.x. Windows用户,请查看Windows上的Flink指南,该指南介绍了如何在Windows上运行Flink以进行本地设置。

您可以通过发出以下命令来检查Java正确安装:

  1. java -version

如果你有Java 8,输出将如下所示:

  1. java version "1.8.0_111"
  2. Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
  3. Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

下载并编译

从我们的某个存储库克隆源代码,例如:

  1. $ git clone https://github.com/apache/flink.git
  2. $ cd flink
  3. $ mvn clean package -DskipTests # this will take up to 10 minutes
  4. $ cd build-target # this is where Flink is installed to
  1. $ ./bin/start-cluster.sh # Start Flink

检查分派器的web前端HTTP://localhost:8081,并确保一切都正常运行。Web前端应报告单个可用的TaskManager实例。

调度员:概述

您还可以通过检查logs目录中的日志文件来验证系统是否正在运行

  1. $ tail log/flink-*-standalonesession-*.log
  2. INFO ... - Rest endpoint listening at localhost:8081
  3. INFO ... - http://localhost:8081 was granted leadership ...
  4. INFO ... - Web frontend listening at http://localhost:8081.
  5. INFO ... - Starting RPC endpoint for StandaloneResourceManager at akka://flink/user/resourcemanager .
  6. INFO ... - Starting RPC endpoint for StandaloneDispatcher at akka://flink/user/dispatcher .
  7. INFO ... - ResourceManager akka.tcp://[email protected]:6123/user/resourcemanager was granted leadership ...
  8. INFO ... - Starting the SlotManager.
  9. INFO ... - Dispatcher akka.tcp://[email protected]:6123/user/dispatcher was granted leadership ...
  10. INFO ... - Recovering all persisted jobs.
  11. INFO ... - Registering TaskManager ... under ... at the SlotManager.

阅读代码

您可以在ScalaJava上的GitHub上找到此SocketWindowWordCount示例的完整源代码

  1. object SocketWindowWordCount {
  2. def main(args: Array[String]) : Unit = {
  3. // the port to connect to
  4. val port: Int = try {
  5. ParameterTool.fromArgs(args).getInt("port")
  6. } catch {
  7. case e: Exception => {
  8. System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>'")
  9. return
  10. }
  11. }
  12. // get the execution environment
  13. val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
  14. // get input data by connecting to the socket
  15. val text = env.socketTextStream("localhost", port, '\n')
  16. // parse the data, group it, window it, and aggregate the counts
  17. val windowCounts = text
  18. .flatMap { w => w.split("\\s") }
  19. .map { w => WordWithCount(w, 1) }
  20. .keyBy("word")
  21. .timeWindow(Time.seconds(5), Time.seconds(1))
  22. .sum("count")
  23. // print the results with a single thread, rather than in parallel
  24. windowCounts.print().setParallelism(1)
  25. env.execute("Socket Window WordCount")
  26. }
  27. // Data type for words with count
  28. case class WordWithCount(word: String, count: Long)
  29. }
  1. public class SocketWindowWordCount {
  2. public static void main(String[] args) throws Exception {
  3. // the port to connect to
  4. final int port;
  5. try {
  6. final ParameterTool params = ParameterTool.fromArgs(args);
  7. port = params.getInt("port");
  8. } catch (Exception e) {
  9. System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>'");
  10. return;
  11. }
  12. // get the execution environment
  13. final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  14. // get input data by connecting to the socket
  15. DataStream<String> text = env.socketTextStream("localhost", port, "\n");
  16. // parse the data, group it, window it, and aggregate the counts
  17. DataStream<WordWithCount> windowCounts = text
  18. .flatMap(new FlatMapFunction<String, WordWithCount>() {
  19. @Override
  20. public void flatMap(String value, Collector<WordWithCount> out) {
  21. for (String word : value.split("\\s")) {
  22. out.collect(new WordWithCount(word, 1L));
  23. }
  24. }
  25. })
  26. .keyBy("word")
  27. .timeWindow(Time.seconds(5), Time.seconds(1))
  28. .reduce(new ReduceFunction<WordWithCount>() {
  29. @Override
  30. public WordWithCount reduce(WordWithCount a, WordWithCount b) {
  31. return new WordWithCount(a.word, a.count + b.count);
  32. }
  33. });
  34. // print the results with a single thread, rather than in parallel
  35. windowCounts.print().setParallelism(1);
  36. env.execute("Socket Window WordCount");
  37. }
  38. // Data type for words with count
  39. public static class WordWithCount {
  40. public String word;
  41. public long count;
  42. public WordWithCount() {}
  43. public WordWithCount(String word, long count) {
  44. this.word = word;
  45. this.count = count;
  46. }
  47. @Override
  48. public String toString() {
  49. return word + " : " + count;
  50. }
  51. }
  52. }

运行示例

现在,我们将运行此Flink应用程序。它将从套接字读取文本,并且每5秒打印一次前5秒内每个不同单词的出现次数,即处理时间的翻滚窗口,只要文字漂浮在其中。

  • 首先,我们使用netcat来启动本地服务器
  1. $ nc -l 9000
  • 提交Flink计划:
  1. $ ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000
  2. Starting execution of program

程序连接到套接字并等待输入。您可以检查Web界面以验证作业是否按预期运行:

调度员:概述(续)

调度程序:运行作业

  • 单词在5秒的时间窗口(处理时间,翻滚窗口)中计算并打印到stdout。监视TaskManager的输出文件并写入一些文本nc(输入在点击后逐行发送到Flink):
  1. $ nc -l 9000
  2. lorem ipsum
  3. ipsum ipsum ipsum
  4. bye

.out文件将在每个时间窗口结束时,只要打印算作字浮在,例如:

  1. $ tail -f log/flink-*-taskexecutor-*.out
  2. lorem : 1
  3. bye : 1
  4. ipsum : 4

停止Flink当你做类型:

  1. $ ./bin/stop-cluster.sh

下一步

查看更多示例以更好地了解Flink的编程API。完成后,请继续阅读流处理指南