我已经四处看了看。我是Java的新手,我正在尝试将Double转换为Instant。该用例适用于Google Dataflow Java SDK。 Double是我使用TextIO读取的文件的UNIX时间戳。当我System.out.println(row.get("timestamp"))
时,我确实得到了UNIX时间戳。当我System.out.println(row.get("timestamp").getClass().getName())
时,我得到java.lang.double
。我所拥有的如下:
static class ExtractTimestamp extends DoFn<TableRow, TableRow> {
@Override
public void processElement(ProcessContext c) {
TableRow row = c.element();
Instant timestamp = (Instant) row.get("timestamp");
c.outputWithTimestamp(row, timestamp);
}
}
我得到的错误是:
java.lang.Double cannot be cast to org.joda.time.Instant
问题是我想将两倍的UNIX时间戳转换为Instants,因此我可以将它传递给outputWithTimestamp
。我相信这应该是一个微不足道的问题,但我还没有找到解决方案。谢谢。
答案 0 :(得分:4)
你不能&#34;演员&#34; Double
到Instant
。您需要将时间戳作为constructor argument传递:
Instant timestamp = new Instant(((Number)row.getTimestamp("timestamp")).longValue());
这假设&#34;时间戳&#34;值以毫秒为单位。如果是秒,只需乘以1000。