VOOZH about

URL: https://www.cdata.com/kb/tech/athena-cloud-apps-script.rst

⇱ Connect to Amazon Athena Data in Google Apps Script via Connect AI


Connect to Amazon Athena Data in Google Apps Script via Connect AI

πŸ‘ Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Use CData Connect AI to access Amazon Athena data in Google Apps Script.

Google Apps Script empowers users to build custom functionality within their Google documents, including Google Sheets and Google Docs. Apps Script natively supports SQL Server connectivity via JDBC, providing a powerful extensibility tool for connecting Google cloud applications to external data. Paired with the SQL connectivity offered by CData Connect AI, users can easily access live Amazon Athena data directly from within their Google documents.

This article shows how to connect to Amazon Athena in Connect AI and provides sample scripting for processing Amazon Athena data in a Google Spreadsheet.

About Amazon Athena Data Integration

CData provides the easiest way to access and integrate live data from Amazon Athena. Customers use CData connectivity to:

  • Authenticate securely using a variety of methods, including IAM credentials, access keys, and Instance Profiles, catering to diverse security needs and simplifying the authentication process.
  • Streamline their setup and quickly resolve issue with detailed error messaging.
  • Enhance performance and minimize strain on client resources with server-side query execution.

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.


Getting Started


Our script only reads data from a specified table, but you can easily extend the script to incorporate update functionality.

Configure Amazon Athena Connectivity for Google Apps Scripts

Connectivity to Amazon Athena from Google Apps Scripts is made possible through CData Connect AI. To work with Amazon Athena data from Google Apps Scripts, we start by creating and configuring a Amazon Athena connection.

CData Connect AI uses a straightforward, point-and-click interface to connect to data sources.

  1. Log into Connect AI, click Sources, and then click Add Connection
  2. πŸ‘ Adding a Connection
  3. Select "Amazon Athena" from the Add Connection panel
  4. πŸ‘ Selecting a data source
  5. Enter the necessary authentication properties to connect to Amazon Athena.

    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 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.

    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 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.

    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 . 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.

    Authenticating with MFA

    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).

    Connecting to Amazon Athena

    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.

    πŸ‘ Configuring a connection (Salesforce is shown)
  6. Click Save & Test
  7. Navigate to the Permissions tab in the Add Amazon Athena Connection page and update the User-based permissions. πŸ‘ Updating permissions

Add a Personal Access Token

When connecting to Connect AI through the REST API, the OData API, or the Virtual SQL Server, a Personal Access Token (PAT) is used to authenticate the connection to Connect AI. It is best practice to create a separate PAT for each service to maintain granularity of access.

  1. Click on the Gear icon () at the top right of the Connect AI app to open the settings page.
  2. On the Settings page, go to the Access Tokens section and click Create PAT.
  3. Give the PAT a name and click Create. πŸ‘ Creating a new PAT
  4. The personal access token is only visible at creation, so be sure to copy it and store it securely for future use.

With the connection configured and a PAT generated, you are ready to connect to Amazon Athena data from Google Apps Script.

Connect to Amazon Athena Data from Apps Script

At this point, you should have configured a connection Amazon Athena in Connect AI. All that is left new is to use Google Apps Script to access Connect AI and work with your Amazon Athena data in Google Sheets.

In this section, you will create a script (with a menu option to call the script) to populate a spreadsheet with Amazon Athena data. We have created a sample script and explained the different parts. You can view the raw script at the and of the article.

1. Create an Empty Script

To create a script for your Google Sheet, click Tools Script editor from the Google Sheets menu:

πŸ‘ Open script editor

2. Declare Class Variables

Create a handful of class variables to be available for any functions created in the script.

//replace the variables in this block with real values as needed
var address = 'tds.cdata.com:14333';
var user = 'CONNECT_USER'; // [email protected]
var userPwd = 'CONNECT_USER_PAT';
var db = 'AmazonAthena1';

var dbUrl = 'jdbc:sqlserver://' + address + ';databaseName=' + db;

3. Add a Menu Option

This function adds a menu option to your Google Sheet, allowing you to use the UI to call your function.

