nerdexam
Microsoft

DP-700 · Question #47

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 is designed to handle the initial creation of the Delta table if it does not already exist, ensuring that the loading process can proceed without manual table setup.

Design and implement data ingestion and transformation

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, does the loading pattern support both full and incremental loading requirements?

Options

  • AYes
  • BNo

How the community answered

(29 responses)
  • A
    83% (24)
  • B
    17% (5)

Why each option

The code is designed to handle the initial creation of the Delta table if it does not already exist, ensuring that the loading process can proceed without manual table setup.

AYesCorrect

The initial `try-except` block correctly implements an idempotent mechanism where it first attempts to load an existing Delta table using `DeltaTable.forName`. If this operation fails (e.g., because the table does not exist), the `except` block then creates the table using `df_source.write.format('delta').mode('overwrite').saveAsTable`, ensuring the table is available for subsequent merge operations.

BNo

The statement is true because the code explicitly includes logic to create the target Delta table if it does not already exist, making the loading process more robust.

Concept tested: Idempotent Delta table creation for loading

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

Topics

#Delta Lake#Incremental Loading#Data Ingestion#Merge Operation

Community Discussion

No community discussion yet for this question.

Full DP-700 Practice