ダウンロードして起動FLINK
ファイル名を指定して実行FLINKは、唯一の要件は、プリインストールさjava8ということです。
あなたは地元のコマンドを表示することができます。
java -version
java8がインストールされている場合は、コマンドの出力は次のようになります。
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)
MACシステムの場合は、自作してインストールすることができます
$ brew install apache-flink
...
$ flink --version
Version: 1.2.0, Commit ID: 1c659cf
ローカルクラスタFLINKを開始
$ ./bin/start-cluster.sh # Start Flink
あなたは、HTTPを確認することができます:// localhostを:8081、通常のページにアクセスすることができますがある場合。
また、データ・ログ・ファイルのパスを確認することができます。
$ 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 ... at ResourceManager
コードを読みます
あなたは、コンパイルしたコードSocketWindowWordCount、githubのアドレスを実行することができますSocketWindowWordCount
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秒以内に倍の各単語の別の番号を印刷します。
まず、ローカルサーバーを起動するnetcatを使用します
$ nc -l 9000
FLINKプログラムを送信
$ ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000
Starting execution of program
この手順では、受け入れるソケットを接続することによって入力を待ちます。
$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
そして、それは出力で見ることができるログを印刷
$ tail -f log/flink-*-taskexecutor-*.out
lorem : 1
bye : 1
ipsum : 4
最後の停止FLINK clusterコマンド:
$ ./bin/stop-cluster.sh