我在 DAG 中编写 DataQualityOperator。它应该检查 Redshift 表中是否有数据。为此,我想检查主列是否包含空值。使用sql,我找到了列主键的名称。如何检查它是否包含空值?(这意味着该表在我的情况下不好)。
class DataQualityOperator(BaseOperator):
check_template = """
SELECT a.attname
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = 'tablename'::regclass
AND i.indisprimary;
"""
def __init__ (self,redshift_conn_id = "", target_table="", *args, **kwargs):
super(...)
def execute(self, context):
self.log.info(f'DataQualityOperator processing {target_table}')
redshift = PostgresHook(postgres_conn_id=self.redshift_conn_id)
check_records = redshift.get_records(check_template.format(self.target_table))
如何做到这一点?感谢您的帮助。