Skip to Main Content
Spark高级数据分析(第2版)
book

Spark高级数据分析(第2版)

by Sandy Ryza, Uri Laserson, Sean Owen, Josh Wills
June 2018
Beginner to intermediate content levelBeginner to intermediate
246 pages
6h 57m
Chinese
Posts & Telecom Press
Content preview from Spark高级数据分析(第2版)
纽约出租车轨迹的空间和时间数据分析
159
RichRow
类中,
getAs[T]
方法总是返回一个
Option[T]
,而不是直接返回原始值。如果
返回原始值,我们可以明确处理解析一个
Row
对象时出现的缺少字段的情况。在这个例子
中,数据集中的所有字段都是字符串,所以我们只需要处理
Option[String]
类型的值。
接下来,我们需要使用
Java
SimpleDateFormat
类的实例来处理上下车时间,并使用适当
的格式化字符串来获取以毫秒为单位的时间:
def parseTaxiTime(rr: RichRow, timeField: String): Long = {
val formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
val optDt = rr.getAs[String](timeField)
optDt.map(dt => formatter.parse(dt).getTime).getOrElse(0L)
}
然后,我们将使用
Scala
的隐式
toDouble
方法解析上下车位置的经度和纬度,将
String
式地转为
Double
,如果坐标缺失,则使用默认值
0.0
def parseTaxiLoc(rr: RichRow, locField: String): Double = {
rr.getAs[String](locField).map(_.toDouble).getOrElse(0.0) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

大数据项目管理:从规划到实现

大数据项目管理:从规划到实现

Ted Malaska, Jonathan Seidman
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey

Publisher Resources

ISBN: 9787115482525