ApacheBeam:WriteToBigQuery无法正常工作,如果在之前使用了有状态转换,除非应用重新划分时间窗口。
创始人
2024-09-05 11:00:52
0次

解决方法是在WriteToBigQuery之前使用re-apply窗口转换,以确保它的输入是在全新的窗口之内。以下是使用Re-window操作符解决此问题的代码示例:

 class MyPipelineOptions(PipelineOptions):
   @classmethod
   def _add_argparse_args(cls, parser):
     parser.add_argument('--window_size', type=int, default=30)
 
 def run():
   options = PipelineOptions()
   p = beam.Pipeline(options=options)
   # Reads from PubSubTopic.
   lines = p | beam.io.ReadStringsFromPubSubTopic(input_topic)
   # Parsing PubSub message and adding Processing time.
   records = (lines
              | beam.ParDo(ParsePubSubMessage())
              | beam.Map(lambda elem: beam.window.TimestampedValue(elem, elem['timestamp']))
              | beam.WindowInto(window.FixedWindows(options.window_size)))
   # Keying by user id, then developing session information per window.
   sessioned_items = (records
                      | beam.ParDo(SessionGenerator())
                      | beam.Map(lambda elem: (elem['user_id'], elem)))
   # Stateful ParDo operation to keep track of session-level information.
   user_activity = (sessioned_items
                    | 'KeyByKeying' >> beam.GroupByKey()
                    | 'Stateful_Process' >> beam.ParDo(UserState()))
   # Adds start and end times in xx:xx:xx format and removes activity durations which are lower than the threshold.
   final_activity = (user_activity
                     | beam.ParDo(FormatSessionAndDiscardShortActivities()))
   # Writing results into BigQuery.
   final_activity | beam.io.WriteToBigQuery(output_table,
                                            schema=OUTPUT_SCHEMA,
                                            create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
                                            write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND)
   result = p.run()
   result.wait_until_finish()

相关内容

热门资讯

玩家必看教程!大赢家跑得快开挂... 玩家必看教程!大赢家跑得快开挂功能技巧,大赢家跑得快确实真的是有开挂技巧(有辅助挂教学)1、在大赢家...
玩家攻略推荐!德州扑克辅助器功... 玩家攻略推荐!德州扑克辅助器功能攻略,德州扑克好像有辅助器攻略(真的有辅助挂)1)德州扑克免费钻石:...
推荐一款!一起斗地主辅助功能神... 推荐一款!一起斗地主辅助功能神器,一起斗地主其实存在有辅助神器(有辅助挂教学)1、首先打开一起斗地主...
必看攻略!芷芷休闲透视挂功能软... 必看攻略!芷芷休闲透视挂功能软件,芷芷休闲都是有透视挂软件(有辅助挂方式)1、芷芷休闲有没有辅助教程...
2分钟细说!宝宝浙江开挂功能a... 2分钟细说!宝宝浙江开挂功能app,宝宝浙江都是是有开挂app(有辅助挂教学)小薇(辅助器软件下载)...
揭秘关于!棋牌游辅助功能攻略,... 揭秘关于!棋牌游辅助功能攻略,棋牌游竟然真的有辅助攻略(有辅助挂攻略)1.棋牌游 选牌创建新账号,点...
实测揭晓!七月游戏透视功能ap... 实测揭晓!七月游戏透视功能app,七月游戏确实是有透视app(有辅助挂猫腻)1、完成七月游戏辅助器v...
玩家必看科普!如意棋牌辅助器功... 玩家必看科普!如意棋牌辅助器功能工具,如意棋牌其实是真的有辅助器工具(有辅助挂详细)如意棋牌能透视中...
最新技巧!鼎盛棋牌开挂功能插件... 最新技巧!鼎盛棋牌开挂功能插件,鼎盛棋牌总是是有开挂插件(有辅助挂神器)1、在鼎盛棋牌插件功能辅助器...
揭秘一下!微友约局开挂功能教程... 揭秘一下!微友约局开挂功能教程,微友约局总是有开挂教程(真实有辅助挂)1、揭秘一下!微友约局开挂功能...