org.apache.spark.sql.analysisexception:无法解析spark sql中的“column\u name”异常

lyfkaqu1  于 2021-05-31  发布在  Hadoop
关注(0)|答案(2)|浏览(2119)

我正在尝试从配置单元表中读取数据,然后添加具有空值的加法列。通过使用此选项,我得到以下错误:

Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve '`address_1`' given input columns: [postalcode, first_name, organization_name, application_number, type, last_name, country];;

实际上,地址\u 1不是配置单元中的列,正在尝试使用默认值“null”添加此列。
到目前为止我尝试的是:

val ipa_agent = hiveContext.sql("select * from agent")

val df1 = ipa_agent.withColumn("address_1",lit("null"))

除了withcolumn,还有其他方法添加列吗?

htrmnn0y

htrmnn0y1#

列出查询中的所有列+附加列:

val ipa_agent = hiveContext.sql("select postalcode, first_name, organization_name, application_number, type, last_name, country, cast(null as string) as address_1 from agent")
htrmnn0y

htrmnn0y2#

我尝试使用withcolumn添加一个新列,它对我来说很好

import sqlContext.implicits._
import org.apache.spark.sql.functions._

val df = sc.parallelize(Array(("101",1),("102",2))).toDF("id","rank")

val df_added_column = df.withColumn("address1", lit("null"));
df_added_column.show

    +---+----+--------+
    | id|rank|address1|
    +---+----+--------+
    |101|   1|    null|
    |102|   2|    null|
    +---+----+--------+

另一个选项是,您可以尝试leftjoin提到的方法,即提及所有列的名称,并在末尾添加address1作为null。

相关问题