Created
August 12, 2015 13:05
-
-
Save zsxwing/b554f6d936c1075d2b56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import org.apache.spark.SparkConf; | |
import org.apache.spark.api.java.function.FlatMapFunction; | |
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; | |
import org.apache.spark.streaming.scheduler.*; | |
import scala.Tuple2; | |
import java.util.Arrays; | |
public class StreamingApp1 { | |
public static void main(String[] args) { | |
SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount"); | |
Duration batchInterval = new Duration(2000); | |
final JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, batchInterval); | |
JavaDStream<String> lines = ssc.textFileStream("..."); | |
JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() { | |
@Override | |
public Iterable<String> call(String s) { | |
return Arrays.asList(s.split(" ")); | |
} | |
}); | |
JavaPairDStream<String, Integer> ones = words.mapToPair(new PairFunction<String, String, Integer>() { | |
@Override | |
public Tuple2<String, Integer> call(String s) { | |
return new Tuple2<String, Integer>(s, 1); | |
} | |
}); | |
JavaPairDStream<String, Integer> counts = ones.reduceByKey(new Function2<Integer, Integer, Integer>() { | |
@Override | |
public Integer call(Integer i1, Integer i2) { | |
return i1 + i2; | |
} | |
}); | |
counts.print(); | |
ssc.addStreamingListener(new StreamingListener() { | |
private int completedBatchCount = 0; | |
@Override | |
public void onReceiverStarted(StreamingListenerReceiverStarted receiverStarted) { | |
} | |
@Override | |
public void onReceiverError(StreamingListenerReceiverError receiverError) { | |
} | |
@Override | |
public void onReceiverStopped(StreamingListenerReceiverStopped receiverStopped) { | |
} | |
@Override | |
public void onBatchSubmitted(StreamingListenerBatchSubmitted batchSubmitted) { | |
} | |
@Override | |
public void onBatchStarted(StreamingListenerBatchStarted batchStarted) { | |
} | |
@Override | |
public void onBatchCompleted(StreamingListenerBatchCompleted batchCompleted) { | |
completedBatchCount += 1; | |
if (completedBatchCount == 10) { | |
// Note: because this runs asynchronously, it's possible that we launch more than 10 batches | |
System.out.println(completedBatchCount + " batches has completed"); | |
new Thread() { | |
@Override | |
public void run() { | |
ssc.stop(true, true); | |
} | |
}.start(); | |
} | |
} | |
}); | |
ssc.start(); | |
ssc.awaitTermination(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment