Ready to get started?

Download a free trial of the Amazon Athena Cmdlets to get started:

 Download Now

Learn more:

Amazon Athena Icon Amazon Athena Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Amazon Athena. The Cmdlets allow users to easily query live data - just like working with SQL server.

PowerShell Scripting to Replicate Amazon Athena Data to MySQL



Write a simple PowerShell script to replicate Amazon Athena data to a MySQL database.

The CData Cmdlets for Amazon Athena offer live access to Amazon Athena 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 Amazon Athena and the CData Cmdlets for MySQL in PowerShell to replicate Amazon Athena data to a MySQL database.

After obtaining the needed connection properties, accessing Amazon Athena data in PowerShell and preparing for replication consists of four basic steps.

Authenticating to Amazon Athena

To authorize Amazon Athena requests, provide the credentials for an administrator account or for an IAM user with custom permissions: Set AccessKey to the access key Id. Set SecretKey to the secret access key.

Note: Though you can connect as the AWS account administrator, it is recommended to use IAM user credentials to access AWS services.

Obtaining the Access Key

To obtain the credentials for an IAM user, follow the steps below:

  1. Sign into the IAM console.
  2. In the navigation pane, select Users.
  3. To create or manage the access keys for a user, select the user and then select the Security Credentials tab.

To obtain the credentials for your AWS root account, follow the steps below:

  1. Sign into the AWS Management console with the credentials for your root account.
  2. Select your account name or number and select My Security Credentials in the menu that is displayed.
  3. Click Continue to Security Credentials and expand the Access Keys section to manage or create root account access keys.

Authenticating from an EC2 Instance

If you are using the CData Data Provider for Amazon Athena 2018 from an EC2 Instance and have an IAM Role assigned to the instance, you can use the IAM Role to authenticate. To do so, set UseEC2Roles to true and leave AccessKey and SecretKey empty. The CData Data Provider for Amazon Athena 2018 will automatically obtain your IAM Role credentials and authenticate with them.

Authenticating as an AWS Role

In many situations it may be preferable to use an IAM role for authentication instead of the direct security credentials of an AWS root user. An AWS role may be used instead by specifying the RoleARN. This will cause the CData Data Provider for Amazon Athena 2018 to attempt to retrieve credentials for the specified role. If you are connecting to AWS (instead of already being connected such as on an EC2 instance), you must additionally specify the AccessKey and SecretKey of an IAM user to assume the role for. Roles may not be used when specifying the AccessKey and SecretKey of an AWS root user.

Authenticating with MFA

For users and roles that require Multi-factor Authentication, specify the MFASerialNumber and MFAToken connection properties. This will cause the CData Data Provider for Amazon Athena 2018 to submit the MFA credentials in a request to retrieve temporary authentication credentials. Note that the duration of the temporary credentials may be controlled via the TemporaryTokenDuration (default 3600 seconds).

Connecting to Amazon Athena

In addition to the AccessKey and SecretKey properties, specify Database, S3StagingDirectory and Region. Set Region to the region where your Amazon Athena data is hosted. Set S3StagingDirectory to a folder in S3 where you would like to store the results of queries.

If Database is not set in the connection, the data provider connects to the default database set in Amazon Athena.

Collecting Amazon Athena Data

  1. Install the module:

    Install-Module AmazonAthenaCmdlets
  2. Connect to Amazon Athena:

    $amazonathena = Connect-AmazonAthena -AccessKey $AccessKey -SecretKey $SecretKey -Region $Region -Database $Database -S3StagingDirectory $S3StagingDirectory
  3. Retrieve the data from a specific resource:

    $data = Select-AmazonAthena -Connection $amazonathena -Table "Customers"

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

    $data = Invoke-AmazonAthena -Connection $amazonathena -Query 'SELECT * FROM Customers WHERE CustomerId = @CustomerId' -Params @{'@CustomerId'='12345'}
  4. Save a list of the column names from the returned data.

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

Inserting Amazon Athena 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 Amazon Athena 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 Amazon Athena resource (Customers) and to exist in the database.

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

You have now replicated your Amazon Athena data to a MySQL database. This gives you freedom to work with Amazon Athena 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 Amazon Athena and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-AmazonAthena -Connection $amazonathena -Table "Customers" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Customers" -Columns $columns -Values $values }
  • If you wish to replicate the Amazon Athena 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-AmazonAthena 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')}