简体   繁体   English

将cloumn的数据类型从StringType转换为spark scala中dataframe中的StructType

[英]Convert datatype of cloumn from StringType to StructType in dataframe in spark scala

|     ID|CO_ID|                           DATA|
+--------------------+--------------------+----+
|ABCD123|abc12|[{"month":"Jan","day":"monday"}] |
|BCHG345|wed34|[{"month":"Jul","day":"tuessay"}]|

I have dataframe above in which column DATA is of StringType.I want it to convert to StructType. 我上面有数据框,其中DATA是StringType.I希望它转换为StructType。 How can I do this? 我怎样才能做到这一点?

Use from_json 使用from_json

df.withColumn("data_struct",from_json($"data",StructType(Array(StructField("month", StringType),StructField("day", StringType)))))

On Spark 2.4.0, I get the following 在Spark 2.4.0上,我得到以下内容

import org.apache.spark.sql.types.{StructType, StructField, StringType}

val df = List ( ("[{\"month\":\"Jan\",\"day\":\"monday\"}]")).toDF("data")

val df2 = df.withColumn("data_struct",from_json($"data",StructType(Array(StructField("month", StringType),StructField("day", StringType)))))

df2.show

+--------------------+-------------+
|                data|  data_struct|
+--------------------+-------------+
|[{"month":"Jan","...|[Jan, monday]|
+--------------------+-------------+

df2.printSchema

root
 |-- data: string (nullable = true)
 |-- data_struct: struct (nullable = true)
 |    |-- month: string (nullable = true)
 |    |-- day: string (nullable = true)

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

相关问题 Spark - 将包含 JSON 字符串的列从 StringType 转换为 ArrayType(StringType()) - Spark - Convert a coulmn containing JSON string from StringType to ArrayType(StringType()) 将带有字符串列的 spark 数据框转换为 StructType 列 - Convert spark dataframe with string column to StructType column 从scala中的spark.structType读取 - reading from a spark.structType in scala 如何在 scala spark dataFrame 中将模式的 DataType 转换为另一个 - How to convert DataType of schema to another in scala spark dataFrame Spark Dataframe 转 StringType - Spark Dataframe to StringType VectorAssembler 不支持 StringType 类型的 scala spark 转换 - VectorAssembler does not support the StringType type scala spark convert 使用 Scala 将某个 DataType 的所有列的 DataType 转换为 Spark DataFrame 中的另一个 DataType - Convert DataType of all columns of certain DataType to another DataType in Spark DataFrame using Scala UDF中Scala中Spark dataframe的ListType、MapType、StructType字段的通用处理? - General processing on ListType, MapType, StructType fields of Spark dataframe in Scala in UDF? 使用 pyspark 将 StructType、ArrayType 转换/转换为 StringType(单值) - Convert / Cast StructType, ArrayType to StringType (Single Valued) using pyspark Spark Scala:将StructType转换为String - Spark Scala: Cast StructType to String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM