繁体   English   中英

Spark Kafka Direct DStream - 如果设置了num-executors,则在yarn-cluster模式下有多少个执行程序和RDD分区?

[英]Spark Kafka Direct DStream - How many executors and RDD partitions in yarn-cluster mode if num-executors is set?


我正在尝试使用Spark Kafka Direct Stream方法。 它通过创建与kafka主题分区一样多的RDD分区来简化并行性,如本文档中所述 根据我的理解,spark将为每个RDD分区创建一个执行程序来进行计算。

因此,当我以纱线群集模式提交应用程序,并将选项num-executors指定为与分区数量不同的值时,将会有多少执行程序?

例如,有一个带有2个分区的kafka主题,我将num-executors指定为4:

export YARN_CONF_DIR=$HADOOP_HOME/client_conf

./bin/spark-submit \
--class playground.MainClass \
--master yarn-cluster \
--num-executors 4 \
../spark_applications/uber-spark-streaming-0.0.1-SNAPSHOT.jar \
127.0.0.1:9093,127.0.0.1:9094,127.0.0.1:9095 topic_1

我试一试,发现执行器的数量是4,每个执行器都会从kafka读取和处理数据。 为什么? kafka主题中只有2个分区,4个执行程序如何从kafka主题中读取,该主题只有2个分区?

以下是spark应用程序和日志的详细信息。

我的spark应用程序 ,它从每个执行程序中的kafka打印收到的消息(以flatMap方法):

    ...
    String brokers = args[0];
    HashSet<String> topicsSet = new HashSet<String>(Arrays.asList(args[1].split(",")));
    kafkaParams.put("metadata.broker.list", brokers);

    JavaPairInputDStream<String, String> messages =
        KafkaUtils.createDirectStream(jssc, String.class, String.class, StringDecoder.class, StringDecoder.class,
            kafkaParams, topicsSet);

    JavaPairDStream<String, Integer> wordCounts =
        messages.flatMap(new FlatMapFunction<Tuple2<String, String>, String>()
        {
            public Iterable<String> call(Tuple2<String, String> tuple) throws Exception
            {
                System.out.println(String.format("[received from kafka] tuple_1 is %s, tuple_2 is %s", tuple._1(),
                    tuple._2())); // print the kafka message received  in executor
                return Arrays.asList(SPACE.split(tuple._2()));
            }

        }).mapToPair(new PairFunction<String, String, Integer>()
        {
            public Tuple2<String, Integer> call(String word) throws Exception
            {
                System.out.println(String.format("[word]: %s", word));
                return new Tuple2<String, Integer>(word, 1);
            }

        }).reduceByKey(new Function2<Integer, Integer, Integer>()
        {
            public Integer call(Integer v1, Integer v2) throws Exception
            {
                return v1 + v2;
            }

        });

    wordCounts.print();

    Runtime.getRuntime().addShutdownHook(new Thread(){
        @Override
        public void run(){
            System.out.println("gracefully shutdown Spark!");
            jssc.stop(true, true);
        }
    });
    jssc.start();
    jssc.awaitTermination();

我的Kafka主题 ,有2个分区。 字符串“hello hello word 1”,“hello hello word 2”,“hello hello word 3”, ...被发送到主题。

Topic: topic_2  PartitionCount:2    ReplicationFactor:2 Configs:
Topic: topic_2  Partition: 0    Leader: 3   Replicas: 3,1   Isr: 3,1
Topic: topic_2  Partition: 1    Leader: 1   Replicas: 1,2   Isr: 1,2

Webconsle 在此输入图像描述

执行器1的控制台输出

...
[received from kafka] tuple_1 is null, tuple_2 is hello hello world 12
[word]: hello
[word]: hello
[word]: world
[word]: 12
...

执行器2的控制台输出

...
[received from kafka] tuple_1 is null, tuple_2 is hello hello world 2
[word]: hello
[word]: hello
[word]: world
[word]: 2
...

执行器3的控制台输出

...
[received from kafka] tuple_1 is null, tuple_2 is hello hello world 3
[word]: hello
[word]: hello
[word]: world
[word]: 3
...

每个分区由一个执行器一次操作(假设您没有打开推测执行)。

如果你有比执行分区更多的执行程序,那么并非所有执行程序都可以在任何给定的RDD上工作。 但正如您所指出的,由于DStream是一系列RDD,随着时间的推移,每个执行程序都会做一些工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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