Flink的简单Hello World示例

时间:2019-12-15 19:08:01

标签: apache-flink

我正在寻找使用Apache flink进行打招呼的最简单示例。

假设我刚刚在一个干净的盒子上安装了flink,那么我要做的最低工作是什么?我意识到这很模糊,下面是一些示例。

来自终端的三个python示例:

python -c "print('hello world')"
python hello_world.py
python python -c "print(1+1)"

当然,流应用程序要复杂一些,但是这与我之前在火花流处理中所做的类似:

https://spark.apache.org/docs/latest/streaming-programming-guide.html#a-quick-example

如您所见,这些示例具有一些不错的属性:

  1. 它们很小
  2. 对其他工具/资源的依赖程度最低
  3. 可以微调逻辑(例如,不同的数字或不同的分隔符)

所以我的问题:

Flink最简单的世界示例是什么


到目前为止,我发现的示例包含需要编译的50行代码。

如果由于第3点而无法避免这种情况,那么满足第1点和第2点并使用(仅)默认情况下出厂的罐子或易于从信誉良好的来源获得的罐子的东西也将是可以的。

3 个答案:

答案 0 :(得分:2)

好吧,这怎么办

public static void main(String[] args) throws Exception {
  final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

  env.fromElements(1, 2, 3, 4, 5)
    .map(i -> 2 * i)
    .print();

  env.execute();
}

答案 1 :(得分:1)

在大多数大数据及相关框架中,我们以Word Count程序作为Hello World示例。以下是Flink中的单词计数代码:

final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    DataSet<String> text = env.fromCollection(Arrays.asList("This is line one. This is my line number 2. Third line is here".split(". ")));

    DataSet<Tuple2<String, Integer>> wordCounts = text
        .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
          @Override
          public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (String word : line.split(" ")) {
              out.collect(new Tuple2<>(word, 1));
            }
          }
        })
        .groupBy(0)
        .sum(1);

wordCounts.print();

从文件读取

final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(1);

    //The path of the file, as a URI
    //(e.g., "file:///some/local/file" or "hdfs://host:port/file/path").
    DataSet<String> text = env.readTextFile("/path/to/file");

    DataSet<Tuple2<String, Integer>> wordCounts = text
        .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
          @Override
          public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
            for (String word : line.split(" ")) {
              out.collect(new Tuple2<String, Integer>(word, 1));
            }
          }
        })
        .groupBy(0)
        .sum(1);

    wordCounts.print();

不要使用try catch处理在wordCounts.print()上引发的异常,而应将throw添加到方法签名。

将以下依赖项添加到pom.xml。

<dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-java</artifactId>
      <version>1.8.0</version>
</dependency> 

在这里了解flatMap,groupBy,sum和其他flink操作:https://ci.apache.org/projects/flink/flink-docs-stable/dev/stream/operators/

Flink流媒体文档和示例:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/datastream_api.html

答案 2 :(得分:0)

使用标准资源的最少步骤

我不确定这是否将是最终的答案,但是我发现flink通常随示例一起提供,这些示例可以轻松地进行一些轻松的交互。

以下是一个可能的hello world示例,其中包含基于默认字数统计的flink 1.9.1随附的标准资源:

  1. 确保已启动flink集群,并且在flink目录中打开了三个终端。

  2. 在终端1中打开与右侧端口的连接

nc -l 9000

  1. 在下一行的同一终端中,键入一些文本,然后按Enter键

Hello World

  1. 在终端2中启动标准字数统计

./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000

  1. 在3号航站楼检查计数结果

tail -f log/flink-*-taskexecutor-*.out

您现在应该看到:

Hello : 1
World : 1

就是这样,从这里您可以在1号航站楼中输入更多信息,再次查看日志时,您将看到更新的字数统计。

如果您以前已经做过一次并且想重新开始,则可以使用rm log/flink-*-taskexecutor-*.out清除日志(假设是沙盒环境)