|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.github.dunwu.bigdata.flink.streaming.socket; |
| 20 | + |
| 21 | +import org.apache.flink.api.common.functions.FlatMapFunction; |
| 22 | +import org.apache.flink.api.common.functions.ReduceFunction; |
| 23 | +import org.apache.flink.api.java.utils.ParameterTool; |
| 24 | +import org.apache.flink.streaming.api.datastream.DataStream; |
| 25 | +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
| 26 | +import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows; |
| 27 | +import org.apache.flink.streaming.api.windowing.time.Time; |
| 28 | +import org.apache.flink.util.Collector; |
| 29 | + |
| 30 | +/** |
| 31 | + * Implements a streaming windowed version of the "WordCount" program. |
| 32 | + * |
| 33 | + * <p>This program connects to a server socket and reads strings from the socket. The easiest way to |
| 34 | + * try this out is to open a text server (at port 12345) using the <i>netcat</i> tool via |
| 35 | + * |
| 36 | + * <pre> |
| 37 | + * nc -l 12345 on Linux or nc -l -p 12345 on Windows |
| 38 | + * </pre> |
| 39 | + * |
| 40 | + * <p>and run this example with the hostname and the port as arguments. |
| 41 | + */ |
| 42 | +@SuppressWarnings("serial") |
| 43 | +public class SocketWindowWordCount { |
| 44 | + |
| 45 | + public static void main(String[] args) throws Exception { |
| 46 | + |
| 47 | + // the host and the port to connect to |
| 48 | + final String hostname; |
| 49 | + final int port; |
| 50 | + try { |
| 51 | + final ParameterTool params = ParameterTool.fromArgs(args); |
| 52 | + hostname = params.has("hostname") ? params.get("hostname") : "localhost"; |
| 53 | + port = params.has("port") ? params.getInt("port") : 9000; |
| 54 | + } catch (Exception e) { |
| 55 | + System.err.println( |
| 56 | + "No port specified. Please run 'SocketWindowWordCount " |
| 57 | + + "--hostname <hostname> --port <port>', where hostname (localhost by default) " |
| 58 | + + "and port is the address of the text server"); |
| 59 | + System.err.println( |
| 60 | + "To start a simple text server, run 'netcat -l <port>' and " |
| 61 | + + "type the input text into the command line"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // get the execution environment |
| 66 | + final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); |
| 67 | + |
| 68 | + // get input data by connecting to the socket |
| 69 | + DataStream<String> text = env.socketTextStream(hostname, port, "\n"); |
| 70 | + |
| 71 | + // parse the data, group it, window it, and aggregate the counts |
| 72 | + DataStream<WordWithCount> windowCounts = |
| 73 | + text.flatMap( |
| 74 | + new FlatMapFunction<String, WordWithCount>() { |
| 75 | + @Override |
| 76 | + public void flatMap( |
| 77 | + String value, Collector<WordWithCount> out) { |
| 78 | + for (String word : value.split("\\s")) { |
| 79 | + out.collect(new WordWithCount(word, 1L)); |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + .keyBy(value -> value.word) |
| 84 | + .window(TumblingProcessingTimeWindows.of(Time.seconds(5))) |
| 85 | + .reduce( |
| 86 | + new ReduceFunction<WordWithCount>() { |
| 87 | + @Override |
| 88 | + public WordWithCount reduce(WordWithCount a, WordWithCount b) { |
| 89 | + return new WordWithCount(a.word, a.count + b.count); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + // print the results with a single thread, rather than in parallel |
| 94 | + windowCounts.print().setParallelism(1); |
| 95 | + |
| 96 | + env.execute("Socket Window WordCount"); |
| 97 | + } |
| 98 | + |
| 99 | + // ------------------------------------------------------------------------ |
| 100 | + |
| 101 | + /** Data type for words with count. */ |
| 102 | + public static class WordWithCount { |
| 103 | + |
| 104 | + public String word; |
| 105 | + public long count; |
| 106 | + |
| 107 | + public WordWithCount() {} |
| 108 | + |
| 109 | + public WordWithCount(String word, long count) { |
| 110 | + this.word = word; |
| 111 | + this.count = count; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String toString() { |
| 116 | + return word + " : " + count; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments