Search code examples
arraysscalaapache-sparkgraphgraphframes

How to convert Array[String] to Array[Any] in Spark/Scala


I am trying to generate sourceIds for the parallelPersonalizedPageRank algorithm inside Graphframes and call the algoirthm as following:

val PPRIdCS = studentCS.select("id").collect.map(row => row.getString(0))
val ranksCS = studentGraph
  .parallelPersonalizedPageRank
  .resetProbability(0.15)
  .maxIter(10)
  .sourceIds(PPRIdCS)
  .run()

The error information I got is as following:

Message: <console>:46: error: type mismatch;
found   : Array[String]
required: Array[Any]
Note: String <: Any, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 
3.2.10).sourceIds(PPRIdCS)

I could not figure out what is the way to cast a String type to Any type, or a way to map String to Any while generating PPRIdCS. Thanks!


Solution

  • As noted from my comment, you can map to .asInstanceOf[Any].

    Example:

    val arr = Array("a", "b") 
    val asAny = arr.map(_.asInstanceOf[Any])
    

    This also appears to work...

    val asAny = arr.asInstanceOf[Array[Any]]
    

    Now doing this is assuming you for some reason do not want to specify the type explicitly as noted by the other answer.