繁体   English   中英

在Spark Scala中创建ArrayType列

[英]Create ArrayType column in Spark scala

要创建的架构的结构:

|-- col1: boolean (nullable = true)
|-- col2: array (nullable = true)
|    |-- element: struct (containsNull = true)
|    |    |-- col2_1: boolean (nullable = true)
|    |    |-- col2_2: string (nullable = true)

创建架构的代码:

val prodSchema = StructType(Array(StructField("col1", StringType), StructField("col2",ArrayType(Array(StructField("element",StructType(Array(StructField("col2_1",StringType)))))))))

错误:

found   : Array[org.apache.spark.sql.types.StructField]
required: org.apache.spark.sql.types.DataType
StructField("col2",ArrayType(Array(StructField("element",StructType(Array(StructField("col2_1",StringType)))))))

有关如何纠正此架构错误的任何建议。

您可以使用Schema DSL创建模式:

val col2 = new StructType().add($"col2_1".boolean).add($"col2_2".string)
val schema = new StructType()
                 .add($"col1".boolean)
                 .add($"col2".array(col2))

schema.printTreeString()

root
 |-- col1: boolean (nullable = true)
 |-- col2: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- col2_1: boolean (nullable = true)
 |    |    |-- col2_2: string (nullable = true)

希望能帮助到你。

尝试这个:

val schema = StructType(Seq(  
    StructField("col1",BooleanType,false),
    StructField("col2",ArrayType(StructType(Seq(  
                       StructField("col2_1",BooleanType,true),
                       StructField("col2_2",StringType,true)
                         )))
               )))

我想你可以这样写:

val prodSchema =
  StructType(
    List(
      StructField("col1", BooleanType),
      StructField("col2", ArrayType(
        StructType(
          List(
            StructField("col2_1", BooleanType),
            StructField("col2_2",StringType)
          )
        )
      ))
    )
  )


prodSchema.printTreeString()

root
 |-- col1: boolean (nullable = true)
 |-- col2: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- col2_1: boolean (nullable = true)
 |    |    |-- col2_2: string (nullable = true)

暂无
暂无

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

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