if (self.config.filter_deleted_flag) & ( is_deleted_column in df.columns): # if True removes records that marked deleted filter = (F.col(ranking_column) == 1) & (F.col(is_deleted_column) == "False") else: self.logger.warning(f"Records marked as deleted will be loaded to {self.config.table_full_name} table!") filter = F.col(ranking_column) == 1
时间: 2024-03-18 22:43:56 浏览: 199
这段代码是用来过滤数据框中被标记为“删除”的记录的。如果`self.config.filter_deleted_flag`为True并且`is_deleted_column`在数据框`df`的列中,则筛选出排名为1且未被标记为“删除”的记录。如果`self.config.filter_deleted_flag`为False,则会发出警告,并筛选出排名为1的所有记录,包括被标记为“删除”的记录。其中`F`是指`pyspark.sql.functions`模块中的函数,用于构建Spark SQL表达式。
相关问题
def deduplicate(self, df: DataFrame) -> DataFrame: key_columns = [F.col(column_name) for column_name in self.config.deduplication_key_columns] order_by_columns = [F.col(column_name).desc() for column_name in self.config.deduplication_order_columns] if self.config.deduplicate_order_by_type == "asc": order_by_columns = [F.col(column_name) for column_name in self.config.deduplication_order_columns] ranking_column = "duplicates_rank" is_deleted_column = "IsDeleted" if (self.config.filter_deleted_flag) & ( is_deleted_column in df.columns): # if True removes records that marked deleted filter = (F.col(ranking_column) == 1) & (F.col(is_deleted_column) == "False") else: self.logger.warning(f"Records marked as deleted will be loaded to {self.config.table_full_name} table!") filter = F.col(ranking_column) == 1 return df.withColumn(ranking_column, F.row_number().over(Window.partitionBy(key_columns).orderBy(order_by_columns))).filter( filter).drop(ranking_column)
这段代码是用来去重的,输入是一个DataFrame,输出也是一个DataFrame。该函数首先根据配置文件中的列名获取需要去重的列和排序列,然后按照排序列进行降序或升序排列。接着,该函数给每个分组内的记录打上重复排名,最后根据重复排名过滤出排名为1的记录并删除重复排名列。如果配置文件中开启了筛选标记为删除的记录的功能,并且DataFrame中有IsDeleted列,则会将标记为删除的记录过滤掉。如果没有开启该功能,则会将标记为删除的记录加载到目标表中。
key_columns = [F.col(column_name) for column_name in self.config.deduplication_key_columns] order_by_columns = [F.col(column_name).desc() for column_name in self.config.deduplication_order_columns] if self.config.deduplicate_order_by_type == "asc": order_by_columns = [F.col(column_name) for column_name in self.config.deduplication_order_columns] ranking_column = "duplicates_rank" is_deleted_column = "IsDeleted" if (self.config.filter_deleted_flag) & ( is_deleted_column in df.columns): # if True removes records that marked deleted filter = (F.col(ranking_column) == 1) & (F.col(is_deleted_column) == "False") else: self.logger.warning(f"Records marked as deleted will be loaded to {self.config.table_full_name} table!") filter = F.col(ranking_column) == 1这里能看出源数据是怎么样的df结构,给我一个例子,并且告诉我sql怎么筛选df的列出来
假设源数据`df`的列名和数据如下:
```
+----+-------+--------+-----------+
| id | name | gender | is_deleted|
+----+-------+--------+-----------+
| 1 | Alice | female | False |
| 2 | Bob | male | False |
| 3 | Bob | male | True |
| 4 | Alice | female | True |
| 5 | Carol | female | False |
+----+-------+--------+-----------+
```
则根据Python代码,可以使用以下SQL语句筛选出`key_columns`和`is_deleted_column`所对应的列:
```
SELECT id, name, gender, is_deleted
FROM source_table;
```
即将`source_table`替换为源数据表的表名,将`id, name, gender, is_deleted`替换为`self.config.deduplication_key_columns`和`is_deleted_column`所对应的列名,即可筛选出指定的列。
阅读全文