0

When I try to split a string by backtick (`) symbol in REPL, it gives me an array of string output and the same was working during run time also with my jar file

Suddenly, the jar file code is not splitting the string by backtick

This backtick is the delimiter and it's being passed as an argument from a shell script

I tried to manually put backtick in split function in the code but below is the error for both cases

    val delimiter=args(0)     //this argument value is backtick (`),passed from shell during spark-submit command like spark-submit jarfile \`
    val FileContent="tablename`dbname`type"

    case class ABC(a: String,b: String,c: String)  
                       
    def parseFile(): ABC= {
      val Array(a,b,c) = FileContent.split(delimiter) //this is where its giving below mentioned error, its not splitting by backtick
      ABC(a,b,c)
    }

    val individualRecord = parseFile

    Exception in thread "main" scala.MatchError: [Ljava.lang.String;@a4b2d8f (of class [Ljava.lang.String;)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.apache.spark.deploy.JavaMainApplication.start(SparkApplication.scala:52)
            at org.apache.spark.deploy.SparkSubmit.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:851)
            at org.apache.spark.deploy.SparkSubmit.doRunMain$1(SparkSubmit.scala:167)
            at org.apache.spark.deploy.SparkSubmit.submit(SparkSubmit.scala:195)
            at org.apache.spark.deploy.SparkSubmit.doSubmit(SparkSubmit.scala:86)
            at org.apache.spark.deploy.SparkSubmit$$anon$2.doSubmit(SparkSubmit.scala:926)
            at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:935)
            at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)

if(delimiter == "`") println("yes") else println("no")
//above prints **yes**
//and below prints **backtick**
delimiter match {
      case "`" => println("backtick")
      case _ => println("not a backtick")
    }
akm
  • 113
  • 1
  • 2
  • 10
  • 1
    Can you share the code and examples of data raising the error? – Gaël J Oct 27 '21 at 17:39
  • @GaëlJ added the code block and error details – akm Oct 28 '21 at 05:11
  • How do you _know_ that `args(0)` is what you expect? How have you tested it? Does `delimiter == "\`"` return **true**? – jwvh Oct 28 '21 at 06:08
  • What does `println(s"->$delimiter<-")` produce? You also might try `delimiter.map(_.toInt).mkString(",")`. – jwvh Oct 28 '21 at 07:00
  • @jwvh if(delimiter == "`") println("yes") else println("no") this gives yes and println(s"->$delimiter<-") produce ->\`<- – akm Oct 28 '21 at 07:18
  • I find it hard to believe that `delimiter == "\`"` but `fileContent.split(delimiter) != fileContent.split("\`")`. Something is amiss. – jwvh Oct 28 '21 at 07:42
  • yea split is not working. when i do println("records - "+fileContent.split(delimiter)), it gives this, records - [Ljava.lang.String;@4678a2eb – akm Oct 28 '21 at 07:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238640/discussion-between-akm-and-jwvh). – akm Oct 28 '21 at 08:20
  • I copied this into Scastie and it works: https://scastie.scala-lang.org/M5G6d2KiQeqgHW8e0OSDEQ – stefanobaghino Oct 28 '21 at 14:32

0 Answers0