![]() |
VOOZH | about |
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.
CData provides the easiest way to access and integrate live data from Amazon Athena. Customers use CData connectivity to:
Users frequently integrate Athena with analytics tools like Tableau, Power BI, and Excel for in-depth analytics from their preferred tools.
To learn more about unique Amazon Athena use cases with CData, check out our blog post: https://www.cdata.com/blog/amazon-athena-use-cases.
After obtaining the needed connection properties, accessing Amazon Athena data in PowerShell and preparing for replication consists of four basic steps.
To authorize Amazon Athena requests, provide the credentials for an administrator account or for an IAM user with custom permissions: Set to the access key Id. Set 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.
To obtain the credentials for an IAM user, follow the steps below:
To obtain the credentials for your AWS root account, follow the steps below:
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 to true and leave and empty. The CData Data Provider for Amazon Athena 2018 will automatically obtain your IAM Role credentials and authenticate with them.
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 . 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 and of an IAM user to assume the role for. Roles may not be used when specifying the and of an AWS root user.
For users and roles that require Multi-factor Authentication, specify the and 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 (default 3600 seconds).
In addition to the and properties, specify , and . Set to the region where your Amazon Athena data is hosted. Set to a folder in S3 where you would like to store the results of queries.
If is not set in the connection, the data provider connects to the default database set in Amazon Athena.
Install the module:
Install-Module AmazonAthenaCmdlets
Connect to Amazon Athena:
$amazonathena = Connect-AmazonAthena -AWSAccessKey $AWSAccessKey -AWSSecretKey $AWSSecretKey -AWSRegion $AWSRegion -Database $Database -S3StagingDirectory $S3StagingDirectory
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'}
Save a list of the column names from the returned data.
$columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name
With the data and column names collected, you are ready to replicate the data into a MySQL database.
Install the module:
Install-Module MySQLCmdlets
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
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.
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')}
Download a free trial of the Amazon Athena Cmdlets to get started:
Download NowLearn more:
👁 Amazon Athena IconAn 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.