Real-time Data Ingestion — Apache Kafka to Snowflake
This guide explores a common use case: optimizing and transforming event data in JSON format.
Last updated
This guide explores a common use case: optimizing and transforming event data in JSON format.
Last updated
Apache Kafka is an open-source distributed event streaming platform set apart by its ability to handle a high volume of data from various systems and applications. Popular data warehouses such as Snowflake and Amazon Redshift take data from Kafka and enable you to run analytics and derive insight. However, these warehouses are designed to support batch processing. They also struggle with data consistency, particularly when there’s a constant flow of evolving data. This can increase both the cost of preparing data and the time it takes to design, implement, and maintain your data architecture.
You can structure data lakes in many different ways. Most commonly, though, you structure your data in three logical zones: the Raw Zone, the Staging Zone, and the Refined Zone. This is Upsolver's recommended approach. It's simple to get started, scales with your data, and easily extend to support new use cases.
The raw zone stores all data. However, as the name implies, the data in question is uncleaned, unverified, and unoptimized for analysis. At this point, the data is only being collected, stored, and cataloged.
In the staging zone, you expose data for easy discovery and convert it to an optimized file format such as Apache Parquet. It's also compressed -- this reduces the cost of storage -- and automatically compacted, in which small files merge into larger, efficient ones. These optimizations improve the hygiene of your data and the performance of query engines. This results in reliable and fresh query results. While you can query the data now, it hasn't yet been transformed to match exact business needs. You define data pipelines that read and transform the data in the staging zone and store the results in the refined zone.
Upsolver automatically optimizes data in the refined zone. Here's how:
It converts the files into Apache Parquet
It partitions the data by date
It continuously compacts small files. The schema evolves over time as this occurs.
The end result is a prepared and fully-optimized data lake that you can query and get results from in a matter of minutes instead of days.
In this scenario, we explore a common use case: optimizing and transforming event data in the JSON format. The data, which contains nested fields, moves from the raw zone of your data to the staging zone, and then to the refined zone for further analysis by engines such as Snowflake.
The following steps illustrate how to create a data pipeline that reads from a Kafka raw zone, stages the data in the staging zone, and then performs basic transformations before writing the results to a refined zone such as Snowflake, from where you can query the results.
Connect to Kafka in the raw zone
Create a staging table
Copy from Kafka
Connect to your Snowflake EDW
Create an output table for refined data
Prepare your data for the refined zone
Read your data from Snowflake
When you first deploy Upsolver in the customer VPC, you create an Identity and Access Management (IAM) role that gives you access to any AWS resources you might require to build data pipelines. See AWS Role Permissions for more information.
In this scenario, our cluster has permission to read and write to the AWS Glue Data Catalog and to S3. If your Kafka cluster is in a VPC different from the one created for Upsolver and isn’t accessible via a public IP, you must create a VPC peering connection. Here’s a guide explaining how to do so.
Create a Kafka connection to consume data from your cluster. This connection gives you the ability to configure the AWS IAM credentials, bucket names, and prefixes that Upsolver needs to access the data. All you need to know to do this is familiar SQL syntax.
Create a Kafka connection:
You can use Upsolver to stage your data. To do this, create a job in Upsolver that reads from Kafka and creates a table with the appropriate schema in the AWS Glue Data Catalog.
Here’s the code to create a staging table to store your raw data:
Create a job to extract the raw data from Kafka and load it into the staging table.
The SQL statement below creates a job that reads data from the Kafka topic to a staging location and catalogs the metadata in the Glue Data Catalog. Note that Upsolver automatically creates a Glue Data Catalog table and uses the name you designate in the INTO
statement for the database and table names. It also creates an S3 prefix in your source S3 bucket using the name given in the PREFIX
statement.
Create an ingestion job:
Upsolver uses a JDBC connection to write data to Snowflake. This connection creates temporary tables in Snowflake while the data is continuously streamed. It also merges delta lakes into Snowflake tables. Upsolver automatically manages the merging process. This essentially means that you no longer must maintain Delta files or tables. Ultimately this eliminates the need for large-scale, full table scans on Snowflake that can be slow or expensive.
Here’s the code for creating that connection:
INSERT
MERGE
DELETE
To take full advantage of these jobs, we recommend that you explicitly create your output table, which enables you to define partitions, primary keys, and more.
You don’t need to define all of your output columns, as Upsolver automatically adds any missing columns. If you do want to strictly control which columns to add, then when you CREATE JOB
you can define them in the CREATE TABLE
statement and set the ADD_MISSING_COLUMNS
job property to FALSE
.
Here's example code for creating an output table in Snowflake:
Now that you have defined the output table, create an INSERT
job to read data from the staging table and write the results into the target table inside your refined zone.
Here's the code for creating a job to read from staging and write to the refined zone:
To view the pipeline results you can query in Snowflake. Use SELECT
to query your table.
This guide showed how to take raw data from Kafka, stage the data, and transform it to your specific business needs. We modeled this approach after a common data lake design pattern, in which data transitions through the raw, staging, and refined zones. By adopting and implementing familiar SQL syntax, you can use Upsolver to create data pipelines and organize your data to easily perform analytics and ML. Furthermore, as your business needs evolve, so can your data. In the future, you can create additional jobs that use the staging table as the source of creativity and innovation, while your pipelines indefinitely keep your data fresh.
To launch the template:
Launch Upsolver by navigating to https://db.upsolver.com
Sign up by entering your email address and setting a password.
After you click through the onboarding panel, a panel with a list of templates is displayed.
Select the Kafka to Snowflake template.
Upsolver runs on AWS Virtual Private Cloud (VPC). Learn how to and Deploy Upsolver on AWS.
There are several types of jobs you can create to do this -- for example, copying the data from the source to your destination. To ingest data to the staging zone, use the command.
To start transforming your data, choose from three types of job: