当我试图将输入表作为视图查询时,我会得到错误com.palantir.foundry.spark.api.errors.DatasetPathNotFoundException。我的代码如下:
def Median_Product_Revenue_Temp2(Merchant_Segments):
Merchant_Segments.createOrReplaceTempView('Merchant_Segments_View')
df = spark.sql('select * from Merchant_Segments_View limit 5')
return df我需要动态地查询这个表,因为我试图在许多字段中使用percentile_approx来计算中位数,而且我不知道如何在不使用spark.sql的情况下做到这一点。
如果我避免使用spark.sql来使用下面的代码计算大量字段的中值,则会导致Missing Transform Attribute: A module object does not have an attribute percentile_approx. Please check the spelling and/or the datatype of the object.错误。
import pyspark.sql.functions as F
exprs = {x: percentile_approx("x", 0.5) for x in df.columns if x is not exclustion_list}
df = df.groupBy(['BANK_NAME','BUS_SEGMENT']).agg(exprs)发布于 2022-02-25 20:11:14
我设法避免使用动态sql来使用以下代码计算跨列的中间值:
df_result = df.groupBy(group_list).agg(
*[ F.expr('percentile_approx(nullif('+col+',0), 0.5)').alias(col) for col in df.columns if col not in exclusion_list]
)将percentile_approx嵌入到F.expr中,绕过了我在后半部分的帖子中遇到的问题。
发布于 2022-06-27 06:14:49
试试createGlobalTempView。对我起作用了。
例:
df.createGlobalTempView("people")(不知道localTempView不起作用的根本原因)
https://stackoverflow.com/questions/71270673
复制相似问题