【问题标题】:Timestamp field is losing precision when queried using spark-sql [duplicate]使用 spark-sql 查询时,时间戳字段失去精度 [重复]
【发布时间】:2017-06-28 20:57:02
【问题描述】:

当使用 Spark SQLHive Metastore 查询同一个表时,时间戳字段会丢失精度。

我的表格描述是这样的:

col_name  data_type  comment
id          bigint    null
name        string    null
joined_time timestamp null

使用 Hive QL,我得到了以毫秒为单位的 joined_time 值。 Hive QL 结果:

select * from employees;

1   foo 2016-07-04 02:12:10.0
2   bar 2016-07-04 02:12:10.0

在使用 spark-sql 时,我会失去精度,最多几分钟。例如:

val result = sqlContext.sql("select * from employees")
result.show()

1  foo 2016-07-04 02:12:...
2  bar 2016-07-04 02:12:...

【问题讨论】:

  • 它并没有失去精度。我刚刚截断了显示。你可以用 result.show(false) 显示它
  • @eliasah 没有带有布尔参数的 show 方法
  • 它说错误:type mismatch required int
  • 你是哪个版本的spark?我每天都用它:)
  • 我在 cloudera-quick-start-vm-5.4.2 中使用 spark 1.3.0

标签: scala apache-spark hive apache-spark-sql


【解决方案1】:

它并没有失去精度。它刚刚截断了显示。

由于Spark 1.6,你可以用result.show(false)显示它

http://spark.apache.org/docs/latest/api/scala/#org.apache.spark.sql.Dataset

val df = Seq((1,2),(2,4)).toDF("x","y")
df.show(false)
// +---+---+
// |x  |y  |
// +---+---+
// |1  |2  |
// |2  |4  |
// +---+---+

现在有时间戳:

sqlContext.sql("select current_timestamp()").show
// +--------------------+
// |                 _c0|
// +--------------------+
// |2017-02-10 14:40:...|
// +--------------------+

sqlContext.sql("select current_timestamp()").show(false)
// +-----------------------+
// |_c0                    |
// +-----------------------+
// |2017-02-10 14:40:14.038|
// +-----------------------+

【讨论】:

猜你喜欢
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2017-11-04
相关资源
最近更新 更多