A Hybrid Recommender with Yelp Challenge Data -- Part II

Part II: Building Our Pipeline: Kafka, Spark, & Flask

In Part I of our blog post, we covered the machine learning models used to create our recommender system. Part II of our blog will go through the technical implementation of our pipeline. After reading countless blog posts by fellow engineers creating similar pipelines, we were unable to find one with a detailed explaination of their process. For that reason, we wrote this blog post to serve as a guide for others. All code associated to this blog post can be found in our github repository.

Introduction:

As we explored the different approaches of building and implementing our pipeline, a few factors were taken into consideration:1. Can it be built in two weeks?
With two weeks to complete our capstone and having little data engineering experience, we divided the project into three phases:

Researching what's necessary to build a pipeline

Implementing a working pipeline

Optimizing our pipeline

2. Can we each work on our parts of the Capstone without impacting each other?
Being a team of five, we had to be intentional about how we distributed and then integrated the individual pieces that made up our project.3. Are we using the appropriate technology and is it scalable?
With the popularity of "big data" in the tech-world, we wanted to make sure we were using the appropriate tools for our project. Additionally, our project needed to be scalable. Amazon Web Services (AWS) offered the flexibility to allocate the appropriate number of resources, allowing you to upgrade/downgrade your configuration based on computational needs.

The Components:

Our pipeline consisted of three main parts:

Apache Spark - a fast and general engine for large-scale data processing: This is where our machine-learning models were trained and data was processed.

Having the least familiarity with Kafka and Spark, we began with those two components and integrated our Flask App once we had succesffully implemented a connection between Kafka and Spark.

Building & Configuring the Pipeline:Google (Cloud), Amazon (AWS), and Microsoft (Azure) all offer free trials for their cloud computing services, providing a nice option for students to gain familiarity with the world of web services. For our project, we decided to use Amazon Web Services.

Both Spark & Kafka will be installed on Amazon EC2 Instances.

Configuring Kafka on EC2:

As touched on above, we used Kafka to stream information from our webapp into our machine-learning model and then send new information back to our webapp. If you'd like more information on Kafka, they provide a nice introduction with more details on its API and various configurations.

Step 1: Creating an EC2 Instance:

From your AWS dashboard, click on the launch a virtual machine wizard to create a new instance (this will be where you will run Kafka/Spark on). We used a t2.medium type on an Ubuntu Server, though a t2.micro instance-type can also be used.

Make sure you download the key-pair .pem file, as you will need it to log into the instance from your local machine. If you're using a Mac/Linux machine, you can store your SSH keys in the ~/.ssh/ directory. Windows users, click here to configure your ssh key to work with Putty. To make your key executable on your local machine, change its permissions by running:

5. AWS configures your EC2 instance to only be accessable via SSH and on your current IP Address. If you need access from another IP Address, you can do so by changing the allowed IP's in your Security Group settings in AWS.

Step 2: Installing Java:

Once you have connected to your ec2 instance, run the following commands to update the packages library and update all installed packages:

$ sudo apt-get update$ sudo apt-get upgrade

Kafka runs on top of the *Java Runtime Environment*, which must be installed first:

$ sudo apt-get install default-jdk

To confirm installation, you should see the following after running java -version:

Step 4: Configuring Kafka:

Topics: Topics can be thought of as channels where data can travel through. A server can have multiple topics and each topic can have multiple subscribers.

Producers: Produces data to the specified topic.

Consumers: Consumes data from the specified topic.

If you are running a t2.micro EC2 instance, you will need to reconfigure your ZooKeeper and Kafka server script to prevent your instance from running out of memory. Use your preferred editor to update the .sh file and replace the memory size to 256MB.

Structured Streaming simplifies the syntax for reading and writing streams, replacing read()& write() with readStream() & writeStream(). Each new line of data being streamed into Spark can be thought of as an appended observation to an unbounded table, as visualized above.

On your Spark EC2 instance, create a python script with the following code:

Testing the Pipeline:

The video below shows each of the three sections of our pipeline in communication. The top-right corner is the Python code above, running on our local machine. The middle section is a Kafka consumer, showing each stream being consumed by Kafka. The bottom section is the Spark console, showing Spark reading in each observation and writing it out to the console.

After confirming that our pipeline was working, we were then able to move on to integrating our machine learning models and connecting to our Flask application, which can be found in Part 1 of our blog post.

About Authors

Reza Rad

Recognizing the increasingly powerful role that data plays in society, Reza enrolled in the NYC Data Science Academy to be better equipped in solving tomorrow's big questions. With a B.S. in Biomedical Engineering from George Washington University, ﻿he's...

Sam O'Mullane

Sam O'Mullane is a multidisciplinary scientist who is passionate about making data science accessible to a wide audience. Sam received his MSc in Quantum Fields and Fundamental Forces from Imperial College London (Th. Physics) in 2013 and PhD...

Sean Kickham

Sean migrated from the Midwest to New York City after graduating with a BS in Mathematics from the University of Notre Dame. He taught middle school math for five years in city schools. Equipped with a Masters in...

Andrew Rubino

Andrew graduated from UC Santa Barbara with a degree in English and Statistics. He previously worked at an adtech company where he learned the ins and outs of cleaning, transforming, and reporting on big data using a variety...

Chao Shi

Chao Shi is a quantitative thinker with business vision. After graduating from Peking University in Geophysics, he obtained a PhD from Cornell in Computational Geodynamics by modeling the earth interior with Finite Element Method and Distributed Computing techniques....