“WrappedArray$ofRef cannot be cast to String” 在 Java Spark 中的异常。

huangapple go评论83阅读模式
英文:

"WrappedArray$ofRef cannot be cast to String" Exception in Java Spark

问题

在Spark Java编写代码时,访问DataFrame的行的列时遇到了这个错误。我不明白为什么从行中获取的是WrappedArray而不是普通的数组。

java.lang.ClassCastException: class scala.collection.mutable.WrappedArray$ofRef cannot be cast to class [Ljava.lang.String

示例代码:

String[] myarray = my_df_row.getAs("col_name");

我需要Java代码,不要Scala或Python。如何解决这个问题?

英文:

When writing in Spark Java I'm meeting this error while accessing a column of row of a Dataframe. I don't get why WrappedArray is retrieved from the Row instead of a normal Array.

java.lang.ClassCastException: class scala.collection.mutable.WrappedArray$ofRef cannot be cast to class [Ljava.lang.String

Example code :
String[] myarray = my_df_row.getAs("col_name");

I need Java code, not scala or python.
How to solve this issue ?

答案1

得分: 1

The WrappedArray needs to be converted to a Java list and then an Array. Code example:

import scala.collection.JavaConversions;

String[] myarray;
myarray = JavaConversions.seqAsJavaList(my_df_row.getAs("col_name")).toArray(myarray);
英文:

The WrappedArray needs to be converted to a Java list and then an Array. Code example :

import scala.collection.JavaConversions;

String[] myarray;
myarray = JavaConversions.seqAsJavaList(my_df_row.getAs("col_name")).toArray(myarray);

huangapple
  • 本文由 发表于 2020年8月12日 00:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63362403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定