error: No implicit Ordering defined for Any

scala中经常遇到最头疼的问题,就是类型不匹配或者带Any,Option的提示错误信息。

最近碰到的是取最大值,但是明明已经Long类型的,却提示下面这个错误信息。

相关的源程序如下:

// 获取offset
1. val beginOffsets = KafkaTool.getBeginningOffset(broker,group,topic).map(o=>{ (KafkaTool.getPath(baseOffsetPath,group,topic,o._1.partition) -> o._2) }).toMap
// 根据partitionPath获取Map的值,这里返回的是一个Option[Long]类型 2. val b_offset
= beginOffsets.get(partitionPath).getOrElse(0L)
// 比较最大值
3. val max
= Seq(b_offset,offset.toLong).max

这里如果将上面的第2行添加返回类型,则会提示以下错误:

解决方法:

添加转换_.toLong

 val b_offset = beginOffsets.get(partitionPath).map(_.toLong).getOrElse(0L)
原文地址:https://www.cnblogs.com/30go/p/8274344.html