Search in sources :

Example 1 with SparkStreamingPulsarReceiver

use of org.apache.pulsar.spark.SparkStreamingPulsarReceiver in project incubator-pulsar by apache.

the class SparkStreamingPulsarReceiverExample method main.

public static void main(String[] args) throws InterruptedException {
    SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("pulsar-spark");
    JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(5));
    ClientConfiguration clientConf = new ClientConfiguration();
    ConsumerConfiguration consConf = new ConsumerConfiguration();
    String url = "pulsar://localhost:6650/";
    String topic = "persistent://sample/standalone/ns1/topic1";
    String subs = "sub1";
    JavaReceiverInputDStream<byte[]> msgs = jssc.receiverStream(new SparkStreamingPulsarReceiver(clientConf, consConf, url, topic, subs));
    JavaDStream<Integer> isContainingPulsar = msgs.flatMap(new FlatMapFunction<byte[], Integer>() {

        @Override
        public Iterator<Integer> call(byte[] msg) {
            return Arrays.asList(((new String(msg)).indexOf("Pulsar") != -1) ? 1 : 0).iterator();
        }
    });
    JavaDStream<Integer> numOfPulsar = isContainingPulsar.reduce(new Function2<Integer, Integer, Integer>() {

        @Override
        public Integer call(Integer i1, Integer i2) {
            return i1 + i2;
        }
    });
    numOfPulsar.print();
    jssc.start();
    jssc.awaitTermination();
}
Also used : JavaStreamingContext(org.apache.spark.streaming.api.java.JavaStreamingContext) SparkStreamingPulsarReceiver(org.apache.pulsar.spark.SparkStreamingPulsarReceiver) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) Iterator(java.util.Iterator) SparkConf(org.apache.spark.SparkConf) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration)

Aggregations

Iterator (java.util.Iterator)1 ClientConfiguration (org.apache.pulsar.client.api.ClientConfiguration)1 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)1 SparkStreamingPulsarReceiver (org.apache.pulsar.spark.SparkStreamingPulsarReceiver)1 SparkConf (org.apache.spark.SparkConf)1 JavaStreamingContext (org.apache.spark.streaming.api.java.JavaStreamingContext)1