<jupyter_start><jupyter_text>GroupByKey<jupyter_code>bounded_data = [(random.choice(['a', 'b', 'c']), random.randint(0,10)) for _ in range(20)]
with beam.Pipeline(options=pipeline_options) as pipeline:
  (
      pipeline
      | beam.Create(bounded_data)
      | 'Number per letter' >> beam.GroupByKey()
      | 'Print Groups' >> beam.Map(print)
  )
with beam.Pipeline(options=pipeline_options) as pipeline:
  (
      pipeline
      | beam.Create(bounded_data)
      | 'Number of words' >> beam.GroupByKey()
      | 'Sum of lists' >> beam.Map(lambda x: (x[0], sum([int(n) for n in x[1]])))
      | 'Print words' >> beam.Map(print)
  )<jupyter_output><empty_output><jupyter_text>CoGroupByKey()<jupyter_code>dataA = zip(['a','b','c', 'a'], ['1','2','3', 4])
dataB = zip(['a','b','c'], ['11','22','33'])
with beam.Pipeline(options=pipeline_options) as p:
    singles = p | "table A" >> beam.Create(dataA)
    couples = p | "table B" >> beam.Create(dataB)
    merged = (
        {'singles': singles, 'couples':couples} 
        | "CoGroup" >> beam.CoGroupByKey()
        | "Print" >> beam.Map(print)
    )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19805481874447392, 'fraction_numerical': 0.015030946065428824, 'mean_word_length': 4.008849557522124, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '167709', 'license': 'MIT', 'n_tokens_mistral': 424, 'n_tokens_neox': 416, 'n_words': 76, 'path': '01-basics/013 - Groups.ipynb', 'repo_name': 'ivanCanaveral/apache-beam-tips', 'size': '1131'}