Ready to get started?

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

 Download Now

Learn more:

Amazon Athena Icon Amazon Athena ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Amazon Athena.

DataBind Charts to Amazon Athena Data



Use the standard ADO.NET procedures for databinding to provide bidirectional access to Amazon Athena data from controls in the Visual Studio toolbox. This article demonstrates a graphical approach using wizards in Visual Studio, as well as how to databind with only a few lines of code.

DataBinding facilitates two-way interaction with data through UI controls. Using the CData ADO.NET Provider for Amazon Athena streamlines the process of binding Amazon Athena data to Windows Forms and Web controls within Visual Studio. In this article, we will demonstrate using wizards to establish a binding between Amazon Athena data and a chart that dynamically updates. Additionally, the code walk-through section will guide you through the creation of a chart using just 10 lines of code.

DataBind to a Chart

DataBinding consists of three steps: Instantiate the control, configure the data source, and databind.

Configure the Connection and Select Database Objects

To create a chart control and establish a connection to Amazon Athena, follow the steps outlined below using the Data Source Configuration Wizard. Within the wizard, you'll have the option to choose the specific Amazon Athena entities you wish to bind to.

  1. In a Windows Forms project, drag and drop a Chart control from the toolbox to the form. In the Data section of the Chart properties, select DataSource and then select Add Project Data Source from the menu.
  2. In the Data Source Configuration Wizard that appears, select Database -> Dataset.
  3. In the Choose Your Data Connection step, click New Connection.
  4. In the Add Connection dialog, click Change to select the CData Amazon Athena Data Source.

    Below is a typical connection string:

    AccessKey='a123';SecretKey='s123';Region='IRELAND';Database='sampledb';S3StagingDirectory='s3://bucket/staging/';

    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.

    When you configure the connection, you may also want to set the Max Rows connection property. This will limit the number of rows returned, which is especially helpful for improving performance when designing reports and visualizations.

  5. Choose the database objects you want to work with. This example uses the Customers table.

DataBind

After adding the data source and selecting database objects, you can bind the objects to the chart. This example assigns the x-axis to Name and the y-axis to TotalDue.

  1. In the Chart properties, click the button in the Series property to open the Series Collection Editor.
  2. In the Series properties, select the columns you want for the x- and y-axes: Select columns from the menu in the XValueMember and YValueMember properties.

The chart is now databound to the Amazon Athena data. Run the chart to display the current data.

Code Walk-through

DataBinding to Amazon Athena data requires only a few lines of code and can be completed in three easy steps.

  1. Connect to Amazon Athena.
  2. Create the AmazonAthenaDataAdapter to execute the query and create a DataSet to be filled with its results.
  3. DataBind the result set to the chart.

Below is the complete code:

AmazonAthenaConnection conn = new AmazonAthenaConnection("AccessKey='a123';SecretKey='s123';Region='IRELAND';Database='sampledb';S3StagingDirectory='s3://bucket/staging/';"); AmazonAthenaCommand comm = new AmazonAthenaCommand("SELECT Name, TotalDue FROM Customers", conn); AmazonAthenaDataAdapter da = new AmazonAthenaDataAdapter(comm); DataSet dataset = new DataSet(); da.Fill(dataset); chart1.DataSource = dataset; chart1.Series[0].XValueMember = "Name"; chart1.Series[0].YValueMembers = "TotalDue"; // Insert code for additional chart formatting here. chart1.DataBind();