|
| 1 | +## Basic Usage |
| 2 | + |
| 3 | +This example assumes a working AWS account and an S3 storage. |
| 4 | +It will execute a simple SQL statement on some data (located on S3) and |
| 5 | +write the results to another S3 bucket. An Athena database must also configured. |
| 6 | + |
| 7 | +This example uses the data from a MATLAB example file that can be found here (execute the following code in MATLAB): |
| 8 | +```matlab |
| 9 | +which airlinesmall.csv |
| 10 | +``` |
| 11 | + |
| 12 | +### Database setup |
| 13 | +To prepare for the demo, copy this data to a bucket on S3, and then |
| 14 | +create an Athena database from this data. In this example, it's called |
| 15 | +`MyAirlines.airlines`. |
| 16 | +Refer to the [AWS Athena](https://aws.amazon.com/athena/) pages for how to setup an Athena database. |
| 17 | +To facilitate creating this database, it can be helpful to look at the |
| 18 | +information from the CSV file providing the data. |
| 19 | +```matlab |
| 20 | +ds = datastore('airlinesmall.csv'); |
| 21 | +dbt=cellfun(@fmtToDBType, ds.TextscanFormats, 'Uni', 0); |
| 22 | +names = ds.VariableNames; |
| 23 | +both = [names;dbt]; |
| 24 | +disp(sprintf('%s %s, ', both{:})) |
| 25 | +``` |
| 26 | +Use the above output for defining the types and columns of the Athena |
| 27 | +database (*the `fmtToDBType`* file is present in the `Examples` directory). |
| 28 | + |
| 29 | +Lastly, a bucketfor storing the results is needed, e.g. |
| 30 | +``` |
| 31 | +s3://testingathena/outputs/ |
| 32 | +``` |
| 33 | +### Authentication |
| 34 | +If *AWS CLI* is available on machine running MATLAB, there will probably be a file like `~/.aws/credentials` on the machine. |
| 35 | +If the credentials there are valid, it should be possible to start off without any issues. If not, there are other ways to |
| 36 | +authenticate (see [Authentication](Authentication.md)). |
| 37 | + |
| 38 | + |
| 39 | +### Running the code |
| 40 | +Setup variables |
| 41 | +```matlab |
| 42 | +dbName = 'MyAirlines.airlines'; |
| 43 | +resultBucket = 's3://testingathena/outputs/'; |
| 44 | +distLimit = 1000; |
| 45 | +``` |
| 46 | +Connect to the client |
| 47 | +```matlab |
| 48 | +ath = aws.athena.AthenaClient(); |
| 49 | +ath.Database = dbName; |
| 50 | +ath.initialize |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +Create and execute a query |
| 55 | +```matlab |
| 56 | +queryFar = sprintf('SELECT UniqueCarrier, distance FROM %s WHERE distance > %d;', ... |
| 57 | + dbName, distLimit); |
| 58 | +resultIDFar = ath.submitQuery(queryFar, resultBucket); |
| 59 | +``` |
| 60 | +This function will return quickly, with a result string like *94079584-26b3-4caa-92cc-91fa94291bd4*, but the request may still be running. |
| 61 | +The status of a running request can be checked like this: |
| 62 | +```matlab |
| 63 | +status = char(ath.getStatusOfQuery(resultIDFar)); |
| 64 | +``` |
| 65 | +which will show the current state of the query (**SUCCEEDED**, **RUNNING**, etc.). |
| 66 | +When the query has succeeded, the resulting files can be found in S3, but these |
| 67 | +files can also be retrieved directly from MATLAB. The result will have the name |
| 68 | +```matlab |
| 69 | +resFile = sprintf('%s/%s.csv', resultBucket, char(resultIDFar)) |
| 70 | +``` |
| 71 | +``` |
| 72 | +resFile = |
| 73 | + 's3://testingathena/outputs/94079584-26b3-4caa-92cc-91fa94291bd4.csv' |
| 74 | +``` |
| 75 | +This file can be read using a datastore. The datastore, however, |
| 76 | +will rely on having the AWS keys available in environment variables, so first |
| 77 | +do something like this: |
| 78 | +```matlab |
| 79 | +setenv('AWS_REGION', 'eu-central-1') |
| 80 | +setenv('AWS_ACCESS_KEY_ID', 'A<RETRACTED>Z') |
| 81 | +setenv('AWS_SECRET_ACCESS_KEY', 'B<RETRACTED>X') |
| 82 | +``` |
| 83 | +The MATLAB documentation for how to *"Work with remote data"* describes this in more detail. |
| 84 | + |
| 85 | +After this,the data can be read from the datastore. |
| 86 | +```matlab |
| 87 | +ds = datastore(resFile); |
| 88 | +ds.NumHeaderLines = 1; |
| 89 | +farResult = ds.readall(); |
| 90 | +``` |
| 91 | + |
| 92 | +### Athena limits |
| 93 | +There are [limitations](https://docs.aws.amazon.com/athena/latest/ug/service-limits.html) to how many queries can be run in Athena. |
| 94 | +If the limit is exceeded, |
| 95 | +the submitted query will fail with a message similar to this one. |
| 96 | + |
| 97 | + Problems executing Athena query: |
| 98 | + com.amazonaws.services.athena.model.AmazonAthenaException: |
| 99 | + Rate exceeded (Service: AmazonAthena; Status Code: 400; |
| 100 | + Error Code: ThrottlingException; |
| 101 | + Request ID: 5740d70a-e53d-4cb4-9c40-695cf31d828c) |
| 102 | + |
| 103 | +This must be handled by the application. |
0 commit comments