gpt4 book ai didi

python - 将 spark DataFrame 保存为 Hive 表的问题

转载 作者:太空狗 更新时间:2023-10-30 02:52:32 28 4
gpt4 key购买 nike

我有两个 spark 的数据框。其中之一是使用 HiveContext 从配置单元表中收到的:

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'])`

我想将任何数据框保存为配置单元表

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

第一个数据帧保存没有问题,但是当我尝试以同样的方式保存第二个数据帧(spark_df_test)时,我得到了这个错误

File "/home/jup-user/testdb/scripts/caching.py", line 90, in spark_df_test.write.mode("overwrite").format("orc").saveAsTable("testdb.new_res5") File "/data_disk/opt/cloudera/parcels/CDH-5.15.1-1.cdh5.15.1.p0.4/lib/spark/python/lib/pyspark.zip/pyspark/sql/readwriter.py", line 435, in saveAsTable File "/data_disk/opt/cloudera/parcels/CDH-5.15.1-1.cdh5.15.1.p0.4/lib/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in call File "/data_disk/opt/cloudera/parcels/CDH-5.15.1-1.cdh5.15.1.p0.4/lib/spark/python/lib/pyspark.zip/pyspark/sql/utils.py", line 51, in deco 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 (`).;'

最佳答案

问题是您正试图用不同的数据框覆盖同一个配置单元表。这现在无法在 spark 中完成。

原因如下code .这确保了表是否存在以抛出异常。理想的方法是将数据框保存在新表中

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")

关于python - 将 spark DataFrame 保存为 Hive 表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53010579/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com