gpt4 book ai didi

Python Apache Beam : date value out of range

转载 作者:行者123 更新时间:2023-12-01 08:49:17 25 4
gpt4 key购买 nike

正在申请thisthis构建我的程序的示例,每次我尝试插入到 Big Query 时,都会出现此错误:

OverflowError:日期值超出范围[运行“Format”时]

我的光束管道是这样的:

Bigquery = (transformation
| 'Format' >> beam.ParDo(FormatBigQueryoFn())
| 'Write to BigQuery' >> beam.io.Write(beam.io.BigQuerySink(
'XXXX',
schema=TABLE_SCHEMA,
create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND
)))

在类FormatBigQueryoFn中应该是窗口数据时间的逻辑

示例1的代码:

def timestamp2str(t, fmt='%Y-%m-%d %H:%M:%S.000'):
"""Converts a unix timestamp into a formatted string."""
return datetime.fromtimestamp(t).strftime(fmt)

class TeamScoresDict(beam.DoFn):
"""Formats the data into a dictionary of BigQuery columns with their values
Receives a (team, score) pair, extracts the window start timestamp, and
formats everything together into a dictionary. The dictionary is in the format
{'bigquery_column': value}
"""

def process(self, team_score, window=beam.DoFn.WindowParam):
team, score = team_score
start = timestamp2str(int(window.start))
yield {
'team': team,
'total_score': score,
'window_start': start,
'processing_time': timestamp2str(int(time.time()))
}

示例2的代码:

class FormatDoFn(beam.DoFn):
def process(self, element, window=beam.DoFn.WindowParam):
ts_format = '%Y-%m-%d %H:%M:%S.%f UTC'
window_start = window.start.to_utc_datetime().strftime(ts_format)
window_end = window.end.to_utc_datetime().strftime(ts_format)
return [{'word': element[0],
'count': element[1],
'window_start':window_start,
'window_end':window_end}]

我的管道可能出了什么问题?

编辑:

例如,如果我打印 window.start 我得到:

Timestamp(-9223372036860)

最佳答案

问题是我之前从文件中读取数据,然后使用 Google Pub/Sub 进行测试。

当我从文件中读取数据时,元素没有时间戳。

元素中必须有时间戳。

Pub/Sub 自动附加此时间戳。

来自documentation :

最简单的窗口形式是使用固定时间窗口:给定一个可能不断更新的带有时间戳的 PCollection,每个窗口可能会捕获(例如)具有落入五分钟间隔的时间戳的所有元素。

关于Python Apache Beam : date value out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53194377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com