【问题标题】:How to get Running sum of based on two columns using Spark scala RDD如何使用 Spark scala RDD 获得基于两列的运行总和
【发布时间】:2017-05-25 21:00:51
【问题描述】:

我在 RDD 中有数据,其中包含地理、产品、时间和价格等 4 列。我想根据地理和时间计算运行总和。

给定数据

我需要类似的结果。

[

我需要这个 spark-Scala-RDD。我是这个 Scala 世界的新手,我可以在 SQL 中轻松实现这一点。我想在 spark -Scala -RDD 中执行此操作,例如使用 (map,flatmap)。

提前感谢您的帮助。

【问题讨论】:

  • 任何代码都会激励我们。
  • 您必须使用累加器来跟踪总和。
  • @balaji 您可以将 RDD 转换为 DataFrame 并重用现有 SQL :)
  • 是的,这个论坛解决了我的问题,很抱歉问我如何接受。哪里有接受的选项?

标签: scala apache-spark rdd


【解决方案1】:

这可以通过定义一个窗口函数来实现:

>>> val data = List(
    ("India","A1","Q1",40),
    ("India","A2","Q1",30),
    ("India","A3","Q1",21),
    ("German","A1","Q1",50),
    ("German","A3","Q1",60),
    ("US","A1","Q1",60),
    ("US","A2","Q2",25),
    ("US","A4","Q1",20),
    ("US","A5","Q5",15),
    ("US","A3","Q3",10)
)

>>> val df = sc.parallelize(data).toDF("country", "part", "quarter", "result")
>>> df.show()

+-------+----+-------+------+
|country|part|quarter|result|
+-------+----+-------+------+
|  India|  A1|     Q1|    40|
|  India|  A2|     Q1|    30|
|  India|  A3|     Q1|    21|
| German|  A1|     Q1|    50|
| German|  A3|     Q1|    60|
|     US|  A1|     Q1|    60|
|     US|  A2|     Q2|    25|
|     US|  A4|     Q1|    20|
|     US|  A5|     Q5|    15|
|     US|  A3|     Q3|    10|
+-------+----+-------+------+

>>> val window = Window.partitionBy("country").orderBy("part", "quarter")
>>> val resultDF = df.withColumn("agg", sum(df("result")).over(window))
>>> resultDF.show()

+-------+----+-------+------+---+
|country|part|quarter|result|agg|
+-------+----+-------+------+---+
|  India|  A1|     Q1|    40| 40|
|  India|  A2|     Q1|    30| 70|
|  India|  A3|     Q1|    21| 91|
|     US|  A1|     Q1|    60| 60|
|     US|  A2|     Q2|    25| 85|
|     US|  A3|     Q3|    10| 95|
|     US|  A4|     Q1|    20|115|
|     US|  A5|     Q5|    15|130|
| German|  A1|     Q1|    50| 50|
| German|  A3|     Q1|    60|110|
+-------+----+-------+------+---+

您可以使用 Window 函数执行此操作,请查看有关 Windows 的 Databrick 博客: https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html

希望这会有所帮助。

快乐火花!干杯,福克

【讨论】:

    【解决方案2】:

    我认为这也会对其他人有所帮助。我在 SCALA RDD 中尝试过。

        val fileName_test_1 ="C:\\venkat_workshop\\Qintel\\Data_Files\\test_1.txt"
    
    
         val rdd1 = sc.textFile(fileName_test_1).map { x => (x.split(",")(0).toString() , 
                                                              x.split(",")(1).toString(),
                                                              x.split(",")(2).toString(),
                                                              x.split(",")(3).toDouble
                                                              ) 
                                                      }.groupBy( x => (x._1,x._3) )
                                                       .mapValues
                                                                 { 
                                                                   _.toList.sortWith
                                                                   {
                                                                   (a,b) => (a._4) > (b._4)
                                                                   }.scanLeft("","","",0.0,0.0){
                                                                     (a,b) => (b._1,b._2,b._3,b._4,b._4+a._5)
                                                                   }.tail
                                                                 }.flatMapValues(f => f).values
    

    【讨论】:

    • 我尝试使用此代码获取累积总和,但它没有给出预期的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2016-03-13
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多