Spark:使用Scala编程方式创建数据框架模式

11

我有一个相对较小的数据集,将成为Spark作业的结果。我正在考虑将此数据集转换为DataFrame,以便在作业结束时更方便地处理,但是我一直在努力正确定义模式。问题出在下面的最后一个字段 (topValues) 上;它是元组的ArrayBuffer--键和计数。

  val innerSchema =
    StructType(
      Array(
        StructField("value", StringType),
        StructField("count", LongType)
      )
    )
  val outputSchema =
    StructType(
      Array(
        StructField("name", StringType, nullable=false),
        StructField("index", IntegerType, nullable=false),
        StructField("count", LongType, nullable=false),
        StructField("empties", LongType, nullable=false),
        StructField("nulls", LongType, nullable=false),
        StructField("uniqueValues", LongType, nullable=false),
        StructField("mean", DoubleType),
        StructField("min", DoubleType),
        StructField("max", DoubleType),
        StructField("topValues", innerSchema)
      )
    )

  val result = stats.columnStats.map{ c =>
    Row(c._2.name, c._1, c._2.count, c._2.empties, c._2.nulls, c._2.uniqueValues, c._2.mean, c._2.min, c._2.max, c._2.topValues.topN)
  }

  val rdd = sc.parallelize(result.toSeq)

  val outputDf = sqlContext.createDataFrame(rdd, outputSchema)

  outputDf.show()

我得到的错误是MatchError:scala.MatchError: ArrayBuffer((10,2), (20,3), (8,1)) (of class scala.collection.mutable.ArrayBuffer)

当我调试并检查我的对象时,我看到了这个:

rdd: ParallelCollectionRDD[2]
rdd.data: "ArrayBuffer" size = 2
rdd.data(0): [age,2,6,0,0,3,14.666666666666666,8.0,20.0,ArrayBuffer((10,2), (20,3), (8,1))]
rdd.data(1): [gender,3,6,0,0,2,0.0,0.0,0.0,ArrayBuffer((M,4), (F,2))]

我认为我已经准确地描述了innerSchema中元组的ArrayBuffer,但是Spark持不同意见。

你有什么想法,应该如何定义模式?


如果您提供一个示例数据或至少是rdd的确切类型,那将非常有用。 - zero323
3个回答

17
val rdd = sc.parallelize(Array(Row(ArrayBuffer(1,2,3,4))))
val df = sqlContext.createDataFrame(
  rdd,
  StructType(Seq(StructField("arr", ArrayType(IntegerType, false), false)
)

df.printSchema
root
 |-- arr: array (nullable = false)
 |    |-- element: integer (containsNull = false)

df.show
+------------+
|         arr|
+------------+
|[1, 2, 3, 4]|
+------------+

是的,ArrayType 是正确的方法。谢谢!我的最终模式在我的答案中。 - Stuart

7

正如David指出的那样,我需要使用一个ArrayType。Spark对此很满意:

  val outputSchema =
    StructType(
      Array(
        StructField("name", StringType, nullable=false),
        StructField("index", IntegerType, nullable=false),
        StructField("count", LongType, nullable=false),
        StructField("empties", LongType, nullable=false),
        StructField("nulls", LongType, nullable=false),
        StructField("uniqueValues", LongType, nullable=false),
        StructField("mean", DoubleType),
        StructField("min", DoubleType),
        StructField("max", DoubleType),
        StructField("topValues", ArrayType(StructType(Array(
          StructField("value", StringType),
          StructField("count", LongType)
        ))))
      )
    )

3
import spark.implicits._
import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._


val searchPath = "/path/to/.csv"
val columns = "col1,col2,col3,col4,col5,col6,col7"
val fields = columns.split(",").map(fieldName => StructField(fieldName, StringType, 
nullable = true))
val customSchema = StructType(fields)
var dfPivot =spark.read.format("com.databricks.spark.csv").option("header","false").option("inferSchema", "false").schema(customSchema).load(searchPath)

使用自定义模式加载数据的速度,与使用默认模式加载数据相比要快得多。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接