function onOpen() {
 var spreadsheet = SpreadsheetApp.getActive();
 var menuItems = [
 {name: 'Write data to a sheet', functionName: 'connectToAmazonAthenaData'}
 ];
 spreadsheet.addMenu('Amazon Athena Data', menuItems);
} 
πŸ‘ The newly added Menu option.

4. Write a Helper Function

This function is used to find the first empty row in a spreadsheet.

/*
 * Finds the first empty row in a spreadsheet by scanning an array of columns
 * @return The row number of the first empty row.
 */
function getFirstEmptyRowByColumnArray(spreadSheet, column) {
 var column = spreadSheet.getRange(column + ":" + column);
 var values = column.getValues(); // get all data in one call
 var ct = 0;
 while ( values[ct] && values[ct][0] != "" ) {
 ct++;
 }
 return (ct+1);
}

5. Write a Function to Write Amazon Athena Data to a Spreadsheet

The function below writes the Amazon Athena data, using the Google Apps Script JDBC functionality to connect to Connect AI, SELECT data, and populate a spreadsheet. When the script is run, two input boxes will appear:

The first one asks the user to input the name of a sheet to hold the data (if the spreadsheet does not exist, the function creates it).

πŸ‘ Input box for sheet selection.

The second asks the user to input the name of a Amazon Athena table to read. If an invalid table is chosen, an error message appears and the function is exited.

πŸ‘ Input box for table selection.

Note, while the function is designed for use as a menu option, you can extend it for use as a spreadsheet formula.

/*
 * Reads data from a specified Amazon Athena 'table' and writes it to the specified sheet.
 * (If the specified sheet does not exist, it is created.)
 */
function connectToAmazonAthenaData() {
 var thisWorkbook = SpreadsheetApp.getActive();

 //select a sheet and create it if it does not exist
 var selectedSheet = Browser.inputBox('Which sheet would you like the data to post to?',Browser.Buttons.OK_CANCEL);
 if (selectedSheet == 'cancel')
 return;

 if (thisWorkbook.getSheetByName(selectedSheet) == null)
 thisWorkbook.insertSheet(selectedSheet);
 var resultSheet = thisWorkbook.getSheetByName(selectedSheet);
 var rowNum = 2;

 //select a Amazon Athena 'table'
 var table = Browser.inputBox('Which table would you like to pull data from?',Browser.Buttons.OK_CANCEL);
 if (table == 'cancel')
 return;

 var name = Jdbc.getConnection(dbUrl, {
 user: user, 
 password: userPwd
	}	
 );

 //confirm that var table is a valid table/view
 var dbMetaData = name.getMetaData();
 var tableSet = dbMetaData.getTables(null, null, table, null);
 var validTable = false;
 while (tableSet.next()) {
 var tempTable = tableSet.getString(3);
 if (table.toUpperCase() == tempTable.toUpperCase()){
 table = tempTable;
 validTable = true;
 break;
 }
 } 
 tableSet.close();
 if (!validTable) {
 Browser.msgBox("Invalid table name: " + table, Browser.Buttons.OK);
 return;
 }

 var stmt = name.createStatement();

 var results = stmt.executeQuery('SELECT * FROM ' + table);
 var rsmd = results.getMetaData();
 var numCols = rsmd.getColumnCount();

 //if the sheet is empty, populate the first row with the headers
 var firstEmptyRow = getFirstEmptyRowByColumnArray(resultSheet, "A");
 if (firstEmptyRow == 1) {
 //collect column names
 var headers = new Array(new Array(numCols));
 for (var col = 0; col < numCols; col++){
 headers[0][col] = rsmd.getColumnName(col+1);
 }
 resultSheet.getRange(1, 1, headers.length, headers[0].length).setValues(headers);
 } else {
 rowNum = firstEmptyRow;
 }

 //write rows of Amazon Athena data to the sheet
 var values = new Array(new Array(numCols));
 while (results.next()) {
 for (var col = 0; col < numCols; col++) {
 values[0][col] = results.getString(col + 1);
 }
 resultSheet.getRange(rowNum, 1, 1, numCols).setValues(values);
 rowNum++;
 }

 results.close();
 stmt.close();
}
 

