scala - 在Scala中将时间戳值转换为双精度值
问题描述
所以我改变了TimeStamp的“t”,我必须把它转换成Double。我定义了这个类:
case class RawData(sessionId: String,
t: Double,
channel: Int,
signalName: String,
physicalValue: Double,
messageId: Long,
vehicleId: String)
而且我在这段代码中将“t”转换为double时遇到问题:
def raw(): Unit = {
import rawData.sqlContext.implicits._
//TODO solve timestamp
val datDMY = rawData
.map(row => {
cal.setTimeInMillis(row.t.)
RawDataExtended(
row.sessionId,
row.t,
row.channel,
row.signalName,
row.physicalValue,
row.messageId,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH)
)
})
解决方案
也许这可以帮助你:
case class InputModel(id: Int, time: Timestamp)
case class Foo(id: Int, timeLong: Long, timeDouble: Double)
val xs = Seq((1, Timestamp.from(Instant.now())), (2, Timestamp.from(Instant.now()))).toDF("id", "time")
val ys = xs
.select('id, 'time cast (DataTypes.LongType) as "timeLong", 'time cast (DataTypes.DoubleType) as "timeDouble")
.as[Foo]
val zs = xs
.as[InputModel]
.map(row => {
Foo(row.id, row.time.getTime.toLong, row.time.getTime.toDouble)
})
xs.show(false)
ys.show(false)
zs.show(false)
尽管在转换为 Double 时要小心时区并且要精确 - 请注意 Timestamp 与 Double 相比它是如何表示为 Long 的。
推荐阅读
- database - 当我停止程序并重新启动它时,我的 DataGridView 会暂时更新。但我的 Access 数据库仍然永久更新
- android - 为什么 Flutter App 不能在真机上运行
- java - 在日/夜模式之间切换时启用平滑的活动过渡
- javascript - target="_blank" 在 chrome 中不起作用?
- sass - Sass 从模块扩展类
- python - 使用 Python 和 PySpark 提取 URL 参数
- azure-sql-database - 创建新索引时防止 DTU 耗尽
- makefile - 单个项目的多个 Makefile
- c# - 上课做什么?(带问号的类)在 C# 泛型类型约束中是什么意思?
- mysql - 使用 If 结构的约束