【发布时间】:2019-12-06 00:15:22
【问题描述】:
鉴于这些数据集:
产品元数据DF
{'asin': '0006428320', 'title': 'Six Sonatas For Two Flutes Or Violins, Volume 2 (#4-6)', 'price': 17.95, 'imUrl': 'http://ecx.images-amazon.com/images/I/41EpRmh8MEL._SY300_.jpg', 'salesRank': {'Musical Instruments': 207315}, 'categories': [['Musical Instruments', 'Instrument Accessories', 'General Accessories', 'Sheet Music Folders']]}
productsRatingsDF
{"reviewerID": "AORCXT2CLTQFR", "asin": "0006428320", "reviewerName": "Justo Roteta", "helpful": [0, 0], "overall": 4.0, "summary": "Not a classic but still a good album from Yellowman.", "unixReviewTime": 1383436800, "reviewTime": "11 3, 2013"}
还有这个功能:
def findProductFeatures(productsRatingsDF : DataFrame, productsMetadataDF : DataFrame) : DataFrame = {
productsRatingsDF
.withColumn("averageRating", avg("overall"))
.join(productsMetadataDF,"asin")
.select($"asin", $"categories", $"price", $"averageRating")
}
这是基于 asin 连接这两个数据集的正确方法吗?
这是我得到的错误:
Exception in thread "main" org.apache.spark.sql.AnalysisException: grouping expressions sequence is empty, and '`asin`' is not an aggregate function. Wrap '(avg(`overall`) AS `averageRating`)' in windowing function(s) or wrap '`asin`' in first() (or first_value) if you don't care which value you get.;;
Aggregate [asin#6, helpful#7, overall#8, reviewText#9, reviewTime#10, reviewerID#11, reviewerName#12, summary#13, unixReviewTime#14L, avg(overall#8) AS averageRating#99]
+- Relation[asin#6,helpful#7,overall#8,reviewText#9,reviewTime#10,reviewerID#11,reviewerName#12,summary#13,unixReviewTime#14L] json
我理解的错误是否正确,我加入的方式有误? 我尝试更改 .withColumn 和 .join 的顺序,但没有奏效。 当我尝试根据 asin 编号将 avg("overall") 输入到列中时,似乎也出现了错误。
最终结果应该是,我得到一个包含 4 列“asin”、“categories”、“price”和“averageRating”的数据框。
【问题讨论】:
标签: scala apache-spark