首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型不匹配,找到SortedSet,需要任意

类型不匹配,找到SortedSet,需要任意
EN

Stack Overflow用户
提问于 2019-04-01 05:47:26
回答 1查看 270关注 0票数 0

使用combinebyKey时,得到类型不匹配错误如下所示

代码语言:javascript
复制
scala> rdd.map(x => (x._1, x._2))
          .combineByKey( (x: Int) => x, 
                         (acc: SortedSet[Int], x: Int) => (acc += x), 
                         (acc1: SortedSet[Int], acc2: SortedSet[Int]) => (acc1 ++= acc2))

<console>:29: error: type mismatch;
 found   : (scala.collection.mutable.SortedSet[Int], Int) => scala.collection.mutable.SortedSet[Int]
 required: (Any, Int) => Any
       rdd.map(x => (x._1, x._2)).combineByKey( (x: Int) => x, (acc: SortedSet[Int], x: Int) => (acc += x), (acc1: SortedSet[Int], acc2: SortedSet[Int]) => (acc1 ++= acc2))
                                                                                             ^
<console>:29: error: type mismatch;
 found   : (scala.collection.mutable.SortedSet[Int], scala.collection.mutable.SortedSet[Int]) => scala.collection.mutable.SortedSet[Int]
 required: (Any, Any) => Any
       rdd.map(x => (x._1, x._2)).combineByKey( (x: Int) => x, (acc: SortedSet[Int], x: Int) => (acc += x), (acc1: SortedSet[Int], acc2: SortedSet[Int]) => (acc1 ++= acc2))

为什么scala不能将scala.collection.mutable.SortedSet[Int]视为Any

下面是我尝试过的代码:

代码语言:javascript
复制
import scala.collection.mutable.SortedSet
val data = Array((1, 1, 1), 
                 (1, 1, 2),
                 (1, 1, 3),
                 (1, 2, 1),
                 (1, 2, 2),
                 (1, 2, 3), 
                 (2, 1, 1), 
                 (2, 1, 2), 
                 (2, 1, 3), 
                 (2, 2, 1), 
                 (2, 2, 2), 
                 (2, 2, 3))
val rdd = sc.parallelize(data)

rdd.map(x => (x._1, x._2))
   .combineByKey( (x: Int) => x, 
                  (acc: SortedSet[Int], x: Int) => (acc += x), 
(acc1: SortedSet[Int], acc2: SortedSet[Int]) => (acc1 ++= acc2))

我希望得到((1,(1,2),(2,(1,2)),键/值对中的值不包含重复的元素。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-01 05:58:44

第一个函数的返回类型需要一个排序集,set需要知道如何构造组合器。像这样的东西应该能起作用

代码语言:javascript
复制
rdd.map(x => (x._1, x._2)).combineByKey( 
  (x: Int) => new mutable.TreeSet[Int] += x, 
  (acc: SortedSet[Int], x: Int) => (acc += x), 
  (acc1: SortedSet[Int], acc2: SortedSet[Int]) => (acc1 ++= acc2))
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55448759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档