我知道这里的列是字符串。如果是,
from_json
函数可以将它们解析为结构。
import org.apache.spark.sql.types.{StructField, StructType, IntegerType}
import org.apache.spark.sql.functions.from_json
val recTypeSchema = StructType(Array(
StructField("id", IntegerType, true)
))
val valueSchema = StructType(Array(
StructField("id", IntegerType, true),
StructField("user_id", IntegerType, true),
StructField("price", IntegerType, true)
))
val parsedDf = df
.withColumn("recType", from_json($"recType", recTypeSchema))
.withColumn("value", from_json($"value", valueSchema))
parsedDf.printSchema
root
|-- recType: struct (nullable = true)
| |-- id: integer (nullable = true)
|-- value: struct (nullable = true)
| |-- id: integer (nullable = true)
| |-- user_id: integer (nullable = true)
| |-- price: integer (nullable = true)
parsedDf.filter($"recType.id" === 1).show
+-------+------------+
|recType| value|
+-------+------------+
| {1}|{1, 100, 50}|
+-------+------------+