Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 在Spark Scala中定义自定义项_Apache Spark_Spark Dataframe - Fatal编程技术网

Apache spark 在Spark Scala中定义自定义项

Apache spark 在Spark Scala中定义自定义项,apache-spark,spark-dataframe,Apache Spark,Spark Dataframe,我需要在Spark中使用一个UDF,它接受一个时间戳、一个整数和另一个数据帧,并返回一个包含3个值的元组 我一个接一个地犯错误,我不确定我是否还在努力纠正错误 以下是函数: def determine_price (view_date: org.apache.spark.sql.types.TimestampType , product_id: Int, price_df: org.apache.spark.sql.DataFrame) : (Double, java.sql.Timestamp

我需要在Spark中使用一个UDF,它接受一个时间戳、一个整数和另一个数据帧,并返回一个包含3个值的元组

我一个接一个地犯错误,我不确定我是否还在努力纠正错误

以下是函数:

def determine_price (view_date: org.apache.spark.sql.types.TimestampType , product_id: Int, price_df: org.apache.spark.sql.DataFrame) : (Double, java.sql.Timestamp, Double) = {
    var price_df_filtered = price_df.filter($"mkt_product_id" === product_id && $"created"<= view_date)
    var price_df_joined = price_df_filtered.groupBy("mkt_product_id").agg("view_price" -> "min", "created" -> "max").withColumn("last_view_price_change", lit(1))
    var price_df_final = price_df_joined.join(price_df_filtered, price_df_joined("max(created)") === price_df_filtered("created")).filter($"last_view_price_change" === 1)
    var result = (price_df_final.select("view_price").head().getDouble(0), price_df_final.select("created").head().getTimestamp(0), price_df_final.select("min(view_price)").head().getDouble(0))
    return result
}
val det_price_udf = udf(determine_price)
如果我开始添加参数,我会在其他错误中继续运行,例如Int expected Int.type found或object DataFrame不是包org.apache.spark.sql的成员

给出一些上下文:

我的想法是,我有一个包含价格、产品id和创建日期的数据框,还有一个包含产品id和查看日期的数据框

我需要根据上次创建的、早于查看日期的价格条目来确定价格


因为每个产品ID在第二个数据框中有多个查看日期。我认为UDF比交叉连接快。如果有人有不同的想法,我将不胜感激。

您不能在UDF中传递数据帧,因为UDF将在特定分区上的工作进程上运行。由于不能在Worker()上使用RDD,同样,也不能在Worker()上使用数据帧


你需要为此做一个变通

好的,我从de UDF参数中删除了数据帧。数据帧被缓存和广播,应该可以从函数中访问它。我仍然收到错误:
error:type mismatch;找到:Int.type required:Int val det_price_udf=udf(determine_price(org.apache.spark.sql.types.TimestampType,Int))
如果数据帧不在udf中,则它似乎无法使用。它不是我在Python中习惯的“全局变量”。不知道如何解决这个问题。您的用例是什么?我有一个数据框,其中包含很多产品的页面视图(产品id,查看日期),另一个数据框跟踪产品价格的变化(产品id,更改日期,价格)。对于第一个数据帧中的每个视图,我需要确定在查看页面之前发生了哪些价格变化。因此,如果产品X的价格在18日更改为500,20日更改为600,我需要确定19日记录的视图的价格为500,21日记录的视图的价格为600。我认为如果您可以将此作为单独的问题来提问,那么更好,这样更多的问题可以帮助您解决此用例!在那之前,我一直在思考这个问题!因为它解决了这个问题,所以接受这个答案!
error: missing argument list for method determine_price
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `determine_price _` or `determine_price(_,_,_)` instead of `determine_price`.