欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java.lang.RuntimeException: The 6th field ‘CustomerID‘ of input row cannot be null

程序员文章站 2024-03-24 18:37:16
...

今天在写Spark-Sql的时候过滤数据一报错
看下错误截图
java.lang.RuntimeException: The 6th field ‘CustomerID‘ of input row cannot be null

错误代码:
java.lang.RuntimeException: The 6th field ‘CustomerID‘ of input row cannot be null
这两句都是判断字符串是否为空!
再来看一下原数据集里面的情况
java.lang.RuntimeException: The 6th field ‘CustomerID‘ of input row cannot be null
我认真检查了一下,确实只有第二列和第七列数据存在缺失值所以我过滤掉这两列的缺失值思路没有错,
错误的原因在于:字符串空值和null不是一个概念
在DataFrame中缺失值是用null表示的,而我前面的错误代码
也就是这两句

!x._3.isEmpty &&!x._7.isEmpt

是判断字符串是否为空!而不是判断是否为null,所以不会把缺失值null过滤掉!

正确的写法应该是

.filter(x =>
        x._3 != null  && //过滤掉商品号中的null
        !intRegx.findFirstIn(x._4).isEmpty &&   //以整型规则过滤Quantity列
        !timeRegx.findFirstIn(x._5).isEmpty &&  //以日期规则过滤InvoiceDate列
        !doubleRegx.findFirstIn(x._6).isEmpty && //以浮点型规则过滤UnitPrice列
        x._7 != null  //过滤掉顾客id中的null
    )

正确过滤操作代码截图:
java.lang.RuntimeException: The 6th field ‘CustomerID‘ of input row cannot be null
然后就过滤掉所有缺失值,可以顺利写入json文件啦!
写入json文件的时候只要有个缺失值就会报错哦,所以我也是大费周章才解决了这个问题!

上一篇: 数据库脚本生成POJO字段

下一篇: