This is an automated email from the ASF dual-hosted git repository.
damccorm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new a0081221f1d Fix tour of beam co-group-by-key python example (#29775)
a0081221f1d is described below
commit a0081221f1d2c697412b81b36c9921b09548a9a0
Author: Danny McCormick <[email protected]>
AuthorDate: Thu Dec 21 11:51:51 2023 -0500
Fix tour of beam co-group-by-key python example (#29775)
---
.../map/co-group-by-key/description.md | 48 ++++++++--------------
1 file changed, 17 insertions(+), 31 deletions(-)
diff --git
a/learning/tour-of-beam/learning-content/core-transforms/map/co-group-by-key/description.md
b/learning/tour-of-beam/learning-content/core-transforms/map/co-group-by-key/description.md
index c0e7f9f3e44..5400003b216 100644
---
a/learning/tour-of-beam/learning-content/core-transforms/map/co-group-by-key/description.md
+++
b/learning/tour-of-beam/learning-content/core-transforms/map/co-group-by-key/description.md
@@ -284,42 +284,28 @@ static PCollection<String>
applyTransform(PCollection<String> fruits, PCollectio
{{end}}
{{if (eq .Sdk "python")}}
```
-weight := beam.ParDo(s, func(_ []byte, emit func(string, int)){
- emit("brazil", 1000)
- emit("australia", 150)
- emit("canada", 340)
-}, beam.Impulse(s))
-
-fruits := beam.ParDo(s, func(_ []byte, emit func(string, string)){
- emit("australia", "cherry")
- emit("brazil", "apple")
- emit("canada", "banan")
-}, beam.Impulse(s))
+fruits = p | 'Fruits' >> beam.Create([('australia', 'cherry'), ('brazil',
'apple'), ('canada', 'banana')])
+weights = p | 'Countries' >> beam.Create([('australia', 1000), ('brazil',
150), ('canada', 340)])
```
-Change `Alphabet` to `ProductWeight`:
+Change `alphabet` to `product_weight`:
```
-type WordsAlphabet struct {
- Country string
- Fruit string
- ProductWeight int
-}
+class ProductWeight:
+ def __init__(self, product_weight, fruit, country):
+ self.product_weight = product_weight
+ self.fruit = fruit
+ self.country = country
```
The union takes place through the keys:
```
-func applyTransform(s beam.Scope, fruits beam.PCollection, countries
beam.PCollection) beam.PCollection {
- grouped := beam.CoGroupByKey(s, fruits, countries)
- return beam.ParDo(s, func(key string, weightIter func(*int) bool,
fruitIter func(*string) bool, emit func(string)) {
-
- wa := &WordsAlphabet{
- Country: key,
- }
- weightIter(&wa.ProductWeight)
- fruitIter(&wa.Fruit)
- emit(wa.String())
-
- }, grouped)
-}
+def apply_transforms(fruits, weights):
+ def cogbk_result_to_product_weight(cgbk_result):
+ (country, values) = cgbk_result
+ return WordsAlphabet(values['weights'][0], values['fruits'][0],
country)
+
+ return ({'fruits': fruits, 'weights': weights}
+ | beam.CoGroupByKey()
+ | beam.Map(cogbk_result_to_product_weight))
```
-{{end}}
\ No newline at end of file
+{{end}}