0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.examples.streaming;
0019
0020 import com.google.common.io.Closeables;
0021
0022 import org.apache.spark.SparkConf;
0023 import org.apache.spark.storage.StorageLevel;
0024 import org.apache.spark.streaming.Duration;
0025 import org.apache.spark.streaming.api.java.JavaDStream;
0026 import org.apache.spark.streaming.api.java.JavaPairDStream;
0027 import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
0028 import org.apache.spark.streaming.api.java.JavaStreamingContext;
0029 import org.apache.spark.streaming.receiver.Receiver;
0030 import scala.Tuple2;
0031
0032 import java.io.BufferedReader;
0033 import java.io.InputStreamReader;
0034 import java.net.ConnectException;
0035 import java.net.Socket;
0036 import java.nio.charset.StandardCharsets;
0037 import java.util.Arrays;
0038 import java.util.regex.Pattern;
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 public class JavaCustomReceiver extends Receiver<String> {
0055 private static final Pattern SPACE = Pattern.compile(" ");
0056
0057 public static void main(String[] args) throws Exception {
0058 if (args.length < 2) {
0059 System.err.println("Usage: JavaCustomReceiver <hostname> <port>");
0060 System.exit(1);
0061 }
0062
0063 StreamingExamples.setStreamingLogLevels();
0064
0065
0066 SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver");
0067 JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, new Duration(1000));
0068
0069
0070
0071 JavaReceiverInputDStream<String> lines = ssc.receiverStream(
0072 new JavaCustomReceiver(args[0], Integer.parseInt(args[1])));
0073 JavaDStream<String> words = lines.flatMap(x -> Arrays.asList(SPACE.split(x)).iterator());
0074 JavaPairDStream<String, Integer> wordCounts = words.mapToPair(s -> new Tuple2<>(s, 1))
0075 .reduceByKey((i1, i2) -> i1 + i2);
0076
0077 wordCounts.print();
0078 ssc.start();
0079 ssc.awaitTermination();
0080 }
0081
0082
0083
0084 String host = null;
0085 int port = -1;
0086
0087 public JavaCustomReceiver(String host_ , int port_) {
0088 super(StorageLevel.MEMORY_AND_DISK_2());
0089 host = host_;
0090 port = port_;
0091 }
0092
0093 @Override
0094 public void onStart() {
0095
0096 new Thread(this::receive).start();
0097 }
0098
0099 @Override
0100 public void onStop() {
0101
0102
0103 }
0104
0105
0106 private void receive() {
0107 try {
0108 Socket socket = null;
0109 BufferedReader reader = null;
0110 try {
0111
0112 socket = new Socket(host, port);
0113 reader = new BufferedReader(
0114 new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
0115
0116 String userInput;
0117 while (!isStopped() && (userInput = reader.readLine()) != null) {
0118 System.out.println("Received data '" + userInput + "'");
0119 store(userInput);
0120 }
0121 } finally {
0122 Closeables.close(reader, true);
0123 Closeables.close(socket, true);
0124 }
0125
0126 restart("Trying to connect again");
0127 } catch(ConnectException ce) {
0128
0129 restart("Could not connect", ce);
0130 } catch(Throwable t) {
0131 restart("Error receiving data", t);
0132 }
0133 }
0134 }