无法使用 withColumn 将列添加到 Databricks 数据框,但模式反映了附加列

问题描述 投票:0回答:0

尝试在我的 databricks pyspark 数据框中添加额外的 2 列,但是当我从结果表中选择 * 时它没有显示。

for file in file_list:
  
  try:
    sql_query = create_sql_statement(file)
    df = spark.sql(sql_query) \
    .withColumn('type', F.lit('animal_type')) \
    .withColumn('timestamp', F.current_timestamp())
    df.write.format("delta").option("overwriteSchema", "true").mode("overwrite").saveAsTable(f'{database}.{table}')

  except Exception as e:
    print(e)

create_sql_statement 示例:'CREATE TABLE database.TABLE_NAME AS SELECT FIELD1, FIELD2, FIELD3, FIELD4, type, timestamp FROM DATABASE.TABLENAME'

运行上面的代码时,for 循环成功创建了表,我在下面看到了 pyspark df 结果,但没有看到在我的表中实现的新列。

num_affected_rows:long
num_inserted_rows:long
type:string
timestamp:timestamp

我看到两个结果之一:

  1. 当我从其中一个表中选择 * 时,我看到结果“查询未返回任何结果”,但是如果我从源数据库中选择 *。create_sql_statement 是从中构建的,那里肯定有数据。
  2. 根据表格,对于某些人,当我选择 * 我看到正确的输出,但没有从上面的“withColumn”子句中添加的列。

我在语法上遗漏了什么吗?这是我之前的问题“SQL Error mismatched input 'sql_query' expecting {EOF} when using Create Table in Pyspark”的后续问题,已解决。

sql pyspark schema databricks
© www.soinside.com 2019 - 2024. All rights reserved.