Search code examples
c#azureazure-data-factoryazure-data-lakeu-sql

How to handle a missing file in a U-SQL EXTRACT statement?


I have the following EXTRACT statement that grabs the data from both a staging file and the production file:

DECLARE @staging  string = "/staging/events.csv";
DECLARE @production string = "/production/events.csv";

@events =
        EXTRACT dimensionId         string
              , tenantId            string 
              , internalEntityId    long
              , name                string
              , modifiedTimestamp   DateTime
        FROM @staging, @production
        USING Extractors.Csv();

The statement will fail if either one of the two files is missing, which causes the whole Azure Data Factory pipeline to fail:

Input stream(s) don't exist

Question

How can I gracefully handle a missing file in a U-SQL EXTRACT statement?


Solution

  • I am not sure I can qualify this as "Gracefully" but at least it does the trick.

    I can use the FILE.EXISTS statement to test the presence of the file beforehand and choose to include it or not:

    DECLARE @staging  string = "/staging/events.csv";
    DECLARE @production string = "/production/events.csv";
    
    IF FILE.EXISTS(@production) == true THEN
     @events =
         EXTRACT    dimensionId         string
                  , tenantId            string 
                  , internalEntityId    long
                  , name                string
                  , modifiedTimestamp   DateTime
            FROM @staging, @production
            USING Extractors.Csv();
    ELSE
    @events =
         EXTRACT    dimensionId         string
                  , tenantId            string 
                  , internalEntityId    long
                  , name                string
                  , modifiedTimestamp   DateTime
            FROM @staging
            USING Extractors.Csv();
    END;