从RDD

时间:2016-06-01 08:04:58

标签: apache-spark apache-kafka

我正在尝试阅读kafka主题的火花,如下所示:

Map<TopicAndPartition, Long> map = new HashMap<>();
        map.put(new TopicAndPartition("A", 0), 1L);
        map.put(new TopicAndPartition("B", 0), 1L);

        JavaInputDStream<Map.Entry> topicMessages = KafkaUtils.createDirectStream(
                                                                            jssc,
                                                                            String.class,
                                                                            String.class,
                                                                            StringDecoder.class,
                                                                            StringDecoder.class,
                                                                            Map.Entry.class,
                                                                            kafkaParams,
                                                                            map,
                                                                            messageAndMetadata -> 
                                                                                new AbstractMap.SimpleEntry<>(messageAndMetadata.topic(),
                                                                                                              messageAndMetadata.message())
                                                                          );

现在 topicMessage 包含键和值格式的所有值,如下所示:

A="04/15/2015","18:44:28"
A="04/15/2015","18:44:28"
A="04/15/2015","18:44:28"
B="04/15/2016","18:44:28"
B="04/15/2014","18:44:28"  

如何提取特定主题的值 名为B

的主题,如下所示
"04/15/2016","18:44:28"
"04/15/2014","18:44:28" 

1 个答案:

答案 0 :(得分:2)

如果你想要给定主题的行,你只需要这样做:

JavaPairDStream<String> rowsFromTopicB = topicMessages.filter( entry -> entry.getKey().toString().equals("B")).map(entry -> entry.getValue().toString())