concept U - SQL script in category azure

appears as: U-SQL script, U-SQL scripts, A U-SQL script, U-SQL script, U-SQL scripts
Azure Storage, Streaming, and Batch Analytics: A guide for data engineers

This is an excerpt from Manning's book Azure Storage, Streaming, and Batch Analytics: A guide for data engineers.

With these four elements, you can build complex transformations. By combining file selectors, extractors, expressions, and outputters, you can define analysis jobs that run over small and large amounts of data. A U-SQL script is a file with a .usql extension that contains U-SQL commands. You upload the script to a new job via the Azure portal, Azure PowerShell, or another tool like Azure Data Factory. Let’s look at each element before putting them together in a U-SQL script.

Listing 7.23. U-SQL script to list players
DECLARE EXTERNAL @fileprefix string = "Player";
DECLARE @in string = String.Format(
<lineArrow/>"wasb://biometricstats@adedeveastus2/Staging/Players/{0}{1}.txt",
<lineArrow/>@fileprefix, "{*}");

@Players =
EXTRACT
    PlayerId string,
    PlayerName string,
    TeamName string,
    TeamPosition string,
    PositionStart DateTime,
    PositionEnd DateTime?
FROM @in
USING Extractors.Text(
    delimiter: '\t',
    encoding: Encoding.UTF8,
    escapeCharacter: '#',
    nullEscape: "NULL",
    quoting: true,
    silent: false,
    skipFirstNRows: 1,
    charFormat: "uint16");

@CurrentPlayers =
SELECT
    PlayerName AS Name,
    TeamName AS Team,
    TeamPosition AS Position
    FROM @Players
    WHERE PositionEnd != null
    GROUP BY PlayerName, TeamName, TeamPosition;

OUTPUT @CurrentPlayers
    TO "/Raw/Players/v1/APlayers.csv"
    USING Outputters.Text(
    delimiter: ',',
    dateTimeFormat: "o",
    encoding: Encoding.UTF8,
    escapeCharacter: '#',
    nullEscape: "NOTHING",
    quoting: true,
    rowDelimiter: "\n",
    charFormat: "uint16",
    outputHeader: true);

In the last chapter, you learned how to use Azure Data Lake Analytics (ADLA) to build reusable objects. You also used C# to enhance, and sometimes replace, the functions of SQL. In this chapter, you’ll build on that by adding features to improve your U-SQL scripts. You’ll use the Data Lake store to serve assembly files for use in ADLA jobs. You’ll run Azure PowerShell and U-SQL scripts to modify the ADLA and Data Lake environments. You’ll add new types of data extraction classes to ADLA, and add C# functions for modifying data. You’ll also connect to external providers to add even more data with minimal effort. This extensibility is facilitated by the compiled nature of ADLA jobs.