Apache spark Spark SQL错误分析异常:无法解析列名称
这是Spark SQL中的一个常见错误,我尝试了所有其他答案,但没有区别! 我想从HDFS甚至本地文件系统中读取以下小CSV文件Apache spark Spark SQL错误分析异常:无法解析列名称,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,这是Spark SQL中的一个常见错误,我尝试了所有其他答案,但没有区别! 我想从HDFS甚至本地文件系统中读取以下小CSV文件 +----+-----------+----------+-------------------+-----------+------+------+------+------+------+ | id| first_name| last_name| ssn| test1| test2| test3| test4| final
+----+-----------+----------+-------------------+-----------+------+------+------+------+------+
| id| first_name| last_name| ssn| test1| test2| test3| test4| final| grade|
+----+-----------+----------+-------------------+-----------+------+------+------+------+------+
| 4.0| Dandy| Jim| 087-75-4321| 47.0| 1.0| 23.0| 36.0| 45.0| C+|
|13.0| Elephant| Ima| 456-71-9012| 45.0| 1.0| 78.0| 88.0| 77.0| B-|
|14.0| Franklin| Benny| 234-56-2890| 50.0| 1.0| 90.0| 80.0| 90.0| B-|
|15.0| George| Boy| 345-67-3901| 40.0| 1.0| 11.0| -1.0| 4.0| B|
|16.0| Heffalump| Harvey| 632-79-9439| 30.0| 1.0| 20.0| 30.0| 40.0| C|
+----+-----------+----------+-------------------+-----------+------+------+------+------+------+
代码如下:
List<String> cols = new ArrayList<>();
Collections.addAll(cols, "id, first_name".replaceAll("\\s+", "").split(","));
Dataset<Row> temp = spark.read()
.format("org.apache.spark.csv")
.option("header", true)
.option("inferSchema", true)
.option("timestampFormat", "yyyy/MM/dd HH:mm:ss ZZ")
.csv(path)
.selectExpr(JavaConverters.asScalaIteratorConverter(cols.iterator()).asScala().toSeq());
在某些情况下,它可以正常工作:
如果我没有选择任何内容,它将成功获取所有数据。
如果我只选择列id。
甚至我也尝试使用视图和SQL方法:
但我得到了同样的错误
我对从数据库中读取的数据做了同样的处理,没有任何错误
我使用Spark 2.2.1。无需转换字符串[]->List->Seq 只需在selectExpr方法中传递数组,因为selectExpr支持varargs数据类型
Dataset<Row> temp = spark.read()
.format("org.apache.spark.csv")
.option("header", true)
.option("inferSchema", true)
.option("timestampFormat", "yyyy/MM/dd HH:mm:ss ZZ")
.csv(path)
.selectExpr("id, first_name".replaceAll("\\s+", "").split(","));
无需转换字符串[]->List->Seq 只需在selectExpr方法中传递数组,因为selectExpr支持varargs数据类型
Dataset<Row> temp = spark.read()
.format("org.apache.spark.csv")
.option("header", true)
.option("inferSchema", true)
.option("timestampFormat", "yyyy/MM/dd HH:mm:ss ZZ")
.csv(path)
.selectExpr("id, first_name".replaceAll("\\s+", "").split(","));
这是因为CSV文件的结构不正确!我删除了CSV文件中的空白,现在它可以工作了 这是因为CSV文件的结构不正确!我删除了CSV文件中的空白,现在它可以工作了 请包括文件样本。问题第一部分的表格是csv文件上的全部数据。请包括文件样本。问题第一部分的表格是csv文件上的全部数据。使用字符串[]很好,但我仍然在线程main org.apache.spark.sql.AnalysisException中遇到错误异常:无法解析给定输入列的“first_name”:[final,test1,first_name,test3,id,grade,test2,ssn,test4,last_name];第1行位置0;'Project[id12,'first_name]+-Relation[id12,first_name 13,last_name 14,ssn15,test116,test217,test318,test419,final20,grade21]csvGood point to use String[],但我仍然在thread main org.apache.spark.sql.AnalysisException中遇到错误异常:无法解析给定输入列的'first_name':[final,test1,first_name,test3,id,grade,test2,ssn,test4,last_name];第1行位置0;'Project[id12,first_name]+-关系[id12,first_name 13,last_name 14,ssn15,test116,test217,test318,test419,final20,grade 21]csv
Dataset<Row> temp = spark.read()
.format("org.apache.spark.csv")
.option("header", true)
.option("inferSchema", true)
.option("timestampFormat", "yyyy/MM/dd HH:mm:ss ZZ")
.csv(path)
.selectExpr("id, first_name".replaceAll("\\s+", "").split(","));