Contents

[Chicago Optimizng Public Transportation]

Public Transit Status with Apache Kafka

In this project, we constructed a streaming event pipeline around Apache Kafka and its ecosystem. Using public data from the Chicago Transit Authority we constructed an event pipeline around Kafka that allows us to simulate and display the status of train lines in real time.

When the project is complete, you will be able to monitor a website to watch trains move from station to station.

Check the source code in here

Prerequisites

The following are required to complete this project:

  • Docker
  • Python 3.7

Description

The Chicago Transit Authority (CTA) has asked us to develop a dashboard displaying system status for its commuters. We have decided to use Kafka and ecosystem tools like REST Proxy and Kafka Connect to accomplish this task.

Our architecture will look like so:

Project Diagram

Step 1: Create Kafka Producers

The first step in our plan is to configure the train stations to emit some of the events that we need. The CTA has placed a sensor on each side of every train station that can be programmed to take an action whenever a train arrives at the station.

To accomplish this, you must complete the following tasks:

  1. Complete the code in producers/models/producer.py
  2. Define a value schema for the arrival event in producers/models/schemas/arrival_value.json with the following attributes
    • station_id
    • train_id
    • direction
    • line
    • train_status
    • prev_station_id
    • prev_direction
  3. Complete the code in producers/models/station.py so that:
    • A topic is created for each station in Kafka to track the arrival events
    • The station emits an arrival event to Kafka whenever the Station.run() function is called.
    • Ensure that events emitted to kafka are paired with the Avro key and value schemas
  4. Define a value schema for the turnstile event in producers/models/schemas/turnstile_value.json with the following attributes
    • station_id
    • station_name
    • line
  5. Complete the code in producers/models/turnstile.py so that:
    • A topic is created for each turnstile for each station in Kafka to track the turnstile events
    • The station emits a turnstile event to Kafka whenever the Turnstile.run() function is called.
    • Ensure that events emitted to kafka are paired with the Avro key and value schemas

Step 2: Configure Kafka REST Proxy Producer

Our partners at the CTA have asked that we also send weather readings into Kafka from their weather hardware. Unfortunately, this hardware is old and we cannot use the Python Client Library due to hardware restrictions. Instead, we are going to use HTTP REST to send the data to Kafka from the hardware using Kafka’s REST Proxy.

To accomplish this, you must complete the following tasks:

  1. Define a value schema for the weather event in producers/models/schemas/weather_value.json with the following attributes
    • temperature
    • status
  2. Complete the code in producers/models/weather.py so that:
    • A topic is created for weather events
    • The weather model emits weather event to Kafka REST Proxy whenever the Weather.run() function is called.
      • NOTE: When sending HTTP requests to Kafka REST Proxy, be careful to include the correct Content-Type. Pay close attention to the examples in the documentation for more information.
    • Ensure that events emitted to REST Proxy are paired with the Avro key and value schemas

Step 3: Configure Kafka Connect

Finally, we need to extract station information from our PostgreSQL database into Kafka. We’ve decided to use the Kafka JDBC Source Connector.

To accomplish this, you must complete the following tasks:

  1. Complete the code and configuration in producers/connectors.py

Step 4: Configure the Faust Stream Processor

We will leverage Faust Stream Processing to transform the raw Stations table that we ingested from Kafka Connect. The raw format from the database has more data than we need, and the line color information is not conveniently configured. To remediate this, we’re going to ingest data from our Kafka Connect topic, and transform the data.

