Cataloging S3 Data with Glue
Introduction: From Database to Real Data Cataloging
Welcome back! In the previous lesson, you learned how to create and organize AWS Glue databases, which act as logical containers for your data lake’s metadata. Now that you have a Glue database set up, it’s time to take the next step: connecting Glue to your actual S3 data and automatically discovering the structure of your datasets. This is a key part of making your data lake truly usable, as it allows you to search, analyze, and process your data with other AWS services.
In this lesson, you will learn how to use AWS Glue Crawlers to scan your S3 data, detect its schema, and create tables in your Glue Data Catalog. By the end, you will know how to automate the process of cataloging new data as it arrives in your data lake, making your data easily accessible for analytics and ETL jobs.
Key Concepts: Glue Crawlers and the Data Catalog
Before we dive into the code, let’s briefly review what a Glue crawler is and how it fits into the Glue Data Catalog. You already know that a Glue database is a container for tables, but how do those tables get created and kept up to date? That’s where crawlers come in.
A Glue crawler is a tool that connects to your data sources — like S3 buckets — and automatically scans the files it finds. It examines the data, infers the schema (such as column names and data types), and then creates or updates tables in your Glue database. This process is essential for keeping your data catalog accurate and up to date, especially as new data lands in your S3 data lake.
When you run a crawler, it will look at the files in the specified S3 path, figure out the structure, and register that information as tables in your Glue database. These tables are not the data itself, but rather metadata that describes where the data is stored and how it is structured. This makes it possible for tools like AWS Athena to query your data directly from S3.
Example: Cataloging S3 Data with Glue
Let’s walk through a complete example of how to connect Glue to your S3 data, run a crawler, and inspect the results. The following Python script uses the boto3 library to automate the process. If you are using the CodeSignal platform, boto3 is already installed for you. If you are working on your own device, make sure you have installed boto3 and configured your AWS credentials.
The script below will:
- Create a Glue crawler that targets your S3 bucket and path.
- Start the crawler (without waiting for it to finish).
- List the tables discovered by the crawler, including their columns and partition keys.
Note: The run_crawler() function starts the crawler and returns immediately. The crawler runs asynchronously in the background. It may take a few minutes for the crawler to finish and for new tables to appear in your Glue Data Catalog. If you don’t see your tables right away, wait a bit and then re-run the list_catalog_tables() function to check for updates.
When you run this script, you will see output similar to the following:
This output shows that the crawler has discovered a table called user_events in your S3 bucket, along with its columns and partition keys. The table’s location points to the partitioned data in your data lake. This metadata is now available in your Glue Data Catalog, making it easy to query and process your data.
