Spatial Search with Amazon Redshift and DBeaver
Tags: aws, redshift, dbeaver, qgis
Published: 2021-12-03 13:07:19

![img](https://day-journal.com/memo/images/try-071_00.png)
<br>

### I tried a spatial search with Amazon Redshift and DBeaver :tada:  

This time, I used [DBeaver](https://dbeaver.io) to connect to [Amazon Redshift](https://aws.amazon.com/jp/redshift), a cloud data warehouse provided by AWS, and tried to import location data and execute spatial functions!

<br>

## Advance Preparation of Location Data
In advance, I used QGIS to prepare random points (1 million in blue) and polygons for spatial search (three in yellow) in shapefile format, a standard supported by Amazon Redshift.

- [QGIS #065 - Create random points in a specified area](https://day-journal.com/memo/qgis-065)

![img](https://day-journal.com/memo/images/try-071_04.png)

Also, upload the data set to S3 for importing into Amazon Redshift.
![img](https://day-journal.com/memo/images/try-071_05.png)

<br>

## Creating Amazon Redshift Clusters
To begin, we will create an Amazon Redshift cluster.

<br>

Select Amazon Redshift in the AWS console → Clusters → Click Create Cluster.
![img](https://day-journal.com/memo/images/try-071_01.png)

Set the cluster name, free trial, user name, and password → Click Create Cluster.
![img](https://day-journal.com/memo/images/try-071_02.png)

After a while, the cluster will be created.
![img](https://day-journal.com/memo/images/try-071_03.png)

This completes the creation of the Amazon Redshift cluster :thumbsup: 

<br>

## Configuring Roles and Public Access
Next, we will configure the role settings for accessing S3 from Amazon Redshift and the public access settings for connecting to Amazon Redshift from DBeaver.

<br>

Create a role to access S3.
![img](https://day-journal.com/memo/images/try-071_06.png)

Assign the role you created in the cluster details screen of Amazon Redshift.
![img](https://day-journal.com/memo/images/try-071_07.png)

Click Actions → Click "Change Public Accessible Settings" on the Amazon Redshift cluster details screen.
![img](https://day-journal.com/memo/images/try-071_08.png)

Enable Publicly Accessible → Click Save Changes.
![img](https://day-journal.com/memo/images/try-071_09.png)

Verify that it has been enabled.
![img](https://day-journal.com/memo/images/try-071_10.png)

On the Amazon Redshift cluster details screen, click Security Groups → Add Inbound Rule and set the type to Redshift → Click Save Rule.
![img](https://day-journal.com/memo/images/try-071_11.png)

Verify that the inbound rule is configured.
![img](https://day-journal.com/memo/images/try-071_12.png)

This will complete the configuration of roles and public access :thumbsup: 

<br>

## Importing Location Data with DBeaver
Next, we will use [DBeaver](https://dbeaver.io) to connect to Amazon Redshift and import location data.

<br>

Connect to Amazon Redshift with DBeaver. Set the hostname (cluster endpoint), port, database name, user name, password, and role name.
![img](https://day-journal.com/memo/images/try-071_13.png)

Let's import a random point (1 million points). Create a table in advance, specify the role and destination of the shapefile to be imported, and execute.

```sql
CREATE TABLE points (
   wkb_geometry GEOMETRY,
   id BIGINT
  );

COPY points FROM 's3://redshift-geo-data/points.shp'
FORMAT SHAPEFILE
CREDENTIALS 'aws_iam_role=arn:aws:iam::xxxxxxx:role/redshift-geo-role-2112';
```

![画像](https://day-journal.com/memo/images/try-071_14.png)

Once the data is imported, you can check the information while visualizing it on the map.
![画像](https://day-journal.com/memo/images/try-071_15.png)

Let's try importing the three polygons for spatial search. Create a table in advance, specify the destination and role of the shapefile to be imported, and execute.

```sql
CREATE TABLE polygon (
   wkb_geometry GEOMETRY,
   id BIGINT
  );

COPY polygon FROM 's3://redshift-geo-data/polygon.shp'
FORMAT SHAPEFILE
CREDENTIALS 'aws_iam_role=arn:aws:iam::xxxxxxx:role/redshift-geo-role-2112';
```

![画像](https://day-journal.com/memo/images/try-071_16.png)

Once the data is imported, you can check the information while visualizing it on the map.
![画像](https://day-journal.com/memo/images/try-071_17.png)

This completes the import of location data in DBeaver :thumbsup: 

<br>

## Running a Spatial Function
Finally, we will try to see if we can perform [spatial functions](https://docs.aws.amazon.com/ja_jp/redshift/latest/dg/geospatial-functions.html) in Amazon Redshift. As a typical example, we will use "ST_AsGeoJSON" to convert data into GeoJSON format and "ST_Within" to execute a spatial function that extracts points within a polygon from 1 million points.

<br>

First, let's use "ST_AsGeoJSON" to convert the data to GeoJSON format.

```sql
SELECT ST_AsGeoJSON(wkb_geometry) FROM public.polygon;
```

The data is now output in GeoJSON format!
![img](https://day-journal.com/memo/images/try-071_18.png)

Next, try extracting the points inside the polygon using "ST_Within." 

```sql
SELECT public.points.id, public.points.wkb_geometry FROM public.points, public.polygon WHERE ST_Within(public.points.wkb_geometry, public.polygon.wkb_geometry);
```

The data was output only inside the polygon!
![img](https://day-journal.com/memo/images/try-071_19.png)

Next, use "ST_Within" to extract the count of points within the polygon.

```sql
SELECT COUNT(*) FROM public.points, public.polygon WHERE ST_Within(public.points.wkb_geometry, public.polygon.wkb_geometry);
```

 The count “5167 points” only in the polygon was output!
![img](https://day-journal.com/memo/images/try-071_20.png)

Finally, I would like to check if the spatial search results match those using [QGIS](https://qgis.org/ja/site).

- [QGIS #076 - Data selection in spatial search](https://day-journal.com/memo/qgis-076)

We got the same "5167 points"!
![img](https://day-journal.com/memo/images/try-071_21.png)

<br>

#### We were able to perform a spatial search with Amazon Redshift and DBeaver :thumbsup: 

Using Amazon Redshift and DBeaver, we confirmed that we could import location data and perform a spatial search with Amazon Redshift. It looks like it can be used successfully for location data analysis in the future.

Initially, I’ve tried to use Query Editor V2, which is available in the service, but it hasn’t seemed to support location data import or spatial functions at present. If it is supported in the future, it may become easier to use, or it may become easier to use with [Amazon Redshift Serverless](https://aws.amazon.com/jp/about-aws/whats-new/2021/11/amazon-redshift-serverless), whose preview version was announced this week :thumbsup: