IDE indicates the data type error of my Filter operator in Scala

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

IDE indicates the data type error of my Filter operator in Scala

Hung
This post was updated on .
Hi,

My Filter operator in Scala encounters that IDE indicates the data type error:
"overloaded method value filter with alternatives: (fun: ((String, Int)) ⇒ Boolean cannot be applied..."

Scala for me is quite new. I'm thinking the problem comes from the type doesn't match each other in map operator and filter operator:

val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex => Tuple2(vertex.annotation, vertex.id) }

class PruneToSampleVerticesFilter extends RichFilterFunction[AnnotatedVertex]

Did you have any suggestion that in which direction should I work on to solve this?

One thing makes me confused is the difference of "map" in the following and above(though there is still an error in filter operator):

val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex => AnnotatedVertex}
or
val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex => Tuple1<AnnotatedVertex>}

I'm thinking since the input of "filter" is AnnotatedVertex, would it be better to "map" to AnnotatedVertex?

Best regards,

Hung

----------------------------------------------------------------------------------------------------------
def compute(trackingGraphFile: String, PLDIndexFile: String, outputPath: String) = {

    implicit val env = ExecutionEnvironment.getExecutionEnvironment

    // Vertex => VertexName, VertexID
    val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex => Tuple2(vertex.annotation, vertex.id) }
    val trackingGraphEdges = GraphUtils.readEdges(trackingGraphFile)

    val trackingGraphVertices = trackingGraphEdges.map { edge => Tuple1(edge.target) }
                                                  .distinct

// IDE complains this filter
    val prunedPldIndex = pldIndex.filter(new PruneToSampleVerticesFilter)
                                                   .withBroadcastSet(trackingGraphVertices, "vertices")

    prunedPldIndex.writeAsCsv(outputPath, fieldDelimiter = "\t", writeMode = WriteMode.OVERWRITE)

    env.execute()
  }
 
  class PruneToSampleVerticesFilter extends RichFilterFunction[AnnotatedVertex] {

    var vertices: Set[Int] = null
   
    override def open(parameters: Configuration) = {
      vertices = getRuntimeContext.getBroadcastVariable[Tuple1[Int]]("vertices").map { _._1 }
                                                                                .toSet
    }
   
    override def filter(vertex: AnnotatedVertex): Boolean = {
      vertices.contains(vertex.id)
    }
  }

------------------------------------------------------------------------------------------------------------------
// case class
case class AnnotatedVertex(annotation: String, id: Int)
case class Edge(src: Int, target: Int)
Reply | Threaded
Open this post in threaded view
|

Re: IDE indicates the data type error of my Filter operator in Scala

Hung
Got it. Problem solved by changing the "map"
val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex => AnnotatedVertex(vertex.annotation, vertex.id) }
Reply | Threaded
Open this post in threaded view
|

Re: IDE complains my Filter operator in Scala

Till Rohrmann
In reply to this post by Hung
Hi Hung,

the problem is that the data set pIdIndex contains elements of type Tuple2[String, Int] whereas the defined filter function expects AnnotatedVertex elements. Removing the trailing map operation where you read the vertices should solve this problem.

Another problem is that in the open method of the RichFilterFunction you call map on a Java list. The method getBroadcastVariable returns a java.util.List object which does not support this method. You can easily solve this problem by importing scala.collection.JavaConverters.asScalaBufferConverter in the open method scope and then call getRuntimeContext.getBroadcastVariable[Tuple1[Int]]("vertices").asScala.map { _._1 }.toSet

If you should encounter other problems, then let us know.

Greets,

Till

On Tue, Mar 10, 2015 at 5:00 PM, HungChang <[hidden email]> wrote:
Hi,

My Filter operator in Scala encounters that IDE complains about the data
type:
"overloaded method value filter with alternatives: (fun: ((String, Int)) ⇒
Boolean cannot be applied..."

Scala for me is quite new. I'm thinking the problem comes from the type
doesn't match each other in map operator and filter operator:

val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex =>
Tuple2(vertex.annotation, vertex.id) }

class PruneToSampleVerticesFilter extends
RichFilterFunction[AnnotatedVertex]

Did you have any suggestion that in which direction should I work on to
solve this?

Best regards,

Hung

----------------------------------------------------------------------------------------------------------
def compute(trackingGraphFile: String, PLDIndexFile: String, outputPath:
String) = {

    implicit val env = ExecutionEnvironment.getExecutionEnvironment

    // Vertex => VertexName, VertexID
    val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex =>
Tuple2(vertex.annotation, vertex.id) }
    val trackingGraphEdges = GraphUtils.readEdges(trackingGraphFile)

    val trackingGraphVertices = trackingGraphEdges.map { edge =>
Tuple1(edge.target) }
                                                  .distinct

// IDE complains this filter
    val prunedPldIndex = pldIndex.filter(new PruneToSampleVerticesFilter)

.withBroadcastSet(trackingGraphVertices, "vertices")

    prunedPldIndex.writeAsCsv(outputPath, fieldDelimiter = "\t", writeMode =
WriteMode.OVERWRITE)

    env.execute()
  }

  class PruneToSampleVerticesFilter extends
RichFilterFunction[AnnotatedVertex] {

    var vertices: Set[Int] = null

    override def open(parameters: Configuration) = {
      vertices =
getRuntimeContext.getBroadcastVariable[Tuple1[Int]]("vertices").map { _._1 }

.toSet
    }

    override def filter(vertex: AnnotatedVertex): Boolean = {
      vertices.contains(vertex.id)
    }
  }

------------------------------------------------------------------------------------------------------------------
// case class
case class AnnotatedVertex(annotation: String, id: Int)
case class Edge(src: Int, target: Int)



--
View this message in context: http://apache-flink-incubator-user-mailing-list-archive.2336050.n4.nabble.com/IDE-complains-my-Filter-operator-in-Scala-tp820.html
Sent from the Apache Flink (Incubator) User Mailing List archive. mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: IDE indicates the data type error of my Filter operator in Scala

Till Rohrmann
In reply to this post by Hung
Great :-) Then you can forget my mail I just sent you.

On Tue, Mar 10, 2015 at 9:12 PM, HungChang <[hidden email]> wrote:
Got it. Problem solved by changing the "map"
val pldIndex = GraphUtils.readVertices(PLDIndexFile).map { vertex =>
AnnotatedVertex(vertex.annotation, vertex.id) }



--
View this message in context: http://apache-flink-incubator-user-mailing-list-archive.2336050.n4.nabble.com/IDE-indicates-the-data-type-error-of-my-Filter-operator-in-Scala-tp820p821.html
Sent from the Apache Flink (Incubator) User Mailing List archive. mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: IDE indicates the data type error of my Filter operator in Scala

Hung
In reply to this post by Hung
Thank you. Your explanation helps me to understand more.