上のFlinkのプログラムの例を取得し、2,3の簡単なステップで実行する。
FlinkはLinux, Mac OS X, and Windowsで動作します。Flinkを実行できるようにするための必要条件はJava 7.x (あるいは以上)のインストレーションのみです。Windows ユーザは、ローカルセットアップのためのWindows上でFlinkを実行する方法を説明しているWindows上のFlink ガイドを見てください。
以下のコマンドを実行することで、Javaの正しいインストレーションを調べることができます:
java -version
Java 8 があれば、出力は以下のように見えるでしょう:
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)
repositoriesのうちの1つからソースコードをclonseします。例えば:
$ git clone https://github.com/apache/flink.git
$ cd flink
$ mvn clean package -DskipTests # this will take up to 10 minutes
$ cd build-target # this is where Flink is installed to
$ ./bin/start-local.sh # Start Flink
http://localhost:8081 でジョブマネージャーのwebフロントエンドをチェックし、全てが立ち上がっていて実行中であることを確認します。webフロントエンドは1つの利用可能なタスクマネージャーインスタンスを報告するはずです。
システムが実行中かどうかをlogs
ディレクトリ内のログファイルをチェックすることで検証することもできます:
$ 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)
GitHub上でscala と java のこのSocketWindowWordCountの例の完全なソースコードを見つけることができます。
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)
}
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秒に1回 以前の5秒間の異なる単語の出現数を出力するでしょう。つまり、単語が流れ込む限り処理時間のタンブリング ウィンドウ。
まず最初に、以下のようにローカルサーバを開始するためにnetcatを使います
$ 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. Waiting for job completion.
Connected to JobManager at Actor[akka.tcp://flink@127.0.0.1:6123/user/jobmanager#297388688]
11/04/2016 14:04:50 Job execution switched to status RUNNING.
11/04/2016 14:04:50 Source: Socket Stream -> Flat Map(1/1) switched to SCHEDULED
11/04/2016 14:04:50 Source: Socket Stream -> Flat Map(1/1) switched to DEPLOYING
11/04/2016 14:04:50 Fast TumblingProcessingTimeWindows(5000) of WindowedStream.main(SocketWindowWordCount.java:79) -> Sink: Unnamed(1/1) switched to SCHEDULED
11/04/2016 14:04:51 Fast TumblingProcessingTimeWindows(5000) of WindowedStream.main(SocketWindowWordCount.java:79) -> Sink: Unnamed(1/1) switched to DEPLOYING
11/04/2016 14:04:51 Fast TumblingProcessingTimeWindows(5000) of WindowedStream.main(SocketWindowWordCount.java:79) -> Sink: Unnamed(1/1) switched to RUNNING
11/04/2016 14:04:51 Source: Socket Stream -> Flat Map(1/1) switched to RUNNING
プログラムはソケットに接続し入力を待ちます。期待した通りにジョブを実行しているかどうかを検証するためにwebインタフェースをチェックすることができます:
単語は5秒の時間ウィンドウ内でカウントされ (処理時間、タンブリング ウィンドウ)、stdout
に出力されます。タスクマネージャーの出力ファイルを監視し、nc
になんらかのテキストを書き込みます (当たった後で行ごとにFlinkに入力が送信されます
$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
.out
ファイルは単語が入ってくる限り、各時間ウィンドウの最後にカウントを出力するでしょう。例えば:
$ tail -f log/flink-*-taskmanager-*.out
lorem : 1
bye : 1
ipsum : 4
完了時にFlinkを停止するには、以下をタイプします:
$ ./bin/stop-local.sh
よりFlinkのプログラミング APIの感触をつかむには更なる例を調べてください。それを完了して、ストリーミング ガイドに進んでください。