简体   繁体   中英

Spark Kafka streaming doesn't distribute consumer load on worker nodes

I've created the following application that prints specific messages occurrences within 20sec windows:

public class SparkMain {

public static void main(String[] args) {
    Map<String, Object> kafkaParams = new HashMap<>();

    kafkaParams.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092, localhost:9093");
    kafkaParams.put(GROUP_ID_CONFIG, "spark-consumer-id");
    kafkaParams.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    kafkaParams.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    // events topic has 2 partitions
    Collection<String> topics = Arrays.asList("events");

    // local[*] Run Spark locally with as many worker threads as logical cores on your machine.
    SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("SsvpSparkStreaming");

    // Create context with a 1 seconds batch interval
    JavaStreamingContext streamingContext =
            new JavaStreamingContext(conf, Durations.seconds(1));

    JavaInputDStream<ConsumerRecord<String, String>> stream =
            KafkaUtils.createDirectStream(
                    streamingContext,
                    LocationStrategies.PreferConsistent(),
                    ConsumerStrategies.<String, String>Subscribe(topics, kafkaParams)
            );

    // extract event name from record value
    stream.map(new Function<ConsumerRecord<String, String>, String>() {
        @Override
        public String call(ConsumerRecord<String, String> rec) throws Exception {
            return rec.value().substring(0, 5);
        }})
    // filter events
    .filter(new Function<String, Boolean>() {
        @Override
        public Boolean call(String eventName) throws Exception {
            return eventName.contains("msg");
        }})
    // count with 20sec window and 5 sec slide duration
    .countByValueAndWindow(Durations.seconds(20), Durations.seconds(5))
    .print();

    streamingContext.checkpoint("c:\\projects\\spark\\");
    streamingContext.start();
    try {
        streamingContext.awaitTermination();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

After running the main method inside logs I see only single consumer initialization that gets both partitions:

2018-10-25 18:25:56,007 INFO [org.apache.kafka.common.utils.LogContext$KafkaLogger.info] - <[Consumer clientId=consumer-1, groupId=spark-consumer-id] Setting newly assigned partitions [events-0, events-1]>

Isn't the number of consumers should be equal to the number of spark workers? In accordance with https://spark.apache.org/docs/2.3.2/submitting-applications.html#master-urls

local[*] means - Run Spark locally with as many worker threads as logical cores on your machine.

I have 8 cores CPU, so I expect 8 consumers or at least 2 consumers should be created and each gets the partition of the 'events' topic(2 partitions).

It seems to me that I need to run a whole standalone spark master-worker cluster with 2 nodes where each node starts its own consumer...

You don't necessarily need separate workers or running a cluster manager.

Sounds like you're looking for using 2 Spark executors

How to set amount of Spark executors?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM