Add new partition-scheme to existing table in athena with SQL code
Asked Answered
O

1

0

Is it even possible to add a partition to an existing table in Athena that currently is without partitions? If so, please also write syntax for doing so in the answer.

For example:

ALTER TABLE table1 ADD PARTITION (ourDateStringCol = '2021-01-01')

The above command will give the following error:

FAILED: SemanticException table is not partitioned but partition spec exists

Note: I have done a web-search, and variants exist for SQL server, or adding a partition to an already partitioned table. However, I personally could not find a case where one could successfully add a partition to an existing non-partitioned table.

This is extremely similar to: SemanticException adding partiton Hive table

However, the answer given there requires re-creating the table.

I want to do so without re-creating the table.

Ottoottoman answered 26/3, 2021 at 17:35 Comment(0)
Y
1

Partitions in Athena are based on folder structure in S3. Unlike standard RDBMS that are loading the data into their disks or memory, Athena is based on scanning data in S3. This is how you enjoy the scale and low cost of the service.

What it means is that you have to have your data in different folders in a meaningful structure such as year=2019, year=2020, and make sure that the data for each year is all and only in that folder.

The simple solution is to run a CREATE TABLE AS SELECT (CTAS) query that will copy the data and create a new table that can be optimized for your analytical queries. You can choose the table format (Parquet, for example), the compression (SNAPPY, for example), and also the partition schema (per year, for example).

You answered 27/3, 2021 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.