Spark 1.6 - Remove itemsets with only 1 item

65 views Asked by At

I've the following code:

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()

And I'm trying to extract some association rules. For that I need to guarantee that all the transactions are groupped by more than one product. But with my code I'm getting transaction with only one product.

How can I filter that?

Thanks!

1

There are 1 answers

0
Raphael Roth On BEST ANSWER

Although I'm no 100% sure I got your requirement, I think this should work:

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"))