scala中的java.lang.ArrayIndexOutOfBoundsException

时间:2018-01-22 20:31:18

标签: scala apache-spark

我是热衷于激发和机器学习的人。我正在处理一个数据集,其详细信息如下:

scala> val inp=sc.textFile("Telephone.txt")
inp: org.apache.spark.rdd.RDD[String] = Telephone.txt MapPartitionsRDD[1] at textFile at <console>:35

scala> inp.first()
res0: String = 2014-03-15:10:10:20,Sorrento,8cc3b47e-bd01-4482-b500-28f2342679af,33.6894754264,-117.543308253

scala> case class Telephone(dt:String,ct:String,s:String,lat:Double,lon:Double)

定义类电话

scala> val inp_split=inp.map(x=>x.split(","))
inp_split: org.apache.spark.rdd.RDD[Array[String]] = MapPartitionsRDD[2] at map at <console>:37

scala> val telrdd=inp_split.map(x=>Telephone(x(0),x(1),x(2),x(3).toDouble,x(4).toDouble))
telrdd: org.apache.spark.rdd.RDD[Telephone] = MapPartitionsRDD[3] at map at <console>:41

scala> val telDF=telrdd.toDF()
telDF: org.apache.spark.sql.DataFrame = [dt: string, ct: string, s: string, lat: double, lon: double]

但是当我在telDF上执行计数操作时,我收到以下错误:

scala> teldf.count()
[Stage 31:=============================>                            (1 + 1) / 2]18/01/22 20:16:19 WARN scheduler.TaskSetManager: Lost task 1.0 in stage 31.0 (TID 5
3, cloudera-slavenode2.cloudlab.com, executor 16): java.lang.ArrayIndexOutOfBoundsException: 1

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:1)

我认为你应该检查一下Telephone.txt。最有可能的问题来自于某些行存在错误的数据(例如只是一个空行),因此以下代码中没有x(1)

val inp_split=inp.map(x=>x.split(","))
val telrdd=inp_split.map(x=>Telephone(x(0),x(1),x(2),x(3).toDouble,x(4).toDouble))