I'm using Oracle connection of Airflow.
import cx_Oracle
from airflow.providers.oracle.hooks.oracle import OracleHook
from airflow.hooks.base_hook import BaseHook
The below code has Worked :
last_snap_date = '2021-03-23'
value = hook.get_records (sql="select distinct ven as name from Execution Metrics where date > TO_DATE(\'"+last_snap_date2+"\',\'YYYY-MM-DD\')")
But The Requirements was top using Bind Variables . Here , last_snap_date2 is the Varible i want to pass to method 'get_records' Using sql bind Variables . I tried multiple steps but failed .
def _process():
print('*********** Oracle Conection ***************')
#Oracle Conection
hook = OracleHook(oracle_conn_id="Oracle")
last_snap_date = '2021-03-23'
value = hook.get_records (sql="select distinct ven as name from Execution Metrics where date > TO_DATE(\'"+last_snap_date2+"\',\'YYYY-MM-DD\')")
with DAG(
dag_id='OracleQuery',
start_date=dates.days_ago(2),
schedule_interval='@monthly',
catchup=False,
) as dag :
task_a =PythonOperator(
task_id="QueryIntegration",
python_callable=_process
)
Documentation for the get_records Methods for Reference :
def get_records(self, sql, parameters=None):
"""
Executes the sql and returns a set of records.
:param sql: the sql statement to be executed (str) or a list of
sql statements to execute
:type sql: str or list
:param parameters: The parameters to render the SQL query with.
:type parameters: dict or iterable
"""
with closing(self.get_conn()) as conn:
with closing(conn.cursor()) as cur:
if parameters is not None:
cur.execute(sql, parameters)
else:
cur.execute(sql)
return cur.fetchall()