简体   繁体   English

Spark:如何将 spark arrayType 作为表达式进行迭代

[英]Spark: how to iterate over spark arrayType as an expression

Building a recursive function.构建递归函数。

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
}

I have an error saying that "error: value fields is not a member of org.apache.spark.sql.types.ArrayType".我有一个错误说“错误:值字段不是 org.apache.spark.sql.types.ArrayType 的成员”。 So how do I iterate over each element of the arrayType and return a flattened sequence of strings?那么如何遍历 arrayType 的每个元素并返回一个扁平的字符串序列?

the trick is using .elementType诀窍是使用 .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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM