KeyError on passing PCollection as side input on Apache Beam

936 views Asked by At

I'm passing side_input PCollection as a side input to the ParDo transform, but getting the KeyError for the same

import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from beam_nuggets.io import relational_db
from processors.appendcol import AppendCol
from side_inputs.config import sideinput_bq_config
from source.config import source_config


with beam.Pipeline(options=PipelineOptions()) as si:
  side_input = si | "Reading from BQ side input" >> relational_db.ReadFromDB(
    source_config=sideinput_bq_config,
    table_name='abc',
    query="SELECT * FROM abc"
  )

with beam.Pipeline(options=PipelineOptions()) as p:
  PCollection = p | "Reading records from database" >> relational_db.ReadFromDB(
    source_config=source_config,
    table_name='xyzzy',
    query="SELECT * FROM xyzzy",
 ) | beam.ParDo(
   AppendCol(), beam.pvalue.AsIter(side_input)
 )

And below is the error

Traceback (most recent call last):
  File "athena/etl.py", line 40, in <module>
    extract()
  File "athena/etl.py", line 22, in extract
    PCollection = p | "Reading records from database" >> relational_db.ReadFromDB(
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/pipeline.py", line 555, in __exit__
    self.result = self.run()
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/pipeline.py", line 534, in run
    return self.runner.run_pipeline(self, self._options)
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/runners/direct/direct_runner.py", line 119, in run_pipeline
    return runner.run_pipeline(pipeline, options)
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/runners/portability/fn_api_runner/fn_runner.py", line 175, in run_pipeline
    self._latest_run_result = self.run_via_runner_api(
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/runners/portability/fn_api_runner/fn_runner.py", line 186, in run_via_runner_api
    return self.run_stages(stage_context, stages)
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/runners/portability/fn_api_runner/fn_runner.py", line 329, in run_stages
    runner_execution_context = execution.FnApiRunnerExecutionContext(
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/runners/portability/fn_api_runner/execution.py", line 323, in __init__
    self._build_data_side_inputs_map(stages))
  File "/Users/souvikdey/.pyenv/versions/3.8.5/envs/athena-venv/lib/python3.8/site-packages/apache_beam/runners/portability/fn_api_runner/execution.py", line 386, in _build_data_side_inputs_map
    producing_stage = producing_stages_by_pcoll[side_pc]
KeyError: 'ref_PCollection_PCollection_5'

I'm reading data from a PostgreSQL table, every element of the PCollection is a dictionary.

1

There are 1 answers

0
Pablo On BEST ANSWER

I think the problem is that you have two separate pipelines trying to work together. You should execute all your transforms as part of a single pipeline:

with beam.Pipeline(options=PipelineOptions()) as p:
  side_input = p | "Reading from BQ side input" >> relational_db.ReadFromDB(
    source_config=sideinput_bq_config,
    table_name='abc',
    query="SELECT * FROM abc")

  my_pcoll = p | "Reading records from database" >> relational_db.ReadFromDB(
        source_config=source_config,
        table_name='xyzzy',
        query="SELECT * FROM xyzzy",
    ) | beam.ParDo(
        AppendCol(), beam.pvalue.AsIter(side_input))