Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark spark是否足够聪明,可以在执行聚合时避免冗余值?_Apache Spark_Dataset_Aggregation - Fatal编程技术网

Apache spark spark是否足够聪明,可以在执行聚合时避免冗余值?

Apache spark spark是否足够聪明,可以在执行聚合时避免冗余值?,apache-spark,dataset,aggregation,Apache Spark,Dataset,Aggregation,我有以下数据集 case class Department(deptId:String,locations:Seq[String]) // using spark 2.0.2 // I have a Dataset `ds` of type Department +-------+--------------------+ |deptId | locations | +-------+--------------------+ | d1|[delhi,kera

我有以下数据集

case class Department(deptId:String,locations:Seq[String])

// using spark 2.0.2
// I have a Dataset `ds` of type Department   

+-------+--------------------+
|deptId |      locations     |
+-------+--------------------+
|     d1|[delhi,kerala]      |            
|     d1|[]                  |
|    dp2|[]                  |
|    dp2|[hyderabad]         |       
+-------+--------------------+
我打算把它改成

// Dataset `result` of type Department itself

+-------+--------------------+
|deptId |      locations     |
+-------+--------------------+
|     d1|[delhi,kerala]      |            
|    dp2|[hyderabad]         |   
+-------+--------------------+
我做以下几点

val flatten = udf(
  (xs: Seq[Seq[String]]) => xs.flatten)

val result = ds.groupBy("deptId").
                agg(flatten(collect_list("locations")).as("locations")
我的问题是,Spark是否足够聪明,不会在空的
位置
ie
[]

PS:我不确定这是不是一个愚蠢的问题。

是和否:

  • 是-
    collect\u list
    执行地图端聚合,因此如果每个分组键有多个值,则数据将在洗牌之前合并
  • 否-因为空列表与缺少的数据不同。如果这不是期望的行为,那么应该首先过滤数据

    ds.filter(size($"location") > 0).groupBy("deptId").agg(...)
    
    但请记住,如果
    deptId
    只有空数组,则会产生不同的结果

是和否:

  • 是-
    collect\u list
    执行地图端聚合,因此如果每个分组键有多个值,则数据将在洗牌之前合并
  • 否-因为空列表与缺少的数据不同。如果这不是期望的行为,那么应该首先过滤数据

    ds.filter(size($"location") > 0).groupBy("deptId").agg(...)
    
    但请记住,如果
    deptId
    只有空数组,则会产生不同的结果