[英]Using Spark Data Frame or GlobalTempView with PySpark
我對 Spark 很陌生。 我設法將 PySpark 數據幀(在 %python 上下文中創建)傳遞給 Spark 數據幀(在 Scala 單元中使用)。 但我需要知道的是如何做相反的事情:使用在 Scala 單元格的 %python 上下文中創建的數據幀。
這是詳細信息。 我已經讀取了 txt 文件中的兩個表並將它們傳遞給 PySpark Dataframes。 我還將它們傳遞給 GlobalTempViews。
%python
#Pass txt curated files to Data Frames
df_a = spark.read.option("header","true").option("delimiter","|").csv(curated_path_a)
df_b = spark.read.option("header","true").option("delimiter","|").csv(curated_path_b)
df_a.createOrReplaceTempView("df_a")
df_b.createOrReplaceTempView("df_b")
然后我需要使用 Scala 代碼,所以我通過 PySpark 數據幀來觸發表
//Passing Pyspark Data Frames to Spark Data Frames
val df_a = spark.table("df_a")
val df_b = spark.table("df_b")
然后我加入了這些表並獲得了另一個名為 df_joined_tables 的數據框。 我將它傳遞給 GlobalTempView。
//Creating scala variables for each table
var table_name_a = dbutils.widgets.get("table_name_a")
var table_name_b = dbutils.widgets.get("table_name_b")
//Create scala variable for Join ID
var join_id = dbutils.widgets.get("table_name_b") + "ID"
//var join_id = table_name_b + "ID"
// Define join type
var join_type = dbutils.widgets.get("join_type")
// Joining Tables
var df_joined_tables = df_a
.join(df_b,
df_a(join_id)===df_b(join_id),
join_type
).select($"df_a.*",$"df_b.Description".alias(table_name_b))
df_joined_tables.createOrReplaceGlobalTempView("df_joined_tables")
display(df_joined_tables)
現在我需要在 %python 上下文中使用這個 df_joined_tables 。 我正在嘗試以下代碼,但出現錯誤“找不到表或視圖:df_joined_tables”。 我該如何解決? 先感謝您!
%python
df_joined_tables = spark.table("df_joined_tables")
要訪問全局臨時視圖,您需要在表名之前使用 global_temp 作為關鍵字。 在你的情況下試試這個:
%python
df_joined_tables = spark.table("global_temp.df_joined_tables")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.