【发布时间】:2020-06-06 12:44:34
【问题描述】:
我有这段代码,几乎所有的转换都使用返回数据框的 withColumn 函数。我使用 as[Recipe] 将从 preProcessing 返回的数据帧转换为数据集,但是由于所有函数都使用 .as 一遍又一遍地返回数据帧没有意义。
所以我的问题是 DataSet[U] over Dataset[Row]/DataFrame 的用例是什么?在我的情况下,是否值得使用 Dataset,因为每次转换(带有列)架构都会发生变化?
case class Recipe(
name: String,
ingredients: String,
url: String,
image: String,
cookTime: String,
recipeYield: String,
datePublished: DateType,
prepTime: String,
description: String
)
private def preProcessing[T](spark: SparkSession, data: DataFrame): DataFrame = {
data
.transform(lowerCaseColumn("ingredients"))
.transform(lowerCaseColumn("name"))
.transform(covertStringToDate("datePublished"))
}
private def transform[T](
spark: SparkSession,
data: Dataset[Recipe]
): DataFrame = {
data
.transform(filterRecipesWithBeef())
.persist(StorageLevel.MEMORY_AND_DISK_SER)
.transform(covertRecipeTimeColToMinutes("cookTime"))
.transform(covertRecipeTimeColToMinutes("prepTime"))
.transform(calculateTotalCookingTime())
.transform(calculateRecipeDifficulty())
.transform(calculateAvgCookingtimeByDifficulty())
}
【问题讨论】:
标签: scala apache-spark