[英]How to create schema Array in data frame with spark
I have code to create data frame and this works fine if there is no array in my input data. 我有创建数据框的代码,如果我的输入数据中没有数组,则可以正常工作。
I tried using Json data which doen't have array and it runs successfully. 我尝试使用没有数组的Json数据,它成功运行。 My code is 我的代码是
val vals = sc.parallelize(
"""{"id":"1","name":"alex"}""" ::
Nil
)
val schema = (new StructType)
.add("id", StringType)
.add("name", StringType)
sqlContext.read.schema(schema).json(vals).select($"*").printSchema()
My question is, if I have input data with array like below then how to create schema? 我的问题是,如果我有如下所示的数组输入数据,那么如何创建模式?
val vals = sc.parallelize(
"""{"id":"1","name":"alex","score":[{"keyword":"read","point":10}]}""" ::
Nil
)
val schema = (new StructType)
.add("id", StringType)
.add("name", StringType)
Thanks. 谢谢。
Oke, I could have solution in my code. 好的,我可以在代码中找到解决方案。
Create schema in array in data frame spark you can this code. 在数据帧中的数组中创建模式可以触发此代码。
val vals = sc.parallelize(
"""{"id":"1","name":"alex","score":[{"keyword":"read","point":10}]}""" ::
Nil
)
val schema = StructType(
Array(
StructField("id", StringType),
StructField("name", StringType),
StructField("score", ArrayType(StructType(Array(
StructField("keyword", StringType),
StructField("point", IntegerType)
))))
)
)
and you print schema 然后您打印架构
sqlContext.read.schema(schema).json(vals).select($"*").printSchema()
Thanks is resolved 谢谢解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.