-2
  val result = data.map(line => {
  val event=line.split("\\^")

  val x=Integer.parseInt(event(2))
  val y=event(5)
  val z=event(4)

  (x,y,z,event)

}).filter(line => {
  var flag=false
  if(line._1==100 && line._4.length==7 && line._2.nonEmpty /*&& line._3.length()==8*/)
    flag =true
    flag
}).map(line => {
  (line._1,line._3)
})

println(result.count())
result.foreach(println)

Note: I am getting error java.lang.ArrayIndexOutOfBoundsException: 5

Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
  • 1234^1^100^2015-06-05 22:35:21.543^ ^0122648d-4352-4eec-9327-effae0c34ef2^2016060601 -- It is one record, where i want to parsexml column = 8 and value of c in the record with name and value – Mahesh Yadav Mar 25 '18 at 08:39
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Davis Broda Apr 03 '18 at 19:51

1 Answers1

0

I am getting error java.lang.ArrayIndexOutOfBoundsException: 5

Isn't the error message clear enough to understand what is the mistake? The error message is suggesting you that there are not enough ^ character in some of the lines you are splitting to get 5th array.

Solution

Use Try wherever you suspect there won't be enough data as

import scala.util.Try
val y=Try(event(5)) getOrElse("")
Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97