Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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
Scala 如何使用Spark KeyValueGroupedDataset的agg方法?_Scala_Apache Spark - Fatal编程技术网

Scala 如何使用Spark KeyValueGroupedDataset的agg方法?

Scala 如何使用Spark KeyValueGroupedDataset的agg方法?,scala,apache-spark,Scala,Apache Spark,我们有如下代码: // think of class A as a table with two columns case class A(property1: String, property2: Long) // class B adds a column to class A case class B(property1: String, property2: Long, property3: String) df.as[A].map[B](a => { val my

我们有如下代码:

// think of class A as a table with two columns
case class A(property1: String, property2: Long)

// class B adds a column to class A
case class B(property1: String, property2: Long, property3: String)

df.as[A].map[B](a => {
      val my_udf = // some code here which creates a user defined function
      new B(a.property1, a.property2, my_udf(a))
    })
其中df是一个数据帧。接下来,我们要创建一个C类型的数据集

// we want to group objects of type B by properties 1 and 3 and compute the average of property2 and also want to store a count
case class C(property1: String, property3: String, average: Long, count: Long)
我们会像这样在sql中创建

select property1, property3, avg(property2), count(*) from B group by property1, property3

我们怎样才能在spark中做到这一点?我们正在尝试使用groupByKey,它提供了一个与一起,但无法让它工作。无法理解如何使用如果您有一个名为
ds\u C
的C类型数据集,那么您可以这样做(使用
groupBy.agg
):


如果您有一个名为
ds_C
的C类型数据集,那么您可以执行(使用
groupBy.agg
):


少数问题1。我没有看到任何计数和平均关键字。他们给了我编译器2无法识别的关键字错误。第二,我们如何命名数据集的列,以便此代码可以工作?
import org.apache.spark.sql.functions.\u
可能是在范围内获取这些函数所必需的。您可以检查所有公共列函数。如果要重命名数据框,可以使用
with columnRename(旧名称,新名称)
。问题1。我没有看到任何计数和平均关键字。他们给了我编译器2无法识别的关键字错误。第二,我们如何命名数据集的列,以便此代码可以工作?
import org.apache.spark.sql.functions.\u
可能是在范围内获取这些函数所必需的。您可以检查所有公共列函数。如果要重命名数据框,可以使用
with columnRename(旧名称、新名称)
ds_c.groupBy("property1", "property3").agg(count($"property2").as("count"), 
                                           avg($"property2").as("mean"))