简体   繁体   English

Spark-从Scala代码调用Java方法时出现UnsupportedOperationException

[英]Spark - UnsupportedOperationException when calling Java method from Scala code

I've implemented code in Scala that is using a method written in Java. 我已经在Scala中使用使用Java编写的方法实现了代码。 In the code below processSale() is a Java method that takes util.List<Sale> as a parameter. 在下面的代码中, processSale()是一个Java方法,将util.List<Sale>作为参数。

I've converted Scala Iterable[Sale] to Seq[Sale] and then to util.List<Sale> with the help of scala.collection.JavaConverters._ 我已经在scala.collection.JavaConverters._的帮助下将Scala Iterable[Sale]转换为Seq[Sale] ,然后转换为util.List<Sale>

val parseSales: RDD[(String, Sale)] = rawSales
      .map(sale => sale.Id -> sale)
      .groupByKey()
      .mapValues(a => SaleParser.processSale(a.toSeq.asJava))

However when the code gets executed as part of a Spark driver the job fails due to the task failure with UnsupportedOperationException . 但是,当代码作为Spark驱动程序的一部分执行时,由于任务失败,并且UnsupportedOperationException导致作业失败。 I've looked through the logs and it appears that the reason is within the Java processSale method on the call of Collections.sort 我查看了日志,看来原因是在Collections.sort调用的Java processSale方法中

 Collections.sort(sales, new Comparator<InvocaCall>() {
                @Override
                public int compare(Sale sale1, Sale sale2) {
                    return Long.compare(sale1.timestamp, sale2.timestamp);
                }
            });

I'm stuck at this point because I'm passing the required util.List<Sale> . 由于我要传递所需的util.List<Sale>因此我被困在这一点上。 Why could Collections.sort be an unsupported operation in this case? 在这种情况下,为什么Collections.sort是不受支持的操作?

Add null check for rawSales util.List<Sale> . 为rawSales util.List<Sale>添加空检查。

   val parseSales: RDD[(String, Sale)] = if (rawSales.nonEmpty) 
           //rawSales specific stream operations
          else
           //None or any code as per requirement 

From this documentation : 本文档中

Because Java does not distinguish between mutable and immutable collections in their type, a conversion from, say, scala.immutable.List will yield a java.util.List , where all mutation operations throw an UnsupportedOperationException 因为Java不会区分可变集合和不可变集合的类型,所以从scala.immutable.List进行的转换将产生java.util.List ,其中所有变异操作都将引发UnsupportedOperationException

toSeq from your code returns immutable.Seq , that's why you get the exception. 代码中的toSeq返回immutable.Seq ,这就是为什么您会得到异常的原因。

So you can convert your list to mutable data structure like ListBuffer : 因此, 您可以将列表转换为可变数据结构,例如ListBuffer

list.to[scala.collection.mutable.ListBuffer].asJava

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

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