When the function is completed, you have a spreadsheet populated with your Amazon Athena data, and you can now leverage all of the calculating, graphing, and charting functionality of Google Sheets anywhere you have access to the Internet.


Complete Google Apps Script

//replace the variables in this block with real values as needed
var address = 'tds.cdata.com:14333';
var user = 'CONNECT_USER'; // [email protected]
var userPwd = 'CONNECT_USER_PAT';
var db = 'AmazonAthena1';

var dbUrl = 'jdbc:sqlserver://' + address + ';databaseName=' + db;

function onOpen() {
 var spreadsheet = SpreadsheetApp.getActive();
 var menuItems = [
 {name: 'Write table data to a sheet', functionName: 'connectToAmazonAthenaData'}
 ];
 spreadsheet.addMenu('Amazon Athena Data', menuItems);
}

/*
 * Finds the first empty row in a spreadsheet by scanning an array of columns
 * @return The row number of the first empty row.
 */
function getFirstEmptyRowByColumnArray(spreadSheet, column) {
 var column = spreadSheet.getRange(column + ":" + column);
 var values = column.getValues(); // get all data in one call
 var ct = 0;
 while ( values[ct] && values[ct][0] != "" ) {
 ct++;
 }
 return (ct+1);
}

/*
 * Reads data from a specified 'table' and writes it to the specified sheet.
 * (If the specified sheet does not exist, it is created.)
 */
function connectToAmazonAthenaData() {
 var thisWorkbook = SpreadsheetApp.getActive();

 //select a sheet and create it if it does not exist
 var selectedSheet = Browser.inputBox('Which sheet would you like the data to post to?',Browser.Buttons.OK_CANCEL);
 if (selectedSheet == 'cancel')
 return;

 if (thisWorkbook.getSheetByName(selectedSheet) == null)
 thisWorkbook.insertSheet(selectedSheet);
 var resultSheet = thisWorkbook.getSheetByName(selectedSheet);
 var rowNum = 2;

 //select a Amazon Athena 'table'
 var table = Browser.inputBox('Which table would you like to pull data from?',Browser.Buttons.OK_CANCEL);
 if (table == 'cancel')
 return;

 var name = Jdbc.getConnection(dbUrl, {
 user: user, 
 password: userPwd
	}
 );

 //confirm that var table is a valid table/view
 var dbMetaData = name.getMetaData();
 var tableSet = dbMetaData.getTables(null, null, table, null);
 var validTable = false;
 while (tableSet.next()) {
 var tempTable = tableSet.getString(3);
 if (table.toUpperCase() == tempTable.toUpperCase()){
 table = tempTable;
 validTable = true;
 break;
 }
 } 
 tableSet.close();
 if (!validTable) {
 Browser.msgBox("Invalid table name: " + table, Browser.Buttons.OK);
 return;
 }

 var stmt = name.createStatement();

 var results = stmt.executeQuery('SELECT * FROM ' + table);
 var rsmd = results.getMetaData();
 var numCols = rsmd.getColumnCount();

 //if the sheet is empty, populate the first row with the headers
 var firstEmptyRow = getFirstEmptyRowByColumnArray(resultSheet, "A");
 if (firstEmptyRow == 1) {
 //collect column names
 var headers = new Array(new Array(numCols));
 for (var col = 0; col < numCols; col++){
 headers[0][col] = rsmd.getColumnName(col+1);
 }
 resultSheet.getRange(1, 1, headers.length, headers[0].length).setValues(headers);
 } else {
 rowNum = firstEmptyRow;
 }

 //write rows of Amazon Athena data to the sheet
 var values = new Array(new Array(numCols));
 while (results.next()) {
 for (var col = 0; col < numCols; col++) {
 values[0][col] = results.getString(col + 1);
 }
 resultSheet.getRange(rowNum, 1, 1, numCols).setValues(values);
 rowNum++;
 }

 results.close();
 stmt.close();
}