[英]Failed to execute user defined function on a dataframe in Spark (Scala)
[英]What input does my user defined function in spark dataframe take in?
我嘗試將兩列“格式組”和“格式子組”合並到一個名為格式的列中。
最后 Format 列中的 O/P 應采用 Format Group:Format Subgroup 的形式
我需要使用一些給定的數據創建自己的 UDF,但我不確定為什么我的 UDF 不喜歡我提供的輸入。
這是我使用的數據的第一行:
結帳DF :
BibNumber, ItemBarcode, ItemType, Collection, CallNumber, CheckoutDateTime
1842225, 0010035249209, acbk, namys, MYSTERY ELKINS1999, 05/23/2005 03:20:00 PM
數據字典DF:
Code, Description, Code Type, Format Group, Format Subgroup
acdvd, DVD: Adult/YA, ItemType, Media, Video Disc
更新代碼:將 seq[seq[string]] 更改為 String
def numberCheckoutRecordsPerFormat(checkoutDF: DataFrame, dataDictionaryDF: DataFrame): DataFrame = {
val createFeatureVector = udf{(Format_Group:String, Format_Subgroup:String) => {
dataDictionaryDF.map(x => if(Format_Group.flatten.contains(x)) 1.0 else 0.0)++Array(Format_Subgroup)
}
}
checkoutDF
.na.drop()
.join(dataDictionaryDF
.select($"Format_Group", $"Format_Subgroup", $"Code".as("ItemType"))
, "ItemType")
.withColumn("Format", createFeatureVector(dataDictionaryDF("Format_Group"), dataDictionaryDF("Format_Subgroup")))
.groupBy("ItemBarCode")
.agg(count("ItemBarCode"))
.withColumnRenamed("count(ItemBarCode)", "CheckoutCount")
.select($"Format", $"CheckoutCount")
}
此外, numberCheckoutRecordsPerFormat 應該返回給定項目的格式和結賬次數的數據幀 - 但我自己已經涵蓋了這部分。
使用的數據集是來自 Kaggle 的西雅圖圖書館結帳記錄
謝謝,人們!
Doomdaam,您可以嘗試使用concat_ws
內置函數(盡可能使用內置函數)。 您的代碼將如下所示:
checkoutDF
.na.drop()
.join(dataDictionaryDF
.select($"Format_Group", $"Format_Subgroup", $"Code".as("ItemType"))
, "ItemType")
.withColumn("Format", concat_ws(":",$"Format_Group", $"Format_Subgroup"))
.groupBy("ItemBarCode")
.agg(count("ItemBarCode"))
.withColumnRenamed("count(ItemBarCode)", "CheckoutCount")
.select($"Format", $"CheckoutCount")
否則,您的 UDF 將是:
val createFeatureVector = udf{(formatGroup:String, formatSubgroup:String) => Seq(formatGroup,formatSubgroup).mkString(":")}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.