[英]How to find the sum of all values in RDDs per DStream?
我正在使用Spark Streaming不斷從kafka讀取數據並執行一些統計。 我每秒流。
所以我有第二批(dstreams) 。 此dstream中的每個RDD都包含一個JSON。
這就是我的dstream的方式:
kafkaStream = KafkaUtils.createDirectStream(stream, ['livedata'], {"metadata.broker.list": 'localhost:9092'})
raw = kafkaStream.map(lambda kafkaS: kafkaS[1])
clean = raw.map(lambda xs:json.loads(xs))
我干凈的 dstream中的RDD之一如下所示:
{u'epochseconds': 1458841451, u'protocol': 6, u'source_ip': u'192.168.1.124', \
u'destination_ip': u'149.154.167.120', u'datetime': u'2016-03-24 17:44:11', \
u'length': 1589, u'partitionkey': u'partitionkey', u'packetcount': 10,\
u'source_port': 43375, u'destination_port': 443}
我在每個DStream中都有30-150個這樣的RDD。
現在,我想做的是獲取每個DStream中“長度”的總和或說“ packetcounts”。 那是,
rdd1.length + rdd2.length + ... + LastRDDInTheOneSecondBatch.length
我試過的
add=clean.map(lambda xs: (xs['length'],1)).reduceByKey(lambda a, b: a+b)
我得到了:
頻率而不是總和。
(17, 6)
(6, 24)
如何獲得總和而不是按鍵的頻率?
這是因為您將'length'的值用作鍵,請嘗試以下操作:
add=clean.map(lambda xs: ('Lenght',xs['length'])).reduceByKey(lambda a, b: a+b)
您必須為所有對(鍵,值)設置相同的鍵。 該值可以是字段長度或要匯總的其他字段...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.