2017-01-16 11 views
0

こんにちは私はScalaにとって非常に新しく、この単純なコードを実行しようとしていますが、コンパイルする:スカラ:抽象クラス:コンパイルエラー:クラスXは抽象である必要があります:[エラー]実装されていないメンバーがn個ある

name := "Simple Project" 

version := "1.0" 

scalaVersion := "2.10.4" 

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.5.0" 

libraryDependencies += "org.apache.spark" %% "spark-graphx" % "0.9.0-incubating" 

私はそれをコンパイルしようとすると、私が得る:

$ C:\"Program Files (x86)"\sbt\bin\sbt package 
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 
[info] Set current project to Simple Project (in build file:/C:/spark/simple/) 
[info] Compiling 1 Scala source to C:\spark\simple\target\scala-2.10\classes... 
[error] C:\spark\simple\src\main\scala\SimpleApp.scala:10: class Graph needs to be abstract, since: 
[error] it has 2 unimplemented members. 
[error] /** As seen from class Graph, the missing signatures are as follows. 
[error] * For convenience, these are usable as stub implementations. 
[error] */ 
[error] val edges: org.apache.spark.graphx.EdgeRDD[ED] = ??? 
[error] val vertices: org.apache.spark.graphx.VertexRDD[VD] = ??? 
[error] class Graph[VD, ED] { 
[error]  ^
[error] one error found 
[error] (compile:compileIncremental) Compilation failed 
[error] Total time: 6 s, completed Jan 16, 2017 11:48:51 PM 

私は

/* SimpleApp.scala */ 
import org.apache.spark.SparkContext 
import org.apache.spark.SparkContext._ 
import org.apache.spark.SparkConf 

import org.apache.spark._ 
import org.apache.spark.graphx._ 
import org.apache.spark.rdd.RDD 

class Graph[VD, ED] { 
    val vertices: VertexRDD[VD] 
    val edges: EdgeRDD[ED] 
} 

object SimpleApp { 
    def main(args: Array[String]) { 
    val conf = new SparkConf().setAppName("Simple Application") 
    val sc = new SparkContext(conf) 

    // Create an RDD for the vertices 
    val vertices: RDD[(VertexId, (Int, Int))] = 
     sc.parallelize(Array((1L, (7,-1)), (2L, (3,-1)), 
         (3L, (2,-1)), (4L, (6,-1)))) 

    // Create an RDD for edges 
    val relationships: RDD[Edge[Boolean]] = 
     sc.parallelize(Array(Edge(1L, 2L, true), Edge(1L, 4L, true), 
         Edge(2L, 4L, true), Edge(3L, 1L, true), 
        Edge(3L, 4L, true))) 

    // Create the graph 
    val graph = Graph(vertices, relationships) 

    // Check the graph 
    graph.vertices.collect.foreach(println) 

    sc.stop() 
    } 
} 

そして、ここではのは、SBTファイルですScalaにとって本当に新しいものです。必要なのは、小さく単純なコードを実行することだけですが、コンパイルすることができます。私は頂点と辺を_に設定しようとしましたが、valエッジのアンバインドプレースホルダパラメータが得られました。

答えて

1

このようにして、2つの未定義のメソッドでクラスを定義しています。したがって、抽象として定義するよう求めます。

おそらく、あなたはこのような何かしたい:あなたは二つのフィールドと2つのパラメータ(頂点と辺を)取らデフォルトコンストラクタを持つクラスを定義している

class Graph[VD, ED](
    val vertices: VertexRDD[VD], 
    val edges: EdgeRDD[ED]) { 
} 

このようにしてフィールドにそれぞれの値を代入します同じ名前。

キーワードvalは、その場所に置くと、コンストラクタのパラメータにクラスのフィールドのようにアクセスできるようにすることを意味します。

また、特定のニーズがない場合は、これを単純なタプルで処理する方が便利です。

1

[error] C:\spark\simple\src\main\scala\SimpleApp.scala:10: class Graph needs to be abstract, since:

[error] it has 2 unimplemented members.

これはエラーメッセージから示されています。クラス定義で不変フィールドverticesedgesの値を指定する必要があります。あなたは、あなたが好きな任意の値で、コンストラクタ本体でそれらを初期化すると言うことができ、次のいずれか

class Graph[VD, ED] { 
    val vertices: VertexRDD[VD] = /* calculation here */ 
    val edges: EdgeRDD[ED] = /* calculation here */ 
} 

またはそれらのインスタンスを作成するときにそのユーザーが値を指定することができるように、コンストラクタの引数としてそれらをリスト:

class Graph[VD, ED] (val vertices: VertexRDD[VD], val edges: EdgeRDD[ED]) 

ました

class Graph[VD, ED] (val theVertices: VertexRDD[VD], val theEdges: EdgeRDD[ED]) 
{ 
    val vertices = theVertices 
    val edges = theEdges 
} 
関連する問題