英文:
"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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论