forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-1173] [streaming] Add socket text stream as a data source for …
…the streaming API
- Loading branch information
1 parent
42fe874
commit 80416ac
Showing
5 changed files
with
243 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...rc/main/java/org/apache/flink/streaming/api/function/source/SocketTextStreamFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.streaming.api.function.source; | ||
|
||
import org.apache.flink.util.Collector; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.Socket; | ||
|
||
public class SocketTextStreamFunction implements SourceFunction<String> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private String hostname; | ||
private int port; | ||
private char delimiter; | ||
|
||
public SocketTextStreamFunction(String hostname, int port, char delimiter) { | ||
this.hostname = hostname; | ||
this.port = port; | ||
this.delimiter = delimiter; | ||
} | ||
|
||
@Override | ||
public void invoke(Collector<String> collector) throws Exception { | ||
Socket socket = new Socket(hostname, port); | ||
while (!socket.isClosed() && socket.isConnected()) { | ||
streamFromSocket(collector, socket); | ||
} | ||
} | ||
|
||
public void streamFromSocket(Collector<String> collector, Socket socket) throws Exception { | ||
StringBuffer buffer = new StringBuffer(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
|
||
while (true) { | ||
int data = reader.read(); | ||
if (!socket.isConnected() || socket.isClosed() || data == -1) { | ||
break; | ||
} | ||
|
||
if (data == delimiter) { | ||
collector.collect(buffer.toString()); | ||
buffer = new StringBuffer(); | ||
} else if (data != '\r') { // ignore carriage return | ||
buffer.append((char) data); | ||
} | ||
} | ||
|
||
if (buffer.length() > 0) { | ||
collector.collect(buffer.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...rc/main/java/org/apache/flink/streaming/examples/wordcount/SocketTextStreamWordCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.streaming.examples.wordcount; | ||
|
||
import org.apache.flink.api.java.tuple.Tuple2; | ||
import org.apache.flink.streaming.api.datastream.DataStream; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
|
||
/** | ||
* This example shows an implementation of WordCount with data from socket. | ||
* | ||
* <p> | ||
* Usage: <code>SocketTextStreamWordCount <hostname> <port > <result path></code><br> | ||
* | ||
* <p> | ||
* This example shows how to: | ||
* <ul> | ||
* <li>use StreamExecutionEnvironment.socketTextStream | ||
* <li>write a simple Flink program, | ||
* <li>write and use user-defined functions. | ||
* </ul> | ||
*/ | ||
public class SocketTextStreamWordCount { | ||
public static void main(String[] args) throws Exception { | ||
|
||
if (!parseParameters(args)) { | ||
return; | ||
} | ||
|
||
// set up the execution environment | ||
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); | ||
|
||
// get input data | ||
DataStream<String> text = env.socketTextStream(hostname, port); | ||
|
||
DataStream<Tuple2<String, Integer>> counts = | ||
// split up the lines in pairs (2-tuples) containing: (word,1) | ||
text.flatMap(new WordCount.Tokenizer()) | ||
// group by the tuple field "0" and sum up tuple field "1" | ||
.groupBy(0).sum(1); | ||
|
||
if (fileOutput) { | ||
counts.writeAsText(outputPath, 1); | ||
} else { | ||
counts.print(); | ||
} | ||
|
||
// execute program | ||
env.execute("WordCount with SocketTextStream Example"); | ||
} | ||
|
||
// ************************************************************************* | ||
// UTIL METHODS | ||
// ************************************************************************* | ||
|
||
private static boolean fileOutput = false; | ||
private static String hostname; | ||
private static int port; | ||
private static String outputPath; | ||
|
||
private static boolean parseParameters(String[] args) { | ||
|
||
if (args.length > 0) { | ||
// parse input arguments | ||
if (args.length == 3) { | ||
fileOutput = true; | ||
hostname = args[0]; | ||
port = Integer.valueOf(args[1]); | ||
outputPath = args[2]; | ||
} else if (args.length == 2) { | ||
hostname = args[0]; | ||
port = Integer.valueOf(args[1]); | ||
} else { | ||
System.err.println("Usage: SocketTextStreamWordCount <hostname> <port> <output path>"); | ||
return false; | ||
} | ||
} else { | ||
System.out.println("Executing WordCount example with data from socket."); | ||
System.out.println(" Provide parameters to connect data source."); | ||
System.out.println(" Usage: SocketTextStreamWordCount <hostname> <port> <output path>"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |