Created
August 8, 2016 10:53
-
-
Save anirudh83/09f0f535b166eba015be257fbbb43ee2 to your computer and use it in GitHub Desktop.
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
package com.fidelity.service; | |
import java.util.Arrays; | |
import java.util.Map; | |
import java.util.HashMap; | |
import java.util.Scanner; | |
import java.util.regex.Pattern; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.stereotype.Component; | |
import scala.Tuple2; | |
import org.apache.spark.SparkConf; | |
import org.apache.spark.api.java.function.FlatMapFunction; | |
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.JavaPairReceiverInputDStream; | |
import org.apache.spark.streaming.api.java.JavaStreamingContext; | |
import org.apache.spark.streaming.kafka.KafkaUtils; | |
/** | |
* Consumes messages from one or more topics in Kafka and does wordcount. | |
* | |
* Usage: JavaKafkaWordCount <zkQuorum> <group> <topics> <numThreads> | |
* <zkQuorum> is a list of one or more zookeeper servers that make quorum | |
* <group> is the name of kafka consumer group | |
* <topics> is a list of one or more kafka topics to consume from | |
* <numThreads> is the number of threads the kafka consumer should use | |
* | |
* To run this example: | |
* `$ bin/run-example org.apache.spark.examples.streaming.JavaKafkaWordCount zoo01,zoo02, \ | |
* zoo03 my-consumer-group topic1,topic2 1` | |
*/ | |
public final class JavaKafkaWordCount{ | |
private static final Pattern SPACE = Pattern.compile(" "); | |
private JavaKafkaWordCount() { | |
} | |
public static void main(String... strings) throws Exception { | |
System.out.println("********* ME CHALA ******"); | |
SparkConf sparkConf = new SparkConf().setAppName("JavaKafkaWordCount").setMaster("local[2]").set("spark.executor.memory","1g"); | |
// Create the context with 2 seconds batch size | |
JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(2000)); | |
int numThreads = 2; | |
Map<String, Integer> topicMap = new HashMap<>(); | |
topicMap.put("test",2); | |
JavaPairReceiverInputDStream<String, String> messages = | |
KafkaUtils.createStream(jssc, "localhost", "test-group", topicMap); | |
JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() { | |
@Override | |
public String call(Tuple2<String, String> tuple2) { | |
return tuple2._2(); | |
} | |
}); | |
JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() { | |
@Override | |
public Iterable<String> call(String s) throws Exception { | |
return (Iterable<String>) Arrays.asList(SPACE.split(s)).iterator(); | |
} | |
}); | |
JavaPairDStream<String, Integer> wordCounts = words.mapToPair( | |
new PairFunction<String, String, Integer>() { | |
@Override | |
public Tuple2<String, Integer> call(String s) { | |
return new Tuple2<>(s, 1); | |
} | |
}).reduceByKey(new Function2<Integer, Integer, Integer>() { | |
@Override | |
public Integer call(Integer i1, Integer i2) { | |
return i1 + i2; | |
} | |
}); | |
wordCounts.print(); | |
jssc.start(); | |
jssc.awaitTermination(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment