Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
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  * Custom Receiver that receives data over a socket. Received bytes is interpreted as
0042  * text and \n delimited lines are considered as records. They are then counted and printed.
0043  *
0044  * Usage: JavaCustomReceiver <master> <hostname> <port>
0045  *   <master> is the Spark master URL. In local mode, <master> should be 'local[n]' with n > 1.
0046  *   <hostname> and <port> of the TCP server that Spark Streaming would connect to receive data.
0047  *
0048  * To run this on your local machine, you need to first run a Netcat server
0049  *    `$ nc -lk 9999`
0050  * and then run the example
0051  *    `$ bin/run-example org.apache.spark.examples.streaming.JavaCustomReceiver localhost 9999`
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     // Create the context with a 1 second batch size
0066     SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver");
0067     JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, new Duration(1000));
0068 
0069     // Create an input stream with the custom receiver on target ip:port and count the
0070     // words in input stream of \n delimited text (eg. generated by 'nc')
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   // ============= Receiver code that receives data over a socket ==============
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     // Start the thread that receives data over a connection
0096     new Thread(this::receive).start();
0097   }
0098 
0099   @Override
0100   public void onStop() {
0101     // There is nothing much to do as the thread calling receive()
0102     // is designed to stop by itself isStopped() returns false
0103   }
0104 
0105   /** Create a socket connection and receive data until receiver is stopped */
0106   private void receive() {
0107     try {
0108       Socket socket = null;
0109       BufferedReader reader = null;
0110       try {
0111         // connect to the server
0112         socket = new Socket(host, port);
0113         reader = new BufferedReader(
0114             new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
0115         // Until stopped or connection broken continue reading
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, /* swallowIOException = */ true);
0123         Closeables.close(socket,  /* swallowIOException = */ true);
0124       }
0125       // Restart in an attempt to connect again when server is active again
0126       restart("Trying to connect again");
0127     } catch(ConnectException ce) {
0128       // restart if could not connect to server
0129       restart("Could not connect", ce);
0130     } catch(Throwable t) {
0131       restart("Error receiving data", t);
0132     }
0133   }
0134 }