I want to get some records in the database, preprocess them to a new SQL script, and pass them to another operator
Here is my code example:
data = []
def getSomeData(**kwargs):
global data
conn = psycopg2.connect()
sql = "SELECT * from table"
cur = conn.cursor()
cur.execute(sql)
result = cur.fetchall()
data = result
print(data) #success
conn.commit()
cur.close()
def preprocessSQL(data: list) -> str:
sql = """
INSERT INTO (table2) VALUES
"""
for i in data:
#format here
....
return sql
with DAG(dag_id="get_and_push_data",
start_date=pendulum.datetime(2023, 1, 13),
) as dag:
getDataFromDB = PythonOperator(
task_id="get_data",
provide_context=True,
python_callable=getSomeData,
op_kwargs={},
)
sql = preprocessSQL(data)
# After that will push it into another operator
The problem is I can not get the variable in this way, I can print it in the get data but in the preprocess it is empty. How could I do?