【问题标题】:Using groupByKey to combine values for using with FPGrowth使用 groupByKey 组合值以与 FPGrowth 一起使用
【发布时间】:2016-03-20 06:15:25
【问题描述】:
我有格式为用户、项目的文件,我想将其与Spark Itemsets 一起使用。我已经这样做了:
val data = sc.textFile("myfile")
.map(line => (line.trim.split(' ')(0), line.trim.split(' ')(1)))
.groupByKey()
val fpg = new FPGrowth().setMinSupport(0.2).setNumPartitions(10)
val model = fpg.run(data)
但它在抱怨
推断类型参数 [Nothing,(String, Iterable[String])] 不
符合方法运行的类型参数界限 [Item,Basket <:>
【问题讨论】:
标签:
scala
apache-spark
data-mining
【解决方案1】:
Basket 必须是 java.lang.Iterable,所以 Tuple2 或 Scala Iterable 都不能在这里工作。在将数据传递给run 方法之前,只需放下键并将篮子转换为Array:
val data = sc.parallelize(Seq("1 a", "1 b", "2 b", "2 c"))
.map(_.split(" ") match {
case Array(id, item, _*) => (id, item)
})
.groupByKey()
.values // Take only values
.map(_.toArray) // Convert to Array
val fpg = new FPGrowth().setMinSupport(0.2).setNumPartitions(10)
val model = fpg.run(data)