如何在 spark (scala) 中将 WrappedArray[WrappedArray[Float]] 转换为 Array[Array[Float]]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41904744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:02:55  来源:igfitidea点击:

How to cast a WrappedArray[WrappedArray[Float]] to Array[Array[Float]] in spark (scala)

arraysscalacastingspark-dataframeapache-spark-2.0

提问by bobo32

Im using Spark 2.0. I have a column of my dataframe containing a WrappedArrayof WrappedArrays of Float.

我使用的是 Spark 2.0。我的数据WrappedArray框有一列包含Float 的 WrappedArrays。

An example of a row would be:

一行的一个例子是:

[[1.0 2.0 2.0][6.0 5.0 2.0][4.0 2.0 3.0]]

Im trying to transform this column into an Array[Array[Float]].

我正在尝试将此列转换为Array[Array[Float]].

What I tried so far is the following:

到目前为止我尝试的是以下内容:

dataframe.select("mycolumn").rdd.map(r => r.asInstanceOf[Array[Array[Float]]])

but I get the following error:

但我收到以下错误:

Caused by: java.lang.ClassCastException:
 org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema cannot be cast to [[F

Any idea would be highly appreciated. Thanks

任何想法将不胜感激。谢谢

回答by Sami Badawi

Try this:

试试这个:

  val wawa: WrappedArray[WrappedArray[Float]] = null 
  val res: Array[Array[Float]] = wawa.map(inner => inner.array).toArray

It compiles for me

它为我编译

回答by bobo32

Following @sami-badawi 's answer I am posting the answer for those like me who started from a dataframe.

在@sami-badawi 的回答之后,我为像我这样从数据帧开始的人发布了答案。

dataframe.select("mycolumn").rdd.map
(row => row.get(0).asInstanceOf[WrappedArray[WrappedArray[Float]]].array.map(x=>x.toArray))