Why does reading csv file with empty values lead to IndexOutOfBoundException?

后端 未结 4 1264
無奈伤痛
無奈伤痛 2021-01-19 19:10

I have a csv file with the foll struct

Name | Val1 | Val2 | Val3 | Val4 | Val5
John     1      2
Joe      1      2
David    1      2            10    11
         


        
4条回答
  •  我在风中等你
    2021-01-19 20:01

    You can do it as follows.

    val df = sqlContext
             .read
             .textfile(csvFilePath)
             .map(_.split(delimiter_of_file, -1)
             .map(
                 p => 
                  Row(
                    p(0), 
                    p(1),
                    p(2),
                    p(3),
                    p(4),
                    p(5),
                    p(6))
    

    Split using delimiter of your file. When you set -1 as limit it consider all the empty fields.

提交回复
热议问题