Hands-on Apache Superset, Amazon S3, and Amazon Athena

## What is Apache Superset?
"Apache Superset (incubating) is a modern, enterprise-ready business intelligence web application". (Apache Software Foundation)

Some other equivalents you might've heard of would be Tableau or PowerBI, but they're all business licensed software.

## What about Amazon S3 and Athena?
S3 : "Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. " (Amazon Web Service)

Athena : "Amazon Athena is an interactive query service that makes it easy to analyze data in Amazon S3 using standard SQL. Athena is serverless, so there is no infrastructure to manage, and you pay only for the queries that you run." (Amazon Web Service)

## What You'll Need Beforehand

1. An AWS account (and cash duh).
1. AWS credentials set.
1. An Ubuntu 18.04+ environment.
1. Mapbox account.
1. `pip` installed.


## Installation
### PyAthena
- Apache Superset needs an API interface to interact with AWS Athena.

```
pip install "PyAthena>1.2.0"
```

### Apache Superset
- Install superset

```
pip install apache-superset
```

-  Initialize the database
 
```
superset db upgrade
```

- Create an admin user (you will be prompted to set a username, first and last name before setting a password)


``` 
export FLASK_APP=superset
```

```
superset fab create-admin
```

- Load some data to play with

```
superset load_examples
```

- Create default roles and permissions

```
superset init
```



## Workflow
- To start a development web server on port 8088, use -p to bind to another port

```
superset run -p 8088 --with-threads --reload --debugger
```

- Switch to your browser and go to http://127.0.0.1:8088/, you should now see something resembling the following

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/b7bf2a42-6ff0-4acb-df0d-ce195b9a2aec.png)

- Login with the admin account you have just created. You'll see some examples have been loaded if you followed the tutorial. Play with them if you want to, but we'll be using some other data for demonstrative purposes.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/0a44951b-63e5-8486-23e2-000bdce455e1.png)

- Throw some data into an AWS S3 bucket to process with. [This airbnb data from kaggle](https://www.kaggle.com/dgomonov/new-york-city-airbnb-open-data) is what I'll be using.

```
aws s3 cp ~PATH/TO/AB_NYC_2019.csv s3://YOUR-BUCKET
```

- Now, come back to your Apache Superset's UI and add the click on `Databases`, then the `+` button on the top right hand corner.
- ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/9f5858df-10f3-2510-9cd6-7ea242b992d5.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/a77f76f1-4808-f5ab-69df-10386dade005.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/0f945329-6544-e31c-7f9f-c2b8b79c4414.png)

- Modify and add the following text to `SQL Alchemy URI`.

```
awsathena+rest://{aws_access_key_id}:{aws_secret_access_key}@athena.{region_name}.amazonaws.com/{schema_name}?s3_staging_dir={s3_staging_dir}
```

- Log into AWS Athena's interface and define the columns you need for your database. I won't be using all the columns for simplicity.(Make sure that the region of your AWS Athena and the S3 bucket you made is the same) If you're familiar enough with AWS Athena, you can execute the exact query on Apache Superset's UI.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/6b80299c-6b14-58f8-5a6b-d8d65690138c.png)


- Going back to your Apache Superset UI, you should see the following

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/37c93667-c507-ab32-3679-2477ba7e7dbc.png)

- Run a query of your preference and click on `Explore`.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/ecd1446f-7c82-f93f-5da6-f97ac63db59e.png)

- Running a `deck.gl` visualization gives us ...

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/969fa178-4e36-990b-7dd6-524bffff5f06.png)

- Oops, seems like we need a map token from [MapBox](https://www.mapbox.com/). Register an account and export your token as an environment variable to `MAPBOX_API_KEY`. [See the official documentation](https://superset.incubator.apache.org/faq.html#why-is-the-map-not-visible-in-the-mapbox-visualization)

```
export MAPBOX_API_KEY=your-token
```

- Restart your server and you should now see ... 

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/9563f7a0-9219-4338-dd99-e439082f5c2d.png)

Darker grids are where the average Airbnb price are higher.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/503314/da2e576a-98fc-c250-7ff8-2b39958b935a.png)

Here's a place where some of the more expensive Airbnb's rooms are clustered, and the reasons might be apparent.

## Conclusions
There are a lot left to talk about with Apache Superset, AWS S3, and AWS Athena, but the general idea here is to demonstrate a data analysis workflow combining various tools. Indeed, one can achieve this without using any of the above, for instance, with the combination of Tableau and Google Bigquery.

## Reference
1. Apache Software Foundation, "Apache Superset (incubating)", Apache Software Foundation. https://superset.incubator.apache.org/#apache-superset-incubating. 22 August 2020.
2. Amazon Web Service, "Amazon S3", Amazon Web Service. https://aws.amazon.com/s3/. 22 August 2020.
3. Amazon Web Service, "Amazon Athena", Amazon Web Service. https://aws.amazon.com/athena/. 22 August 2020.