Caused by: java.lang.NumberFormatException: For input string: "|"

在這裏插入圖片描述
這個異常是在使用spark進行數據清洗處理的時候出現的異常,在百度上搜索,發現類似的異常都是出現在jsp頁面的參數設置中,困擾了好半天。

1|24|M|technician|85711
2|53|F|other|94043
3|23|M|writer|32067
4|24|M|technician|43537
5|33|F|other|15213
6|42|M|executive|98101
7|57|M|administrator|91344
8|36|M|administrator|05201
9|29|M|student|01002
10|53|M|lawyer|90703

這是原本的數據格式,以“|”作爲分隔符,所以一般的思路也是把”|”作爲分隔依據

 //讀取數據HDFS上
    val userRdd = sc.sparkContext.textFile("file:///C:/Users/Administrator/Desktop/ml-100k/u.user")
      .map(line=>(line.split("|"))) //需要進行轉義
      .map(t=>User(t(0).toInt,t(1).toInt,t(2),t(3),t(4).toInt))
    //4.導入相關的隱士依賴
    import  sc.implicits._
    val UserDF = userRdd.toDF()
    UserDF.select($"id",$"age",$"sex",$"occuption",$"number")
        .show()

這是代碼塊,這是在這裏忽略了切割字符需要進行轉義,不然確實會出現格式異常,在對“|”做了轉義處理後,正確得到了結果

正確的代碼塊

 //讀取數據HDFS上
    val userRdd = sc.sparkContext.textFile("file:///C:/Users/Administrator/Desktop/ml-100k/u.user")
      .map(line=>(line.split("\\|"))) //需要進行轉義
      .map(t=>User(t(0).toInt,t(1).toInt,t(2),t(3),t(4).toInt))
    //4.導入相關的隱士依賴
    import  sc.implicits._
    val UserDF = userRdd.toDF()
    UserDF.select($"id",$"age",$"sex",$"occuption",$"number")
        .show()

    sc.stop()

運行結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章