package com.cloudera.sa.sparkonalog.spark.streaming.job.basic;

import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;

public class SparkStreamingFromNetworkExample {
	public static void main(String[] args) {
	    if (args.length < 3) {
	      System.err.println("Usage: NetworkWordCount <master> <hostname> <port>\n" +
	          "In local mode, <master> should be 'local[n]' with n > 1");
	      System.exit(1);
	    }

	    // Create the context with a 1 second batch size
	    JavaStreamingContext ssc = new JavaStreamingContext(args[0], "NetworkWordCount",
	            new Duration(5000), System.getenv("SPARK_HOME"), System.getenv("SPARK_EXAMPLES_JAR"));

	    // Create a NetworkInputDStream on target ip:port and count the
	    // words in input stream of \n delimited test (eg. generated by 'nc')
	    JavaDStream<String> lines = ssc.socketTextStream(args[1], Integer.parseInt(args[2]));
	    
	    lines.map(new Function<String, String> () {

			@Override
			public String call(String arg0) throws Exception {
				System.out.println("arg0" + arg0);
				return arg0;
			}}).print();
	    
	    lines.print();
	    ssc.start();


	  }

}