【发布时间】:2021-05-27 21:57:02
【问题描述】:
使用 Spark 2.11,我有以下数据集(从 Cassandra 表中读取):
+------------+----------------------------------------------------------+
|id |attributes |
+------------+----------------------------------------------------------+
|YH8B135U123|[{"id":1,"name":"function","score":10.0,"snippets":1}] |
+------------+----------------------------------------------------------+
这是 printSchema():
root
|-- id: string (nullable = true)
|-- attributes: string (nullable = true)
attributes 列是 JSON 对象数组。我正在尝试将其分解为数据集,但一直失败。我试图将架构定义如下:
StructType type = new StructType()
.add("id", new IntegerType(), false)
.add("name", new StringType(), false)
.add("score", new FloatType(), false)
.add("snippets", new IntegerType(), false );
ArrayType schema = new ArrayType(type, false);
并将其提供给from_json,如下所示:
df = df.withColumn("val", functions.from_json(df.col("attributes"), schema));
这会因 MatchError 失败:
Exception in thread "main" scala.MatchError: org.apache.spark.sql.types.IntegerType@43756cb (of class org.apache.spark.sql.types.IntegerType)
这样做的正确方法是什么?
【问题讨论】:
标签: java json apache-spark apache-spark-sql