How to create a DataFrame from a text file in Spark

后端 未结 8 1048
滥情空心
滥情空心 2021-01-31 19:03

I have a text file on HDFS and I want to convert it to a Data Frame in Spark.

I am using the Spark Context to load the file and then try to generate individual columns f

8条回答
  •  [愿得一人]
    2021-01-31 19:37

    You will not able to convert it into data frame until you use implicit conversion.

    val sqlContext = new SqlContext(new SparkContext())
    
    import sqlContext.implicits._
    

    After this only you can convert this to data frame

    case class Test(id:String,filed2:String)
    
    val myFile = sc.textFile("file.txt")
    
    val df= myFile.map( x => x.split(";") ).map( x=> Test(x(0),x(1)) ).toDF()
    

提交回复
热议问题