Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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
Spark是否支持熔化和dcast_R_Scala_Apache Spark_Spark Dataframe_Melt - Fatal编程技术网

Spark是否支持熔化和dcast

Spark是否支持熔化和dcast,r,scala,apache-spark,spark-dataframe,melt,R,Scala,Apache Spark,Spark Dataframe,Melt,我们使用melt和dcast从宽->长和长->宽格式转换数据。 有关更多详细信息,请参阅 scala或SparkR都可以 我经历了这一切。 我没有看到做类似工作的函数 Spark中是否有任何等效功能?如果没有,在Spark中还有其他方法吗?支持使用pivot进行重塑。我知道melt大致与pivot相反,也被称为unpivot。我对Spark比较陌生。据我所知,我试图实施熔化操作 def melt(df: DataFrame, columns: List[String]): DataFra

我们使用melt和dcast从宽->长和长->宽格式转换数据。 有关更多详细信息,请参阅

scala或SparkR都可以

我经历了这一切。 我没有看到做类似工作的函数

Spark中是否有任何等效功能?如果没有,在Spark中还有其他方法吗?

支持使用
pivot
进行重塑。我知道
melt
大致与pivot相反,也被称为
unpivot
。我对
Spark
比较陌生。据我所知,我试图实施熔化操作

    def melt(df: DataFrame, columns: List[String]): DataFrame ={

    val restOfTheColumns =  df.columns.filterNot(columns.contains(_))
    val baseDF = df.select(columns.head, columns.tail: _*)
    val newStructure =StructType(baseDF.schema.fields ++ List(StructField("variable", StringType, true), StructField("value", StringType, true)))
    var newdf  = sqlContext.createDataFrame(sqlContext.sparkContext.emptyRDD[Row], newStructure)

    for(variableCol <- restOfTheColumns){
      val colValues = df.select(variableCol).map(r=> r(0).toString)
      val colRdd=baseDF.rdd.zip(colValues).map(tuple => Row.fromSeq(tuple._1.toSeq.:+(variableCol).:+(tuple._2.toString)))
      var colDF =sqlContext.createDataFrame(colRdd, newStructure)
      newdf =newdf.unionAll(colDF)
    }
    newdf
  }
可用作

melt(df, List("name", "sex"))
结果如下:

+-----+---+--------+----------+
| name|sex|variable|     value|
+-----+---+--------+----------+
|Alice|  f|     age|        34|
|  Bob|  m|     age|        63|
|Alice|  f|     age|       612|
|  Bob|  m|     age|       612|
|Alice|  f|  street| somewhere|
|  Bob|  m|  street|   nowhere|
|Alice|  f|  street|nextstreet|
|  Bob|  m|  street|      moon|
|Alice|  f|  weight|        70|
|  Bob|  m|  weight|       -70|
|Alice|  f|  weight|        23|
|  Bob|  m|  weight|         8|
+-----+---+--------+----------+

我希望这是有用的,并感谢您的意见,如果有改进的余地

这里有一个
spark.ml.Transformer
,它只使用数据集操作(没有RDD内容)

这是一个使用它的测试

"spark" should "melt a dataset" in {
    import spark.implicits._
    val schema = StructType(
      List(StructField("Melt1",StringType),StructField("Melt2",StringType)) ++
      Range(3,10).map{ i => StructField("name_"+i,DoubleType)}.toList)

    val ds = Range(1,11)
      .map{ i => Row("a" :: "b" :: Range(3,10).map{ j => Math.random() }.toList :_ *)}
      .|>{ rows => spark.sparkContext.parallelize(rows) }
      .|>{ rdd => spark.createDataFrame(rdd,schema) }

    val newDF = ds.transform{ df =>
      Melt("Melt1","Melt2").transform(df) }

    assert(newDF.count() === 70)
  }

.|>是scalaZ管道操作符

Spark DataFrame具有
爆炸
方法,提供R
熔化
功能。 Spark 1.6.1中的示例:

// input df has columns (anyDim, n1, n2)
case class MNV(measureName: String, measureValue: Integer);
val dfExploded = df.explode(col("n1"), col("n2")) {
  case Row(n1: Int, n2: Int) =>
  Array(MNV("n1", n1), MNV("n2", n2))
}
// dfExploded has columns (anyDim, n1, n2, measureName, measureValue)

看起来不像。如果可以将数据放入内存中,请使用
as.data.frame()
将Spark DataFrame转换为本机data.frame,对其进行重塑,然后将其写回Spark。你需要自己写。
"spark" should "melt a dataset" in {
    import spark.implicits._
    val schema = StructType(
      List(StructField("Melt1",StringType),StructField("Melt2",StringType)) ++
      Range(3,10).map{ i => StructField("name_"+i,DoubleType)}.toList)

    val ds = Range(1,11)
      .map{ i => Row("a" :: "b" :: Range(3,10).map{ j => Math.random() }.toList :_ *)}
      .|>{ rows => spark.sparkContext.parallelize(rows) }
      .|>{ rdd => spark.createDataFrame(rdd,schema) }

    val newDF = ds.transform{ df =>
      Melt("Melt1","Melt2").transform(df) }

    assert(newDF.count() === 70)
  }
// input df has columns (anyDim, n1, n2)
case class MNV(measureName: String, measureValue: Integer);
val dfExploded = df.explode(col("n1"), col("n2")) {
  case Row(n1: Int, n2: Int) =>
  Array(MNV("n1", n1), MNV("n2", n2))
}
// dfExploded has columns (anyDim, n1, n2, measureName, measureValue)