簡體   English   中英

在應用聚合函數時在 spark 中獲取 java.lang.ArrayIndexOutOfBoundsException: 1

[英]Getting java.lang.ArrayIndexOutOfBoundsException: 1 in spark when applying aggregate functions

我正在嘗試對數據集進行一些轉換。 在執行df.show()操作時讀取數據集后,我得到了 spark shell 中列出的行。 但是當我嘗試做df.count或任何聚合函數時,我得到

java.lang.ArrayIndexOutOfBoundsException: 1。

val itpostsrow = sc.textFile("/home/jayk/Downloads/spark-data")

import scala.util.control.Exception.catching    
import java.sql.Timestamp

implicit class StringImprovements(val s:String) {
      def toIntSafe = catching(classOf[NumberFormatException]) 
 opt    s.toInt
      def toLongsafe = catching(classOf[NumberFormatException]) 
 opt s.toLong
      def toTimeStampsafe = catching(classOf[IllegalArgumentException])       opt Timestamp.valueOf(s)
      }

case class Post(commentcount:Option[Int],lastactivitydate:Option[java.sql.Timestamp],ownerUserId:Option[Long],body:String,score:Option[Int],creattiondate:Option[java.sql.Timestamp],viewcount:Option[Int],title:String,tags:String,answerCount:Option[Int],acceptedanswerid:Option[Long],posttypeid:Option[Long],id:Long)

def stringToPost(row:String):Post = {
      val r = row.split("~")

      Post(r(0).toIntSafe,
           r(1).toTimeStampsafe,
           r(2).toLongsafe,
           r(3),
           r(4).toIntSafe,
           r(5).toTimeStampsafe,
           r(6).toIntSafe,
           r(7),
           r(8),
           r(9).toIntSafe,
           r(10).toLongsafe,
           r(11).toLongsafe,
           r(12).toLong)
   }
   
val itpostsDFcase1 = itpostsrow.map{x=>stringToPost(x)}
val itpostsDF = itpostsDFcase1.toDF()
    
    

如果文本文件包含一些空行或者拆分后的字段數不是 13,則您的函數stringToPost()可能會導致 Java 錯誤ArrayIndexOutOfBoundsException

由於 Spark 的惰性求值,只有在執行諸如count類的操作時才會注意到此類錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM