将Spark DataFrame保存为Hive表时出现问题

7

我有两个Spark的数据框架。其中一个是使用HiveContext从Hive表中接收的:

spark_df1 = hc.sql("select * from testdb.titanic_pure_data_test")    

我从.csv文件中得到的第二个spark数据框:

lines = sc.textFile("hdfs://HDFS-1/home/testdb/1500000_Sales_Records.csv").map(lambda line: line.split(","))    

spark_df_test = lines.toDF(['Region','Country','Item_Type','Sales_Channel','Order_Priority','Order_Date','Order_ID','Ship_Date','Units_Sold','Unit_Price','Unit_Cost','Total_Revenue','Total_Cost','Total_Profit'])`

我想将任何数据框保存为Hive表。

spark_df1.write.mode("overwrite").format("orc").saveAsTable("testdb.new_res5")

第一个数据框保存没有问题,但当我尝试以同样的方式保存第二个数据框 (spark_df_test) 时,出现了这个错误。
在"/home/jup-user/testdb/scripts/caching.py"文件的第90行,spark_df_test.write.mode("overwrite").format("orc").saveAsTable("testdb.new_res5")中,pyspark.sql.utils.AnalysisException错误被抛出,错误信息为 "Specifying database name or other qualifiers are not allowed for temporary tables. If the table name has dots (.) in it, please quote the table name with backticks (`)."。
1个回答

9
问题在于您正在尝试使用不同的数据框覆盖相同的Hive表,目前在Spark中无法完成此操作。
原因在于以下代码。如果表存在,则会引发异常。理想的方法是将数据框保存在新表中: spark_df_test.write.mode("overwrite").format("orc").saveAsTable("testdb.new_res6") 或者您可以使用'insertInto': spark_df_test.write.mode("overwrite").saveAsTable("temp_table") 然后,您可以覆盖目标表中的行。
val tempTable = sqlContext.table("temp_table") 
tempTable
       .write
       .mode("overwrite").insertInto("testdb.new_res5")

1
欢迎,@VladimirSazonov!有一个请求,您能否接受我的答案? - Avishek Bhattacharya
链接已经损坏。 - nikhase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接