Scala Spark:如何将Spark arrayType作为表达式进行迭代
构建递归函数Scala Spark:如何将Spark arrayType作为表达式进行迭代,scala,apache-spark,Scala,Apache Spark,构建递归函数 def loop(path: String, dt: DataType, acc:Seq[String]): Seq[String] = { dt match { case s: ArrayType => s.fields.flatMap(f => loop(path + "." + f.name, f.dataType, acc)) case s: StructType => s.fields.flatMap(f
def loop(path: String, dt: DataType, acc:Seq[String]): Seq[String] = {
dt match {
case s: ArrayType =>
s.fields.flatMap(f => loop(path + "." + f.name, f.dataType, acc))
case s: StructType =>
s.fields.flatMap(f => loop(path + "." + f.name, f.dataType, acc))
case other =>
acc:+ path
}
我有一个错误,说“error:value字段不是org.apache.spark.sql.types.ArrayType的成员”。那么,如何迭代arrayType的每个元素并返回一个平展的字符串序列呢 诀窍是使用.elementType
def loop(path: String, dt: DataType, acc:Seq[String]): Seq[String] = {
dt match {
case s: ArrayType =>
loop(path, s.elementType, acc)
case s: StructType =>
s.fields.flatMap(f => loop(path + "." + f.name, f.dataType, acc))
case other =>
acc:+ path
}
诀窍是使用.elementType
def loop(path: String, dt: DataType, acc:Seq[String]): Seq[String] = {
dt match {
case s: ArrayType =>
loop(path, s.elementType, acc)
case s: StructType =>
s.fields.flatMap(f => loop(path + "." + f.name, f.dataType, acc))
case other =>
acc:+ path
}
你没有。架构中不会反映单个值。但它会反映。我的架构似乎包含structType元素。我希望循环能够解析它们,并返回一个平展的字符串序列,但事实并非如此。
ArrayType
模式中反映的唯一内容是元素的类型。是否要访问阵列的类型?如果是这样,请使用f.dataType
。您不需要。架构中不会反映单个值。但它会反映。我的架构似乎包含structType元素。我希望循环能够解析它们,并返回一个平展的字符串序列,但事实并非如此。ArrayType
模式中反映的唯一内容是元素的类型。是否要访问阵列的类型?如果是,请使用f.dataType
。