简体   繁体   English

如何在Spark / Scala中将Array [String]转换为Array [Any]

[英]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: 我正在尝试在Graphframes中为parallelPersonalizedPageRank算法生成sourceIds,并按如下所示调用算法:

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. 我不知道在生成PPRIdCS时将String类型转换为Any类型的方法,还是将String映射为Any的方法是什么。 Thanks! 谢谢!

As noted from my comment, you can map to .asInstanceOf[Any]. 如我的评论所述,您可以映射到.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. 现在,执行此操作是假设您出于某种原因不希望如其他答案所述那样显式指定类型。

Just be explicit about the types: 请明确说明类型:

val PPRIdCS: Array[Any] = studentCS.select("id").collect.map(row => row.getString(0))

or better 或更好

val PPRIdCS: Array[Any] = studentCS.select("id").collect.map(row => row(0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM