Ready to get started?

Download a free trial of the Azure Data Lake Storage Cmdlets to get started:

 Download Now

Learn more:

Azure Data Lake Storage Icon Azure Data Lake Storage Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Azure Data Lake Storage. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

PowerShell Scripting to Replicate Azure Data Lake Storage Data to MySQL



Write a simple PowerShell script to replicate Azure Data Lake Storage data to a MySQL database.

The CData Cmdlets for Azure Data Lake Storage offer live access to Azure Data Lake Storage data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for Azure Data Lake Storage and the CData Cmdlets for MySQL in PowerShell to replicate Azure Data Lake Storage data to a MySQL database.

After obtaining the needed connection properties, accessing Azure Data Lake Storage data in PowerShell and preparing for replication consists of four basic steps.

Authenticating to a Gen 1 DataLakeStore Account

Gen 1 uses OAuth 2.0 in Azure AD for authentication.

For this, an Active Directory web application is required. You can create one as follows:

  1. Sign in to your Azure Account through the .
  2. Select "Azure Active Directory".
  3. Select "App registrations".
  4. Select "New application registration".
  5. Provide a name and URL for the application. Select Web app for the type of application you want to create.
  6. Select "Required permissions" and change the required permissions for this app. At a minimum, "Azure Data Lake" and "Windows Azure Service Management API" are required.
  7. Select "Key" and generate a new key. Add a description, a duration, and take note of the generated key. You won't be able to see it again.

To authenticate against a Gen 1 DataLakeStore account, the following properties are required:

  • Schema: Set this to ADLSGen1.
  • Account: Set this to the name of the account.
  • OAuthClientId: Set this to the application Id of the app you created.
  • OAuthClientSecret: Set this to the key generated for the app you created.
  • TenantId: Set this to the tenant Id. See the property for more information on how to acquire this.
  • Directory: Set this to the path which will be used to store the replicated file. If not specified, the root directory will be used.

Authenticating to a Gen 2 DataLakeStore Account

To authenticate against a Gen 2 DataLakeStore account, the following properties are required:

  • Schema: Set this to ADLSGen2.
  • Account: Set this to the name of the account.
  • FileSystem: Set this to the file system which will be used for this account.
  • AccessKey: Set this to the access key which will be used to authenticate the calls to the API. See the property for more information on how to acquire this.
  • Directory: Set this to the path which will be used to store the replicated file. If not specified, the root directory will be used.

Collecting Azure Data Lake Storage Data

  1. Install the module:

    Install-Module ADLSCmdlets
  2. Connect to Azure Data Lake Storage:

    $adls = Connect-ADLS -Schema $Schema -Account $Account -FileSystem $FileSystem -AccessKey $AccessKey
  3. Retrieve the data from a specific resource:

    $data = Select-ADLS -Connection $adls -Table "Resources"

    You can also use the Invoke-ADLS cmdlet to execute pure SQL-92 statements:

    $data = Invoke-ADLS -Connection $adls -Query 'SELECT * FROM Resources WHERE Type = @Type' -Params @{'@Type'='FILE'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting Azure Data Lake Storage Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the Azure Data Lake Storage data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the Azure Data Lake Storage resource (Resources) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Resources" -Columns $columns -Values $values }

You have now replicated your Azure Data Lake Storage data to a MySQL database. This gives you freedom to work with Azure Data Lake Storage data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to Azure Data Lake Storage and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-ADLS -Connection $adls -Table "Resources" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Resources" -Columns $columns -Values $values }
  • If you wish to replicate the Azure Data Lake Storage data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-ADLS cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}