我有CSV文件,如图所示:
name,age,languages,experience
'Alice',31,['C++', 'Java'],2
'Bob',34,['Java', 'Python'],2
'Smith',35,['Ruby', 'Java'],3
'David',36,['C', 'Java', 'R']4
在加载数据时,默认情况下,所有列都以字符串形式加载。
scala> val df = spark.read.format("csv").option("header",true).load("data.csv")
df: org.apache.spark.sql.DataFrame = [name: string, age: string ... 2 more fields]
scala> df.show()
+-------+---+------------------+----------+
| name|age| languages|experience|
+-------+---+------------------+----------+
|'Alice'| 31| ['C++', 'Java']| 2|
| 'Bob'| 34|['Java', 'Python']| 2|
|'Smith'| 35| ['Ruby', 'Java']| 3|
|'David'| 36|['C', 'Java', 'R']| 4|
+-------+---+------------------+----------+
scala> df.printSchema()
root
|-- name: string (nullable = true)
|-- age: string (nullable = true)
|-- languages: string (nullable = true)
|-- experience: string (nullable = true)
因此,我将自定义模式定义为String
、Integer
、Array
、Integer
数据类型:
import org.apache.spark.sql.types.{StructField, StructType, StringType, ArrayType, IntegerType}
val custom_schema = new StructType(Array(StructField("name", StringType), StructField("age", IntegerType), StructField("languages", ArrayType(StringType)), StructField("experience", IntegerType)))
当我使用自定义模式加载数据时,它抛出错误
运行命令后的终端屏幕截图
scala> val df = spark.read.format("csv").option("header",true).schema(custom_schema).load("data.csv")
org.apache.spark.sql.AnalysisException: CSV data source does not support array<string> data type.
at org.apache.spark.sql.execution.datasources.DataSourceUtils$.$anonfun$verifySchema$1(DataSourceUtils.scala:67)
at org.apache.spark.sql.execution.datasources.DataSourceUtils$.$anonfun$verifySchema$1$adapted(DataSourceUtils.scala:65)
at scala.collection.Iterator.foreach(Iterator.scala:941)
at scala.collection.Iterator.foreach$(Iterator.scala:941)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
at scala.collection.IterableLike.foreach(IterableLike.scala:74)
at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
at org.apache.spark.sql.types.StructType.foreach(StructType.scala:102)
at org.apache.spark.sql.execution.datasources.DataSourceUtils$.verifySchema(DataSourceUtils.scala:65)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:445)
at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:326)
at org.apache.spark.sql.DataFrameReader.$anonfun$load$3(DataFrameReader.scala:308)
at scala.Option.getOrElse(Option.scala:189)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:308)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:240)
... 47 elided
如何通过将列作为数组来加载数据以引发数据帧?
从文件中读取后,您可以将其转换为数组,方法是使用regexp_replace
删除方括号([
,]
(,并使用split
用逗号(,
(分割剩余字符串,例如.
val df = spark.read.format("csv").option("header",true).load("data.csv")
val transformedDf = df.withColumn("languages",
split(
regexp_replace(col("languages"),"\[|\]",""),
","
)
)