我正在尝试读取一个架构文件(它是一个文本文件)并将其应用于我的 CSV 文件而没有标题。由于我已经有一个架构文件,因此我不想使用InferSchema开销很大的选项。
我的输入架构文件如下所示,
"num IntegerType","letter StringType"
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下代码来创建架构文件,
val schema_file = spark.read.textFile("D:\\Users\\Documents\\schemaFile.txt")
val struct_type = schema_file.flatMap(x => x.split(",")).map(b => (b.split(" ")(0).stripPrefix("\"").asInstanceOf[String],b.split(" ")(1).stripSuffix("\"").asInstanceOf[org.apache.spark.sql.types.DataType])).foreach(x=>println(x))
Run Code Online (Sandbox Code Playgroud)
我收到如下错误
Exception in thread "main" java.lang.UnsupportedOperationException: No Encoder found for org.apache.spark.sql.types.DataType
Run Code Online (Sandbox Code Playgroud)
- 字段(类:“org.apache.spark.sql.types.DataType”,名称:“_2”) - 根类:“scala.Tuple2”
并尝试将其用作模式文件,同时使用spark.read.csv如下所示并将其写为 ORC 文件
val df=spark.read
.format("org.apache.spark.csv")
.option("header", false)
.option("inferSchema", true)
.option("samplingRatio",0.01)
.option("nullValue", "NULL")
.option("delimiter","|")
.schema(schema_file)
.csv("D:\\Users\\sampleFile.txt")
.toDF().write.format("orc").save("D:\\Users\\ORC")
Run Code Online (Sandbox Code Playgroud)
需要帮助将文本文件转换为架构文件并将我的输入 CSV 文件转换为 ORC。