timestamp字段在使用spark sql查询时会丢失精度

g6baxovj  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(485)

这个问题在这里已经有答案了

如何在spark数据框中显示完整的列内容(14个答案)
一年前关门了。
从中查询同一个表时,时间戳字段正在丢失精度 Hive Metastore 使用sparksql。
我的表格描述如下:

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

使用Hiveql,我得到 joined_time 以毫秒为单位的值精度。配置单元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:...
klh5stk1

klh5stk11#

它并没有失去精确性。它刚刚截断了显示。
从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|
// +-----------------------+

相关问题