Spatial Search of Amazon S3 Express One Zone Data with Amazon Athena and Visualized It in QGIS
Tags: athena, s3, qgis, aws
Published: 2023-12-18 12:08:05

![img](https://memo.dayjournal.dev/images/try-106_27.jpg)
![img](https://memo.dayjournal.dev/images/try-106_00.png)

### I tried a spatial search of Amazon S3 Express One Zone data with Amazon Athena and visualized it with QGIS :tada:  

I previously posted an [article](https://dev.to/aws-builders/spatial-search-of-amazon-s3-data-with-amazon-athena-and-visualized-it-in-qgis-2b9a) verified with S3 Standard. This time, I verified it with the new [S3 Express One Zone](https://aws.amazon.com/s3/storage-classes/express-one-zone) announced at re:Invent 2023, focusing on the results of integrating with Athena, spatial search, and improving search speed when using S3 Express One Zone!

S3 Express One Zone is an Amazon S3 storage option focusing on high performance. This option is also available in the Tokyo Region and is designed to provide up to 10 times better performance than the S3 Standard storage class. In addition, the request fee is 50% less than S3 Standard. To use this service, a specific bucket type called "Directory Bucket" is used.

<br>

## Advance Preparation

Prepare GIS data for use with [Amazon Athena](https://aws.amazon.com/athena). This time, we created four types of sample data in [QGIS](https://qgis.org) in advance.

I prepared GIS data for points, lines, and polygons in CSV (TSV format).
![img](https://memo.dayjournal.dev/images/try-089_01.png)

I prepared an additional 1 million points of GIS data in CSV (TSV format).
![img](https://memo.dayjournal.dev/images/try-089_02.png)

I have registered this sample data on GitHub, so please feel free to use it.
https://github.com/dayjournal/data/tree/main/try-106

<br>

## Bucket Creation & Data Registration (S3 Express One Zone)

Create buckets and register data with Amazon S3 Express One Zone.

Click AWS Management Console → S3.
![img](https://memo.dayjournal.dev/images/amazon-s3-008_01_en.png)

Click "Create Bucket".
![img](https://memo.dayjournal.dev/images/amazon-s3-008_02_en.png)

Set Region Bucket Type as Directory, Availability Zone, and Base Name.
![img](https://memo.dayjournal.dev/images/amazon-s3-008_03_en.png)

A bucket with the specified name is created.
![img](https://memo.dayjournal.dev/images/amazon-s3-008_04_en.png)

Select the target bucket → Click "Upload."
![img](https://memo.dayjournal.dev/images/try-106_01_en.png)

Select the file you want to register → Click "Upload.
![img](https://memo.dayjournal.dev/images/try-106_02_en.png)

Check the uploaded files.
![img](https://memo.dayjournal.dev/images/try-106_03_en.png)

The four types of CSV (TSV format) were saved in a directory bucket with an arbitrary name.
![img](https://memo.dayjournal.dev/images/try-106_25_en.png)

This completes the data registration for S3 Express One Zone!

<br>

## Set the query destination

This is how to set the query destination in Amazon Athena.

Prepare an S3 bucket with an arbitrary name for the query destination in advance.
![img](https://memo.dayjournal.dev/images/try-106_26_en.png)

Click AWS Management Console → Athena.
![img](https://memo.dayjournal.dev/images/try-106_04_en.png)

Click on "Check query editor for details."
![img](https://memo.dayjournal.dev/images/try-106_05_en.png)

Click on "View Settings."
![img](https://memo.dayjournal.dev/images/try-106_06_en.png)

Click "Manage."
![img](https://memo.dayjournal.dev/images/try-106_07_en.png)

Specify the S3 bucket where you want to save the query → Click "Save."
![img](https://memo.dayjournal.dev/images/try-106_08_en.png)

The query destination is set.
![img](https://memo.dayjournal.dev/images/try-106_09_en.png)

The setting of the query destination is now complete!

<br>

## Table Creation

This is how to create a table in Amazon Athena. 

Click the Athena editor → Create Table and View → "S3 Bucket Data."
![img](https://memo.dayjournal.dev/images/try-106_10_en.png)

Set table name, database selection, target S3 bucket specification, data format, and column settings. Check the preview → Click "Create Table."

S3 Express One Zone buckets are currently not displayed in the list, so you must enter the address directly. The address should be prefixed with "s3://".
![img](https://memo.dayjournal.dev/images/try-106_11_en.png)

This time we created four arbitrary tables. Target table → Click "Preview Table."
![img](https://memo.dayjournal.dev/images/try-106_12_en.png)

The retrieved records are displayed.
![img](https://memo.dayjournal.dev/images/try-106_13_en.png)

Now your table creation is complete! We have confirmed that the table can be read by Athena with no problem in S3 Express One Zone.

<br>

## Spatial Search

Finally, here is how to do a [spatial search](https://docs.aws.amazon.com/athena/latest/ug/geospatial-functions-list-v2.html) in Amazon Athena.

Let's get the center of gravity point from a polygon. Download the result data.

**S3 Standard**
Time in queue: 0.243 sec, Run time: 0.799 sec, Data scanned: 1.5KB

**S3 Express One Zone**
Time in queue: 0.120 sec, Run time: 0.899 sec, Data scanned: 1.5KB

```sql
SELECT "geospatial_database"."polygon_table"."name", ST_Centroid(ST_GeometryFromText("geospatial_database"."polygon_table"."wkt")) FROM "geospatial_database"."polygon_table";
```

![img](https://memo.dayjournal.dev/images/try-106_15_en.png)

Visualize the downloaded data in QGIS to confirm the processed data.
![img](https://memo.dayjournal.dev/images/try-106_20.png)

<br>

Try to get the starting point from the line. Download the result data.

**S3 Standard**
Time in queue: 0.175 sec, Run time: 0.601 sec, Data scanned: 1.05KB

**S3 Express One Zone**
Time in queue: 0.119 sec, Run time: 0.948 sec, Data scanned: 1.05KB

```sql
SELECT "geospatial_database"."line_table"."name", ST_StartPoint(ST_GeometryFromText("geospatial_database"."line_table"."wkt")) FROM "geospatial_database"."line_table";
```

![img](https://memo.dayjournal.dev/images/try-106_14_en.png)

Visualize the downloaded data in QGIS to confirm the processed data.
![img](https://memo.dayjournal.dev/images/try-106_21.png)

<br>

Try to get only the points included in the polygon. Download the result data.

**S3 Standard**
Time in queue: 0.313 sec, Run time: 1.230 sec, Data scanned: 2.01KB

**S3 Express One Zone**
Time in queue: 0.073 sec, Run time: 0.993 sec, Data scanned: 2.01KB

```sql
SELECT "geospatial_database"."point_table"."name", "geospatial_database"."point_table"."wkt" FROM "geospatial_database"."point_table", "geospatial_database"."polygon_table" WHERE ST_Within(ST_GeometryFromText("geospatial_database"."point_table"."wkt"), ST_GeometryFromText("geospatial_database"."polygon_table"."wkt"));
```

![img](https://memo.dayjournal.dev/images/try-106_16_en.png)

Visualize the downloaded data in QGIS to confirm the processed data.
![img](https://memo.dayjournal.dev/images/try-106_22.png)

<br>

Try to get only the points included in 1 million polygons. The response time is fast even when searching a large amount of GIS data. Download the result data.

**S3 Standard**
Time in queue: 0.220 sec, Run time: 2.832 sec, Data scanned: 46.41MB

**S3 Express One Zone**
Time in queue: 0.117 sec, Run time: 2.843 sec, Data scanned: 46.41MB

```sql
SELECT "geospatial_database"."randompoint_table"."name", "geospatial_database"."randompoint_table"."wkt" FROM "geospatial_database"."randompoint_table", "geospatial_database"."polygon_table" WHERE ST_Within(ST_GeometryFromText("geospatial_database"."randompoint_table"."wkt"), ST_GeometryFromText("geospatial_database"."polygon_table"."wkt"));
```

![img](https://memo.dayjournal.dev/images/try-106_17_en.png)

Visualize the downloaded data in QGIS to confirm the processed data.
![img](https://memo.dayjournal.dev/images/try-106_23.png)
![img](https://memo.dayjournal.dev/images/try-106_24.png)

By using Amazon Athena, a spatial search of data registered in S3 becomes possible!

<br>

This verification confirmed that even when using S3 Express One Zone, it is possible to link with Athena and realize spatial search.

As for spatial search, we saw a performance improvement of more than 120% for some searches, but no significant overall speed improvement was observed. This may be because S3 Express One Zone specializes in processing many small files and may not be suitable for the large spatial search data used in this verification.

However, in terms of storage cost reduction, we saw significant advantages in using S3 Express One Zone!

<br>

Related Articles

{% link https://dev.to/mierune/trying-out-various-turfjs-547k %}

{% link https://dev.to/mierune/trying-out-various-qgis-processing-2ej1 %}

{% link https://dev.to/aws-heroes/spatial-search-with-amazon-opensearch-service-and-visualized-it-in-qgis-2m70 %}

{% link https://dev.to/aws-builders/spatial-search-with-amazon-redshift-and-dbeaver-3d2d %}

{% link https://dev.to/aws-builders/using-qgis-and-amazon-auroras-postgresql-postgis-to-display-location-data-5ecm %}

<br>

References
[Amazon Athena](https://aws.amazon.com/athena)
[Amazon S3](https://aws.amazon.com/s3)
[QGIS](http://www.qgis.org/ja/site/)