【发布时间】:2021-03-02 09:23:17
【问题描述】:
我正在使用 Spark 从 Cassandra 读取行,其中一列是 JSON 对象。这是数据集中的架构和示例行:
root
|-- attributes: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- name: string (nullable = true)
| | |-- count: integer (nullable = true)
| | |-- score: double (nullable = true)
| | |-- id: integer (nullable = true)
|-- id: string (nullable = true)
|-- col1: string (nullable = true)
+--------------------------------------------------------------------------------+--------+---------+
|attributes |id |col1 |
+--------------------------------------------------------------------------------+--------+---------+
|[[usage,2,5.0,12], [price,1,10.0,48], [hair,1,10.0,23737], [curls,1,10.0,30807]]|19400335|val_str_1|
+--------------------------------------------------------------------------------+--------+---------+
使用以下模式从表中加载:
ArrayType schema =
DataTypes.createArrayType(
new StructType(
new StructField[] {
new StructField("name", DataTypes.StringType, true, Metadata.empty()),
new StructField("count", DataTypes.IntegerType, true, Metadata.empty()),
new StructField("score", DataTypes.DoubleType, true, Metadata.empty()),
new StructField("id", DataTypes.IntegerType, true, Metadata.empty())}));
是否可以向 attributes 列注入新字段 - 将来自 id 列的值放在根下,并将来自 col1 列的值放在结构内?
【问题讨论】:
标签: java apache-spark apache-spark-sql