# Managing AWS glue and Athena tables

**URL:** <https://awshq.com/t/managing-aws-glue-and-athena-tables/11830>\
**Category:** AWS Storage Discussion\
**Tags:** s3, athena, glue\
**Created:** [October 25, 2020, 6:45pm UTC](https://awshq.com/t/managing-aws-glue-and-athena-tables/11830 "2020-10-25T18:45:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jahaz](https://awshq.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jahaz](https://awshq.com/u/jahaz)\
**Post date:** [October 25, 2020, 6:45pm UTC](https://awshq.com/t/managing-aws-glue-and-athena-tables/11830/1 "2020-10-25T18:45:54Z")

</div>

Working on getting my company into a data lake have the data pipeline setup and getting data into s3. My struggle now is aws Athena column types are giving me errors.

I initially thought each table could be made manually, but that became very cumbersome. I then started using aws glue via boto3 api to crawl the tables. Using the table schema to overwrite partition schema.

This worked well but some of the columns are coming out as doubles instead of integers. Making it impossible to join.

Glue catalog seems like a simple fix but when I change the column to string to fix it now the partitions schema are wrong…

Am I Crazy? This seems like a huge mess.

Anyone worked on this before? Is there any solution to this? or do people just use other technology and I’m trying to force something?

* * *

**Related discussions:**

- [Any success stories using Amazon Athena?](https://awshq.com/t/any-success-stories-using-amazon-athena/9769)
- [What is the point of “load partitions” in athena?](https://awshq.com/t/what-is-the-point-of-load-partitions-in-athena/15855)
- [AWS Glue taking long time to process data](https://awshq.com/t/aws-glue-taking-long-time-to-process-data/13705)
- [Migrating JSON document data from Azure CosmoDB to an AWS alternative](https://awshq.com/t/migrating-json-document-data-from-azure-cosmodb-to-an-aws-alternative/18224)
- [RDS vs s3+Athena](https://awshq.com/t/rds-vs-s3-athena/17692)

---

<div class="post-metadata">

**Author:** ![mradzikowski](https://awshq.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mradzikowski](https://awshq.com/u/mradzikowski)\
**Post date:** [October 25, 2020, 7:23pm UTC](https://awshq.com/t/managing-aws-glue-and-athena-tables/11830/2 "2020-10-25T19:23:36Z")

</div>

There are 3 ways of creating tables in Glue Data Catalog (from where Athena takes table schemas):

1. Defining the schema manually - through the Console, SDK, CloudFormation, etc.
2. Using a Glue Crawler to discover tables, schemas, and partitions
3. Through Athena with SQL - with simple `CREATE TABLE` or `CREATE TABLE AS SELECT` (CTAS) queries

When to which one:

1. **Manually:** Doing it manually for a number of tables with a lot of columns is not fun work. And we are hardcoding the schema, which is normal in SQL databases, but here we can get new data with new columns on a random day and we should be able to adapt to it. So this I use only when the other two are not applicable. My most common case for this: tables for files created from Lambda or Kinesis Analytics, without partitioning that would justify running a Crawler. Types are defined manually.
2. **Crawler:** it works automagically, you run the Crawler and get tables. I use the scheduled or triggered Crawlers for data that is ingested regularly and partitioned. For example, after Glue Job that ingests data from the previous hour runs the Crawler to discover a new partition. Types are discovered by Crawler.
3. **SQL:** I use CTAS for all data wrangling and data science stuff. Those are often also scheduled, for example daily, to process new data from one table and save in another. If the table does not exist yet, create it with CTAS. Otherwise, just do INSERT. Column types are the result types of SQL query columns.

So, if I understand, you tried using Crawler and get unexpected types. The question is - what type of data were you parsing? Depending on the data format Crawler can just get the type from the column definition in the file (parquet) or must discover it on its own based on the data (for example JSON and other text types).

If you were processing file types that have defined column types in them - maybe they are not what you think they are.

If you were processing text files - Crawler probably had reasons to set the column type to double. It may be that there was a single non-integer value in there. Or maybe Crawler seeing numbers in text-type file puts the double type as it does not know if the next file later won’t have floating-point values, and this file just luckily had only ints. I’m not sure about this and this can be easily tested.

What can you do:

1. Pre-process files to parquet format - for example with Glue Job (python+pandas or Spark). If you want to run Athena queries on the files it’s much better to have parquet and not text files (read more [here](https://aws.amazon.com/blogs/big-data/top-10-performance-tuning-tips-for-amazon-athena/)). If you set correct types in pre-process then Crawler will have no problem.
2. Do data wrangling with Athena. Run CTAS queries on the raw data you have (with types discovered by the Crawler) and convert types in SQL. Save new table with converted columns.
3. Handle mismatching types in SQL when doing queries, by just converting them on the fly with SQL. But it’s not the best solution if you are 100% sure that given column should be int, not double, as it may result in doing that conversion in multiple queries over time making code less performant.

---

<div class="post-metadata">

**Author:** ![dev2170](https://awshq.com/letter_avatar_proxy/v4/letter/d/e8c25b/32.png) [@dev2170](https://awshq.com/u/dev2170)\
**Post date:** [October 25, 2020, 11:31pm UTC](https://awshq.com/t/managing-aws-glue-and-athena-tables/11830/3 "2020-10-25T23:31:33Z")

</div>

Few options to avoid running glue crawler to infer the schema. If you are sure about the glue table structure upfront then you can create tables using boto3 Athena. If you want to add the partition to the glue table then you can use Athena boto3 with msck repair table command. This way you can avoid having multiple data types when you run the crawler every time. Let me know if you not able to use the boto3 Athena api.

---

<div class="post-metadata">

**Author:** ![jahaz](https://awshq.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jahaz](https://awshq.com/u/jahaz)\
**Post date:** [October 26, 2020, 4:22pm UTC](https://awshq.com/t/managing-aws-glue-and-athena-tables/11830/4 "2020-10-26T16:22:09Z")

</div>

> [@mradzikowski](#):
>
> There are 3 ways of creating tables in Glue Data Catalog (from where Athena takes table schemas): 1. Defining the schema manually - through the Console, SDK, CloudFormation, etc. 2. Using a Glue Crawler to discover …

Thanks for your detailed response. I think it will have to be mixed strategies like you mentioned.

The specific problem I’m facing now is a table has a foreign key 99% of the time. Since there are missing values it changes it to a double when it should be a int.

I guess I’ll hardcode specific tables to handle this issue.

---

<div class="post-metadata">

**Author:** ![jahaz](https://awshq.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jahaz](https://awshq.com/u/jahaz)\
**Post date:** [October 26, 2020, 4:23pm UTC](https://awshq.com/t/managing-aws-glue-and-athena-tables/11830/5 "2020-10-26T16:23:22Z")

</div>

> [@dev2170](#):
>
> Few options to avoid running glue crawler to infer the schema. If you are sure about the glue table structure upfront then you can create tables using boto3 Athena. If you want to add the partition to the glue table …

Thanks for your response. I was trying to avoid hardcoding schema but it seems like the only option.
