new_insert_df = df.join(landing_merge_df, primary_keys_list, "left_anti")转化为sql语句
时间: 2023-11-27 14:06:02 浏览: 130
这段代码可以转换为以下SQL语句:
```
SELECT df.*
FROM df
LEFT JOIN landing_merge_df
ON df.primary_keys_list = landing_merge_df.primary_keys_list
WHERE landing_merge_df.primary_keys_list IS NULL;
```
其中,`df`和`landing_merge_df`分别代表两个数据表,`primary_keys_list`是主键列的列表。这条SQL语句的作用是查询在`df`表中存在,但在`landing_merge_df`表中不存在的记录。
相关问题
new_insert_df = df.join(landing_merge_df, primary_keys_list, "left_anti")
这段代码是什么意思?
这段代码是将两个数据框(df和landing_merge_df)按照主键列表(primary_keys_list)进行左反连接(left_anti join),返回的是df中有但landing_merge_df中没有的数据。也就是说,返回的是df中的数据,但是在landing_merge_df中不存在的数据。
if self.config.load_type == "INC": # adhoc hist job do not need to join landing merge table try: landing_merge_df = self.spark.read.format(self.config.destination_file_type). \ load(self.config.destination_data_path) # dataframe for updated records df = df.drop("audit_batch_id", "audit_job_id", "audit_src_sys_name", "audit_created_usr", "audit_updated_usr", "audit_created_tmstmp", "audit_updated_tmstmp") # dataframe for newly inserted records new_insert_df = df.join(landing_merge_df, primary_keys_list, "left_anti") self.logger.info(f"new_insert_df count: {new_insert_df.count()}") new_insert_df = DataSink_with_audit(self.spark).add_audit_columns(new_insert_df, param_dict) update_df = df.alias('l').join(landing_merge_df.alias('lm'), on=primary_keys_list, how="inner") update_df = update_df.select("l.*", "lm.audit_batch_id", "lm.audit_job_id", "lm.audit_src_sys_name", "lm.audit_created_usr", "lm.audit_updated_usr", "lm.audit_created_tmstmp", "lm.audit_updated_tmstmp") self.logger.info(f"update_df count : {update_df.count()}") update_df = DataSink_with_audit(self.spark).update_audit_columns(update_df, param_dict) # dataframe for unchanged records unchanged_df = landing_merge_df.join(df, on=primary_keys_list, how="left_anti") self.logger.info(f"unchanged_records_df count : {unchanged_df.count()}") final_df = new_insert_df.union(update_df).union(unchanged_df) print("final_df count : ", final_df.count()) except AnalysisException as e: if e.desc.startswith('Path does not exist'): self.logger.info('landing merge table not exists. will skip join landing merge') final_df = DataSink_with_audit(self.spark).add_audit_columns(df, param_dict) else: self.logger.error(f'unknown error: {e.desc}') raise e else: final_df = DataSink_with_audit(self.spark).add_audit_columns(df, param_dict) return final_df
这是一段Python代码,其中包含一个类方法的实现。该方法根据配置参数的不同,从一个特定的数据路径中将数据加载到一个Spark DataFrame中,并对该数据进行一些操作,最终返回一个具有审计列的DataFrame。如果配置参数是"INC",则会执行一些数据合并的操作,包括添加、更新和未更改的记录,并对这些记录添加审计列。如果配置参数是其他值,则只会添加审计列。
阅读全文