2016-05-27 5 views
4

私はコードを実行するためにspark-shellを使用しています。私のコードでは、関数を定義し、その関数をそのパラメータで呼びます。同じエラーが発生したデータ型を持つSparkの "error:type mismatch"

問題は、関数を呼び出すときに以下のエラーが発生することです。

このエラーの背景は何ですか? SparkのGraphデータ型と何か関係がありますか?

コード:これは、関数 "countpermissions"の定義と呼び出しを含む、私のコードの一部です。

class VertexProperty(val id:Long) extends Serializable 
case class User(val userId:Long, val userCode:String, val Name:String, val Surname:String) extends VertexProperty(userId) 
case class Entitlement(val entitlementId:Long, val name:String) extends VertexProperty(entitlementId) 

def countpermissions(es:String, sg:Graph[VertexProperty,String]):Long = { 
    return 0 
} 

val triplets = graph.triplets 
val temp = triplets.map(t => t.attr) 
val distinct_edge_string = temp.distinct  
var bcast_graph = sc.broadcast(graph)   
val edge_string_subgraph = distinct_edge_string.map(es => es -> bcast_graph.value.subgraph(epred = t => t.attr == es)) 
val temp1 = edge_string_subgraph.map(t => t._1 -> countpermissions(t._1, t._2)) 

コードは、上記のエラーが発生する最後の行までエラーなしで実行されます。

答えて

4

ここがトリックです。

scala> case class Foo(i: Int) 
defined class Foo 

と、このクラスで動作するシンプルな機能::

scala> def fooToInt(foo: Foo) = foo.i 
fooToInt: (foo: Foo)Int 

がクラスの再定義:

scala> case class Foo(i: Int) 
defined class Foo 

をしてインスタンスを作成します。

REPLを開き、クラスを定義することができます
scala> val foo = Foo(1) 
foo: Foo = Foo(1) 

すべてwh左の番号はfooToIntです。

scala> fooToInt(foo) 
<console>:34: error: type mismatch; 
found : Foo(in class $iwC)(in class $iwC)(in class $iwC)(in class $iwC) 
required: Foo(in class $iwC)(in class $iwC)(in class $iwC)(in class $iwC) 
      fooToInt(foo) 

おなじみですか?しかし、何が起こっているか、より良いアイデアを得るために別のトリック:

scala> case class Foo(i: Int) 
defined class Foo 

scala> val foo = Foo(1) 
foo: Foo = Foo(1) 

scala> case class Foo(i: Int) 
defined class Foo 

scala> def fooToInt(foo: Foo) = foo.i 
<console>:31: error: reference to Foo is ambiguous; 
it is imported twice in the same scope by 
import INSTANCE.Foo 
and import INSTANCE.Foo 
     def fooToInt(foo: Foo) = foo.i 

だから、長い話短いが、これは同じスコープ内の既存の曖昧な定義から生じる期待、少し混乱しているが、動作です。

REPL状態では、作成するエンティティを追跡する必要があります。タイプ定義が変更された場合、続行する前に曖昧な定義を保持しないようにしてください(必要に応じて上書きする)。

+0

元の著者がどのように彼の問題を解決すべきか、この回答からは不明です。これは、異なるコードを使用して同様のエラーメッセージを作成する方法のみを示すようです。 – GeorgeLewis

+0

@GeorgeLewisフィードバックいただきありがとうございます。それは期待された動作です。ちょうどエラーメッセージはあまり明確ではありません。 REPLの状態を清潔に保ち、あいまいな定義を削除/上書きする以外に、ここでは何もしません。 – zero323

関連する問題