2017-09-12 2 views
0

저는 scala를 처음 사용하여 apache-spark를 배웠습니다. 스칼라가 문자열로 V 치료되는 이유는 무엇 graphX ​​graphX ​​코드에서 스칼라 유형 불일치 오류가 발생했습니다.

def foo(edge: EdgeTriplet[Map[Long, Double], Double]): Iterator[(VertexId, Map[Long, Double])] = { 

    val m = edge.srcAttr 

    for((k, v) <- m){ 
     if (v + edge.attr < edge.dstAttr.getOrElse(k, 10.0)) 
      Iterator(edge.dstId, Map(k -> v + edge.attr)) 

     else 

      Iterator.empty 

    } 

} 

오류

Name: Compile Error 
Message: <console>:37: error: type mismatch; 
found : Double 
required: String 
        Iterator(edge.dstId, Map(k -> v + edge.attr)) 
                 ^
<console>:35: error: type mismatch; 
found : Unit 
required: Iterator[(org.apache.spark.graphx.VertexId, Map[Long,Double])] 
    (which expands to) Iterator[(Long, Map[Long,Double])] 
      for((k, v) <- m){ 
        ^
StackTrace: 

스칼라에 간단한 함수를 썼다? 그리고 두 번째 오류의 원인은 무엇입니까? 도움이된다면

@Alexey에 의해 제안 코드를 편집 한 후, 나는 오류

Name: Compile Error 
Message: <console>:30: error: type mismatch; 
found : scala.collection.immutable.Map[org.apache.spark.graphx.VertexId,scala.collection.immutable.Map[Long,Double]] 
    (which expands to) scala.collection.immutable.Map[Long,scala.collection.immutable.Map[Long,Double]] 
required: Iterator[(org.apache.spark.graphx.VertexId, Map[Long,Double])] 
    (which expands to) Iterator[(Long, Map[Long,Double])] 
       (k, v) <- edge.srcAttr 
         ^
StackTrace: 

받고 있어요,이 code

답변

1

첫 번째에서 SendMessage 함수의 다른 버전을 구현하고 있습니다 Scala의 +에서 흔히 발생하는 문제입니다. 이 경우에는 예상대로 k -> (v + edge.attr)이 아니지만 (k -> v) + edge.attr입니다. 그리고 Tuple2의 유일한 + 메서드는 String을 허용합니다. 이를 수정하려면 올바른 괄호를 추가하십시오.

두 번째 오류는 for(...) { ... }Unit (이는 foreach 호출로 변환 됨)을 반환하기 때문입니다. yield이 누락되었습니다. 당신이 for를 사용하려면 사실, 솔루션에 내가 해결했다

m.flatMap { case (k, v) => 
    if (v + edge.attr < edge.dstAttr.getOrElse(k, 10.0)) 
    Iterator((edge.dstId, Map(k -> (v + edge.attr)))) 
    else 
    Iterator.empty[(Long, Map[Long,Double])] 
} 
+0

첫 번째 오류로 작성 원합니다

for { (k, v) <- m x <- if (v + edge.attr < edge.dstAttr.getOrElse(k, 10.0)) Iterator((edge.dstId, Map(k -> (v + edge.attr)))) else Iterator.empty[(Long, Map[Long,Double])] } yield x 

같은, 감사를해야합니다. 두 번째로 계속 검색 솔루션 – ashwinids

+0

편집 된 답변보기 나는 원래 그것을 발견하지 못했다. –

+0

오류 불일치가 발견되어 두 번째 문제에 대한 솔루션의 전체 코드를 작성할 수 있습니까? Iterable [Any], 필수 Iterator [(Long, Map [Long, Double]) – ashwinids