[英]BigQuery convert unix timestamp struct to struct of datetime
I have a BigQuery table that contains a struct column called daySliderTimes
in the following form:我有一个 BigQuery 表,其中包含一个名为daySliderTimes
的结构列,格式如下:
daySliderTimes STRUCT<_field_1 STRUCT<_seconds INT, _nanoseconds INT>, _field_1 STRUCT<_seconds INT, _nanoseconds INT>
. daySliderTimes STRUCT<_field_1 STRUCT<_seconds INT, _nanoseconds INT>, _field_1 STRUCT<_seconds INT, _nanoseconds INT>
。
_field_1
and _field_2
represent two different timestamps. _field_1
和_field_2
代表两个不同的时间戳。 _seconds
and _nanoseconds
represent time since the unix epoch. _seconds
和_nanoseconds
表示自 Unix 纪元以来的时间。
I want to convert the data into a new STRUCT
with the following form:我想将数据转换为具有以下形式的新STRUCT
:
daySlidertimes STRUCT<startTime DATETIME, endTime DATETIME>
This is the table as seen in the BigQuery UI:这是在 BigQuery UI 中看到的表:
If you want to create a new table from the old one with the format daySlidertimes STRUCT<startTime DATETIME, endTime DATETIME>
, you can cast the data in milliseconds and so then transform it to TIMESTAMP with the function "TIMESTAMP_MICROS", check this link to see the amount of functions to parse timestamp [1].如果要从旧表创建一个格式为daySlidertimes STRUCT<startTime DATETIME, endTime DATETIME>
的新表,您可以以毫秒为单位转换数据,然后使用“TIMESTAMP_MICROS”函数将其转换为 TIMESTAMP,请查看此链接查看解析时间戳的函数数量 [1]。
An example of the query should look something like this:查询示例应如下所示:
CREATE TABLE `project.dataset.new_table` AS
SELECT searchDocId,
STRUCT(TIMESTAMP_MICROS(CAST(
((daySliderTimes.field1.seconds * 1e+6) +
ROUND(daySliderTimes.field1.nanoseconds * 0.001)) AS INT64)) as
startTime,
TIMESTAMP_MICROS(CAST( ((daySliderTimes.field2.seconds * 1e+6) +
ROUND(daySliderTimes.field2.nanoseconds * 0.001)) AS INT64)) as endTime)
as daySliderTimes,
enabledDaySliders
FROM `project.dataset.old_table`
[1] https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_timestamp [1] https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_timestamp
You can use TIMESTAMP_SECONDS() function.您可以使用TIMESTAMP_SECONDS()函数。 This function converts the seconds to DATETIME format.此函数将秒数转换为DATETIME格式。
Therefore, you are ale to transform daySliderTimes._field_1.seconds to a date using TIMESTAMP_SECONDS() function.因此,您可以使用TIMESTAMP_SECONDS()函数将daySliderTimes._field_1.seconds转换为日期。 As well as, for _field_2 , then aggregate them in a new struct format.以及,对于_field_2 ,然后将它们聚合为新的结构格式。
During the creation of the view or table, in your select you can do the following:在创建视图或表期间,您可以在选择中执行以下操作:
WITH table_newStruct as(
SELECT
#Select all the desired fields
searchDocId,
STRUCT(TIMESTAMP_SECONDS(daySliderTimes._field_1.seconds) as startTime,
TIMESTAMP_SECONDS(daySliderTimes._field_.seconds) as endTime) as new_daySlidertimes
FROM 'table_source')
SELECT searchDocId, new_daySlidertimes
FROM 'table_newStruct'
In addition, the returned TIMESTAMP should be in the following format 1970-01-01 00:00:00 UTC .此外,返回的TIMESTAMP应采用以下格式1970-01-01 00:00:00 UTC 。 You can format it using the FORMAT_DATE() function.您可以使用FORMAT_DATE()函数对其进行格式化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.