nerdexam
Microsoft

DP-700 · Question #46

You have the following code segment: def loading_pattern_sample(df_source): try: deltatable = DeltaTable.forName(spark, target_table) except Exception as e…

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.

Ingest and transform data

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

How the community answered

(21 responses)
  • A
    67% (14)
  • B
    33% (7)

Why each option

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.

AYesCorrect

The code segment correctly defines the `match_condition` using `candidate_key` and the `update_condition` based on `change_detection_columns`, which are essential components for performing an upsert (update or insert) operation on a Delta table. These conditions are then passed to the `deltatable.merge` method, laying the groundwork for efficiently synchronizing data.

BNo

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

Source: https://docs.delta.io/latest/api/python/index.html#delta.tables.DeltaTable.merge

Topics

#Delta Lake#Merge operation#Python error handling#Control flow

Community Discussion

No community discussion yet for this question.

Full DP-700 Practice