代码之家  ›  专栏  ›  技术社区  ›  Soheil Pourbafrani

Spark SQL错误分析异常:无法解析列名

  •  1
  • Soheil Pourbafrani  · 技术社区  · 6 年前

    这是spark sql中常见的错误,我尝试了所有其他的答案,但没有区别! 我想从hdfs(甚至本地文件系统)读取下面的csv小文件。

    +----+-----------+----------+-------------------+-----------+------+------+------+------+------+
    |  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());
    

    但它错了:

    Exception in thread "main" org.apache.spark.sql.AnalysisException: resolved attribute(s) 'first_name missing from  last_name#14,      test1#16,id#12, test4#19, ssn#15, test3#18, grade#21, test2#17, final#20, first_name#13 in operator 'Project [id#12, 'first_name];;
    'Project [id#12, 'first_name]
    +- Relation[id#12, first_name#13, last_name#14, ssn#15,      test1#16, test2#17, test3#18, test4#19, final#20, grade#21] csv
    

    在某些情况下,它可以正常工作:

    1. 如果我没有选择任何内容,它将成功获取所有数据。
    2. 如果我只选择“id”列。

    即使我试着用 意见 SQL 方法:

    df.createOrReplaceTempView("csvFile");
    spark.sql(SELECT id, first_name FROM csvFile).show()
    

    但我也有同样的错误!

    我用从数据库中读取的相同数据做了同样的操作,并且没有任何错误。

    我用火花 2.2.1 .

    2 回复  |  直到 6 年前
        1
  •  2
  •   Kaushal    6 年前

    不需要转换字符串[]-->列表<字符串>-->顺序<字符串>。

    只需在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(","));
    
        2
  •  2
  •   Soheil Pourbafrani    6 年前

    这是因为CSV文件的结构不正确!我移除了 white spaces 从csv文件,现在它工作了!