DP-700 · Question #46
DP-700 Question #46: Real Exam Question with Answer & Explanation
The correct answer is A: Yes. The code correctly initializes variables for a Delta Lake MERGE operation, preparing the conditions for matching records and identifying changes based on candidate keys and change detection columns.
Question
You have the following code segment: def loading_pattern_sample(df_source): try: deltatable = DeltaTable.forName(spark, target_table) except Exception as e: df_source.write.format('delta').mode('overwrite').saveAsTable(f'{target_table}') except Exception as e: print(f'load for table {target_table} failed with error: {str(e)}') return try: change_detection_columns = [col for col in df_source.columns if col not in candidate_key] match_condition = ' AND '.join([f'target.{col} = source.{col}' for col in candidate_key]) update_condition = ' OR '.join([f'target.{col} != source.{col}' for col in change_detection_columns]) update_expr = {col: f'source.{col}' for col in df_source.columns} merge_operation = deltatable.alias('target').merge( source=df_source.alias('source'), condition=match_condition ).whenMatchedUpdate( condition=update_condition, set=update_expr ).whenNotMatchedInsertAll() merge_operation.execute() except Exception as e: print(f'insert operation for table {target_table} failed with error: {str(e)}') return Based on the code, will the merge operation always run?
Options
- AYes
- BNo
Explanation
The code correctly initializes variables for a Delta Lake MERGE operation, preparing the conditions for matching records and identifying changes based on candidate keys and change detection columns.
Common mistakes.
- B. The statement is true because the code correctly establishes the necessary conditions and expressions required for an effective Delta Lake upsert operation.
Concept tested. Delta Lake MERGE (UPSERT) operation conditions
Reference. https://docs.delta.io/latest/api/python/index.html#delta.tables.DeltaTable.merge
Topics
Community Discussion
No community discussion yet for this question.