将Scala SQL输出另存为DataFrame

时间:2019-04-18 20:36:19

标签: sql scala apache-spark-sql

我具有以下脚本来运行SQL查询:

val df_joined_sales_partyid = spark.sql(""" SELECT a.sales_transaction_id, b.customer_party_id, a.sales_tran_dt
                                            FROM df_sales_tran a 
                                            JOIN  df_sales_tran_party_xref b
                                            ON a.sales_transaction_id = b.sales_transaction_id
                                            Limit 3""")

我想知道如何将查询的输出保存为永久数据帧表。我注意到,每次运行display(df_joined_sales_partyid)时,似乎都会再次运行查询。如何避免多次运行查询并将结果保存到数据框表中。我刚开始编写Scala,所以如果这是一个简单的问题,请原谅我,但是我找不到在线解决方案。

1 个答案:

答案 0 :(得分:1)

// caches results in memory
df_joined_sales_partyid.cache() 

// or

// memory and disk, see https://spark.apache.org/docs/2.4.0/api/java/index.html?org/apache/spark/storage/StorageLevel.html for other possible values
df_joined_sales_partyid.persist(StorageLevel.MEMORY_AND_DISK) 
相关问题