使用主题Schema创建PubSub转BigQuery订阅时TIMESTAMP类型兼容问题
PubSub转BigQuery时Timestamp类型不兼容问题
BigQuery表Schema
requested_at TIMESTAMP NULLABLE req_url STRING NULLABLE
PubSub主题Protobuf Schema
syntax = "proto3"; message Request { string req_url = 1; int64 requested_at = 2; }
报错信息
Incompatible schema type for field 'requested_at': field is INT64 in the topic schema, but TIMESTAMP in the BigQuery table schema.
问题说明
创建「写入BigQuery」类型的PubSub订阅并选择「使用主题Schema」选项时触发上述错误,但根据官方文档,int64是TIMESTAMP的推荐格式,对此存在困惑。
解决方法
核心原因是PubSub的自动Schema映射规则和BigQuery Write API的规则不统一:Write API支持将int64格式的时间戳转为TIMESTAMP,但PubSub的「使用主题Schema」功能只做严格类型匹配,Protobuf的int64只能对应BigQuery的INT64,无法直接映射到TIMESTAMP。
有两种可行方案:
- 调整BigQuery表结构:将
requested_at字段类型改为INT64,查询时通过TIMESTAMP_SECONDS(requested_at)(秒级时间戳)或TIMESTAMP_MILLIS(requested_at)(毫秒级时间戳)转换为TIMESTAMP类型使用。 - 自定义订阅转换模板:不选择「使用主题Schema」,而是自定义转换规则,将Protobuf的int64时间戳转为BigQuery的TIMESTAMP。示例转换模板:
若时间戳是毫秒级,将{ "BigQuerySchema": [ {"name": "req_url", "type": "STRING", "mode": "NULLABLE"}, {"name": "requested_at", "type": "TIMESTAMP", "mode": "NULLABLE"} ], "Translations": [ {"source": "req_url", "target": "req_url"}, { "source": "requested_at", "target": "requested_at", "transform": "TIMESTAMP_SECONDS(@)" } ] }TIMESTAMP_SECONDS替换为TIMESTAMP_MILLIS即可。
内容的提问来源于stack exchange,提问作者Yannis Weishaupt
相关产品推荐
相关产品推荐

