Spark 1.6 - 删除只有 1 个项目的项目集

Spark 1.6 - Remove itemsets with only 1 item

我有以下代码:

val df = sqlContext.sql("SELECT Transaction_ID,Product_ID FROM Transactions as tmp")
val rawDict = df.select('Product_ID).distinct().sort('Product_ID)
val dictCounts = rawDict.groupBy('Product_ID).count().filter(col("count") >= 2)
val sigCounts = dictCounts.filter('count === 1)
val dupCounts = dictCounts.filter('count > 1) 
val sigDescs = rawDict.join(sigCounts, "Product_ID").drop('count)
val invoiceToStockCode = df.select('Transaction_ID, 'Product_ID).distinct()
val baskets = invoiceToStockCode.groupBy('Transaction_ID).agg(collect_list('Product_ID).as('StockCodes)).cache()

我正在尝试提取一些关联规则。为此,我需要保证所有交易都按不止一种产品分组。但是使用我的代码,我只能与一种产品进行交易。

我该如何过滤?

谢谢!

虽然我不能 100% 确定我满足了您的要求,但我认为这应该可行:

val rawDict = df.select('Product_ID).sort('Product_ID)
val dictCounts = rawDict.groupBy('Product_ID).count()
val valid = dictCounts.filter('count > 1).drop('count) // only consider counts > 1
val singleRemoved=df.join(valid,Seq("Product_ID")).distinct()
val groupedTransactions = singleRemoved.groupBy("Transaction_ID").agg(collect_list("Product_ID"))