scala spark 数据对比,小于Spark Scala RDD中日期的比较

I want to print data of employees who joined before 1991. Below is my sample data:

69062,FRANK,ANALYST,5646,1991-12-03,3100.00,,2001

63679,SANDRINE,CLERK,69062,1990-12-18,900.00,,2001

Initial RDD for loading data:

val rdd=sc.textFile("file:home/hduser/Desktop/Employees/employees.txt").filter(p=>{p!=null && p.trim.length>0})

UDF for converting string column to date column:

def convertStringToDate(s: String): Date = {

val dateFormat = new SimpleDateFormat("yyyy-MM-dd")

dateFormat.parse(s)

}

Mapping each and every column to its datatype:

val dateRdd=rdd.map(_.split(",")).map(p=>(if(p(0).length >0 )p(0).toLong else 0L,p(1),p(2),if(p(3).length > 0)p(3).toLong else 0L,convertStringToDate(p(4)),if(p(5).length >0)p(5).toDouble else 0D,if(p(6).length > 0)p(6).toDouble else 0D,if(p(7).length> 0)p(7).toInt else 0))

Now I get data in tuples as below:

(69062,FRANK,ANALYST,5646,Tue Dec 03 00:00:00 IST 1991,3100.0,0.0,2001)

(63679,SANDRINE,CLERK,69062,Tue Dec 18 00:00:00 IST 1990,900.0,0.0,2001)

Now when I execute command I am getting below error:

scala> dateRdd.map(p=>(!(p._5.before("1991")))).foreach(println)

:36: error: type mismatch;

found : String("1991")

required: java.util.Date

dateRdd.map(p=>(!(p._5.before("1991")))).foreach(println)

^

So where am I going wrong ???

解决方案

Since you are working with rdd's and no df's and you have date strings with simple date checking, the following non-complicated way for an RDD:

val rdd = sc.parallelize(Seq((69062,"FRANK","ANALYST",5646, "1991-12-03",3100.00,2001),(63679,"SANDRINE","CLERK",69062,"1990-12-18",900.00,2001)))

rdd.filter(p=>(p._5 < "1991-01-01")).foreach(println)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值