火花斯卡拉抛出java.util.NoSuchElementException:关键未找到:0例外 [英] spark scala throws java.util.NoSuchElementException: key not found: 0 exception

查看:677
本文介绍了火花斯卡拉抛出java.util.NoSuchElementException:关键未找到:0例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一片火花斯卡拉code的:

I have a piece of spark scala code like this:

val conf = new SparkConf().setAppName("MatrixInversion").setMaster("local")

val sc = new SparkContext(conf)

  def main (args: Array[String]) {

    var array:Array[Array[Double]]=new Array(2)
    for(i<- 0 until 2)
      array(i)=new Array(2)

    array(0)(0)=1
    array(0)(1)=2
    array(1)(0)=3
    array(1)(1)=4

    sc.makeRDD(array).cache()

    //val matrixA3=sc.textFile("A3")
    testCache()

    sc.stop()

  }

  def testCache():Unit={

    val rdds=sc.getPersistentRDDs
    val cacheArray=rdds(0).asInstanceOf[RDD[Array[Double]]]
    println("print cachaArray")
    cacheArray.collect()
    val cacheLength=cacheArray.collect().length
    println("length"+cacheLength)
  }

和现在都会好的。但是,当我取消注释该行: VAL matrixA3 = sc.textFile(A3)

and now it will be ok. But when I uncomment this line: val matrixA3=sc.textFile("A3")

它有这样的一些错误:

Exception in thread "main" java.util.NoSuchElementException: key not found: 0
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at com.scala.inversion.test$.testCache(test.scala:117)

为什么?

推荐答案

您code是错的其实。

Your code is wrong actually.

请参阅行

val cacheArray=rdds(0).asInstanceOf[RDD[Array[Double]]]

RDDS 在这里,其类型为 scala.collection.Map [诠释,org.apache.spark.rdd.RDD [_]]

但是,关键(的内部)并不总是永恒的。 (您可以缓存遥遥领先于其他事情)。为了您的code,键== 0 未在地图上找到。

However, the key (Int) is not always a constant one. (you may cache other thing ahead). For your code, key == 0 is not found in the map.

例如,我已经测试您的code在我的电脑中, RDDS

For example, I have tested your code in my computer, the rdds is

Map(9 -> ParallelCollectionRDD[9]

这篇关于火花斯卡拉抛出java.util.NoSuchElementException:关键未找到:0例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