Spark Python - how to use reduce by key to get minmum/maximum values

筅森魡賤 提交于 2019-12-08 01:38:50

问题


I have a sample data of maximum and minimum temperatures of some cities in csv format.

Mumbai,19,30
Delhi,5,41
Kolkata,20,40
Mumbai,18,35
Delhi,4,42
Delhi,10,44
Kolkata,19,39

I want to find out all time lowest temperature recorded for each city using a spark script in Python.

Here is my script

cityTemp = sc.textFile("weather.txt").map(lambda x: x.split(','))

# convert it to pair RDD for performing reduce by Key

cityTemp = cityTemp.map(lambda x: (x[0], tuple(x[1:])))

cityTempMin = cityTemp.reduceByKey(lambda x, y: min(x[0],y[0]))

cityTempMin.collect()

My expected output is as follows

Delhi, 4
Mumbai, 18
Kolkata, 19

However the script is producing the following output.

[(u'Kolkata', u'19'), (u'Mumbai', u'18'), (u'Delhi', u'1')]

How do I get the desired output?


回答1:


Try the below solution, if you have to use reduceByKey function :

  val df = sc.parallelize(Seq(("Mumbai", 19, 30),
    ("Delhi", 5, 41),
    ("Kolkata", 20, 40),
    ("Mumbai", 18, 35),
    ("Delhi", 4, 42),
    ("Delhi", 10, 44),
    ("Kolkata", 19, 39))).map(x => (x._1,x._2)).keyBy(_._1)


    df.reduceByKey((accum, n) => if (accum._2 > n._2) n else  accum).map(_._2).collect().foreach(println)

Output:

(Kolkata,19)
(Delhi,4)
(Mumbai,18)

If you don't want to do a reduceByKey. Just a group by followed by min function would give you desired result.

val df = sc.parallelize(Seq(("Mumbai", 19, 30),
        ("Delhi", 5, 41),
        ("Kolkata", 20, 40),
        ("Mumbai", 18, 35),
        ("Delhi", 4, 42),
        ("Delhi", 10, 44),
        ("Kolkata", 19, 39))).toDF("city", "minTemp", "maxTemp")

        df.groupBy("city").agg(min("minTemp")).show

Output :

+-------+------------+
|   city|min(minTemp)|
+-------+------------+
| Mumbai|          18|
|Kolkata|          19|
|  Delhi|           4|
+-------+------------+


来源:https://stackoverflow.com/questions/44176782/spark-python-how-to-use-reduce-by-key-to-get-minmum-maximum-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!