Read a TextFile Using Spark Scala

How to Read a Text File Using Spark Scala with Example

Reading a text file in Spark Scala is straightforward. You can use the textFile method.


import org.apache.spark.sql.SparkSession

object ReadTextFile {
    def main(args: Array[String]): Unit = {
      val sparkSession = SparkSession
        .builder()
        .appName("Our First scala spark code")
        .master("local")
        .getOrCreate()
      val salesDF = sparkSession.read.textFile("data/text/data.txt")
      salesDF.show(false)
      sparkSession.stop()
    }
  }
  

Output:

Alps

Below list contains some most commonly used options while reading a csv file