You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by se...@apache.org on 2017/02/13 19:53:50 UTC
[2/2] flink git commit: [FLINK-5729] [examples] Add hostname option
to SocketWindowWordCount examples
[FLINK-5729] [examples] Add hostname option to SocketWindowWordCount examples
This closes #3283
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/30c5b771
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/30c5b771
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/30c5b771
Branch: refs/heads/master
Commit: 30c5b771a7943e981dd5f67131c932fdb204fbc2
Parents: 6bc6b22
Author: WangTaoTheTonic <wa...@huawei.com>
Authored: Tue Feb 7 15:52:26 2017 +0800
Committer: Stephan Ewen <se...@apache.org>
Committed: Mon Feb 13 20:51:50 2017 +0100
----------------------------------------------------------------------
.../examples/socket/SocketWindowWordCount.java | 17 ++++++++-------
.../examples/socket/SocketWindowWordCount.scala | 22 +++++++++++++-------
2 files changed, 24 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/30c5b771/flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/socket/SocketWindowWordCount.java
----------------------------------------------------------------------
diff --git a/flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/socket/SocketWindowWordCount.java b/flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/socket/SocketWindowWordCount.java
index dd2e061..250c5b9 100644
--- a/flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/socket/SocketWindowWordCount.java
+++ b/flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/socket/SocketWindowWordCount.java
@@ -35,23 +35,26 @@ import org.apache.flink.util.Collector;
* <pre>
* nc -l 12345
* </pre>
- * and run this example with the port as an argument.
+ * and run this example with the hostname and the port as arguments.
*/
@SuppressWarnings("serial")
public class SocketWindowWordCount {
public static void main(String[] args) throws Exception {
- // the port to connect to
+ // the host and the port to connect to
+ final String hostname;
final int port;
try {
final ParameterTool params = ParameterTool.fromArgs(args);
+ hostname = params.has("hostname") ? params.get("hostname") : "localhost";
port = params.getInt("port");
} catch (Exception e) {
- System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>', " +
- "where port is the address of the text server");
- System.err.println("To start a simple text server, run 'netcat -l <port>' and type the input text " +
- "into the command line");
+ System.err.println("No port specified. Please run 'SocketWindowWordCount " +
+ "--hostname <hostname> --port <port>', where hostname (localhost by default) " +
+ "and port is the address of the text server");
+ System.err.println("To start a simple text server, run 'netcat -l <port>' and " +
+ "type the input text into the command line");
return;
}
@@ -59,7 +62,7 @@ public class SocketWindowWordCount {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// get input data by connecting to the socket
- DataStream<String> text = env.socketTextStream("localhost", port, "\n");
+ DataStream<String> text = env.socketTextStream(hostname, port, "\n");
// parse the data, group it, window it, and aggregate the counts
DataStream<WordWithCount> windowCounts = text
http://git-wip-us.apache.org/repos/asf/flink/blob/30c5b771/flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/socket/SocketWindowWordCount.scala
----------------------------------------------------------------------
diff --git a/flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/socket/SocketWindowWordCount.scala b/flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/socket/SocketWindowWordCount.scala
index 1761b84..d2afa4d 100644
--- a/flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/socket/SocketWindowWordCount.scala
+++ b/flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/socket/SocketWindowWordCount.scala
@@ -31,20 +31,26 @@ import org.apache.flink.streaming.api.windowing.time.Time
* <pre>
* nc -l 12345
* </pre>
- * and run this example with the port as an argument.
+ * and run this example with the hostname and the port as arguments..
*/
object SocketWindowWordCount {
/** Main program method */
def main(args: Array[String]) : Unit = {
-
- // the port to connect to
- val port: Int = try {
- ParameterTool.fromArgs(args).getInt("port")
+
+ // the host and the port to connect to
+ var hostname: String = "localhost"
+ var port: Int = 0
+
+ try {
+ val params = ParameterTool.fromArgs(args)
+ hostname = if (params.has("hostname")) params.get("hostname") else "localhost"
+ port = params.getInt("port")
} catch {
case e: Exception => {
- System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>', " +
- "where port is the address of the text server")
+ System.err.println("No port specified. Please run 'SocketWindowWordCount " +
+ "--hostname <hostname> --port <port>', where hostname (localhost by default) and port " +
+ "is the address of the text server")
System.err.println("To start a simple text server, run 'netcat -l <port>' " +
"and type the input text into the command line")
return
@@ -55,7 +61,7 @@ object SocketWindowWordCount {
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
// get input data by connecting to the socket
- val text = env.socketTextStream("localhost", port, '\n')
+ val text = env.socketTextStream(hostname, port, '\n')
// parse the data, group it, window it, and aggregate the counts
val windowCounts = text