To accomplish this, you must complete the following tasks:

  1. Complete the code and configuration in `consumers/faust_stream.py

Watch Out!

You must run this Faust processing application with the following command:

faust -A faust_stream worker -l info

Step 5: Configure the KSQL Table

Next, we will use KSQL to aggregate turnstile data for each of our stations. Recall that when we produced turnstile data, we simply emitted an event, not a count. What would make this data more useful would be to summarize it by station so that downstream applications always have an up-to-date count

To accomplish this, you must complete the following tasks:

  1. Complete the queries in consumers/ksql.py

Tips

  • The KSQL CLI is the best place to build your queries. Try ksql in your workspace to enter the CLI.
  • You can run this file on its own simply by running python ksql.py
  • Made a mistake in table creation? DROP TABLE <your_table>. If the CLI asks you to terminate a running query, you can TERMINATE <query_name>

Step 6: Create Kafka Consumers

With all of the data in Kafka, our final task is to consume the data in the web server that is going to serve the transit status pages to our commuters.

To accomplish this, you must complete the following tasks:

  1. Complete the code in consumers/consumer.py
  2. Complete the code in consumers/models/line.py
  3. Complete the code in consumers/models/weather.py
  4. Complete the code in consumers/models/station.py

Documentation

In addition to the course content you have already reviewed, you may find the following examples and documentation helpful in completing this assignment:

Directory Layout

The project consists of two main directories, producers and consumers.

The following directory layout indicates the files that the student is responsible for modifying by adding a * indicator. Instructions for what is required are present as comments in each file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
* - Indicates that the files I completed the code in this file. Other files are prepared by Udacity beforehand. 


├── consumers
│   ├── consumer.py *
│   ├── faust_stream.py *
│   ├── ksql.py *
│   ├── models
│   │   ├── lines.py
│   │   ├── line.py *
│   │   ├── station.py *
│   │   └── weather.py *
│   ├── requirements.txt
│   ├── server.py
│   ├── topic_check.py
│   └── templates
│       └── status.html
└── producers
    ├── connector.py *
    ├── models
    │   ├── line.py
    │   ├── producer.py *
    │   ├── schemas
    │   │   ├── arrival_key.json
    │   │   ├── arrival_value.json *
    │   │   ├── turnstile_key.json
    │   │   ├── turnstile_value.json *
    │   │   ├── weather_key.json
    │   │   └── weather_value.json *
    │   ├── station.py *
    │   ├── train.py
    │   ├── turnstile.py *
    │   ├── turnstile_hardware.py
    │   └── weather.py *
    ├── requirements.txt
    └── simulation.py

Running and Testing

To run the simulation, you must first start up the Kafka ecosystem on their machine utilizing Docker Compose.

%> docker-compose up

Running the Simulation

There are two pieces to the simulation, the producer and consumer. As you develop each piece of the code, it is recommended that you only run one piece of the project at a time.

However, when you are ready to verify the end-to-end system prior to submission, it is critical that you open a terminal window for each piece and run them at the same time. If you do not run both the producer and consumer at the same time you will not be able to successfully complete the project.

To run the producer:

  1. cd producers
  2. virtualenv venv
  3. . venv/bin/activate
  4. pip install -r requirements.txt
  5. python simulation.py

Once the simulation is running, you may hit Ctrl+C at any time to exit.

To run the Faust Stream Processing Application:

  1. cd consumers
  2. virtualenv venv
  3. . venv/bin/activate
  4. pip install -r requirements.txt
  5. faust -A faust_stream worker -l info

To run the KSQL Creation Script:

  1. cd consumers
  2. virtualenv venv
  3. . venv/bin/activate
  4. pip install -r requirements.txt
  5. python ksql.py

To run the consumer:

NOTE: Do not run the consumer until you have reached Step 6!

  1. cd consumers
  2. virtualenv venv
  3. . venv/bin/activate
  4. pip install -r requirements.txt
  5. python server.py

Whole Simulation Process

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
kafka-topics --list --zookeeper localhost:2181
cd producers
python connector.py
cd ../
kafka-console-consumer --bootstrap-server localhost:9092 --topic postgre_connect_stations --from-beginning
cd consumers
faust -A faust_stream worker -l info

cd ../
kafka-console-consumer --bootstrap-server localhost:9092 --topic org.chicago.cta.weather.v1 --from-beginning

cd producers
python simulation.py

cd consumers
python server.py


# delete topics
kafka-topics --zookeeper localhost:2181 --delete --topic arrival.station.*
kafka-topics --zookeeper localhost:2181 --delete --topic turnstile_station
kafka-topics --zookeeper localhost:2181 --delete --topic org.chicago.cta.weather.v1