forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#34051 Add an ExtractWindowingInfo transform.
- Loading branch information
Showing
3 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
sdks/python/apache_beam/yaml/examples/transforms/aggregation/combine_sum_windowed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# coding=utf-8 | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# This is an example that illustrates how to use session windows and | ||
# then extract windowing information for further processing. | ||
|
||
pipeline: | ||
type: chain | ||
transforms: | ||
# Create some fake data. | ||
- type: Create | ||
name: CreateVisits | ||
config: | ||
elements: | ||
- user: alice | ||
timestamp: 1 | ||
- user: alice | ||
timestamp: 3 | ||
- user: bob | ||
timestamp: 7 | ||
- user: bob | ||
timestamp: 12 | ||
- user: bob | ||
timestamp: 20 | ||
- user: alice | ||
timestamp: 101 | ||
- user: alice | ||
timestamp: 109 | ||
- user: alice | ||
timestamp: 115 | ||
|
||
# Use the timestamp field as the element timestamp. | ||
# (Typically this would be assigned by the source.) | ||
- type: AssignTimestamps | ||
config: | ||
timestamp: timestamp | ||
|
||
# Group the data by user for each session window count the number of events | ||
# in each per session. | ||
# See https://beam.apache.org/documentation/programming-guide/#session-windows | ||
- type: Combine | ||
name: SumVisitsPerUser | ||
config: | ||
language: python | ||
group_by: user | ||
combine: | ||
visits: | ||
value: user | ||
fn: count | ||
windowing: | ||
type: sessions | ||
gap: 10s | ||
|
||
# Extract the implicit Beam windowing data (including what the final | ||
# merged session values were) into explicit fields of our rows. | ||
- type: ExtractWindowingInfo | ||
config: | ||
fields: [window_start, window_end, window_string] | ||
|
||
# Drop "short" sessions (in this case, Alice's first two visits.) | ||
- type: Filter | ||
config: | ||
language: python | ||
keep: window_end - window_start > 15 | ||
|
||
# Only keep a couple of fields. | ||
- type: MapToFields | ||
config: | ||
fields: | ||
user: user | ||
window_string: window_string | ||
|
||
- type: LogForTesting | ||
|
||
# Expected: | ||
# Row(user='bob', window_string='[7.0, 30.0)') | ||
# Row(user='alice', window_string='[101.0, 125.0)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters