Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Building and Testing the GTFS Client

Build a client to fetch and parse real-time transit data from GTFS feeds

This module implements a client that communicates with GTFS-realtime feeds to fetch transit vehicle positions, forming the data acquisition layer of the application.

Obtaining an API Token

To access real transit data, you need an API token from a transit data provider. This tutorial uses the San Francisco Bay Area's 511.org API, which provides GTFS-realtime data for multiple transit agencies.

  1. Complete the registration form with your details

  2. Submit the form

  3. Save the API token that arrives by email

The process of obtaining an API token is similar for most transit data providers. If you want to use data from a different agency, check their developer portal for access instructions.

Configuring Environment Variables

To securely manage API tokens without hardcoding them in source code, the application loads environment variables from a .env file.

Create a file named .env in the root of the project with the following content, replacing your_token_here with your actual API token:

# 511.org API token - get yours at https://511.org/open-data/token
API_TOKEN=your_token_here

# GTFS Feed URL
GTFS_BASE_URL=https://api.511.org/transit/vehiclepositions

# GTFS Agency - default is San Francisco Muni
GTFS_AGENCY=SF

Understanding the Client's Role

The GTFS client is responsible for:

  1. External Data Acquisition: Connecting to a GTFS-realtime feed provided by a transit agency

  2. Protocol Buffer Processing: Parsing the binary format used by GTFS-realtime

  3. Data Transformation: Converting external data structures into the domain model

  4. Error Handling: Dealing with network issues, data format changes, and other potential problems

Configuration Service

Open the ConfigService.java file to see how the application loads configuration from the .env file:

The ConfigService uses the dotenv-java library to load environment variables:

This code loads environment variables from the .env file, retrieves the API token, base URL, and agency values, constructs the complete feed URL with query parameters, and provides methods to access these values throughout the application.

Implementing the GTFS Client

The GTFS client is implemented in the GtfsService class. Open it:

The core of the implementation is the getVehiclePositions() method:

This method opens a connection to the GTFS feed URL, uses FeedMessage.parseFrom() to parse the binary Protocol Buffer data, iterates through each entity in the feed, extracts relevant fields (vehicle ID, route ID, position, timestamp, status), converts timestamps to LocalDateTime for Ignite compatibility, and creates VehiclePosition objects matching the database schema.

The mapVehicleStatus() method converts GTFS enum values to readable strings:

Testing the GTFS Client

Run the GTFS feed example to test the client:

This runs the GtfsFeedExample class. Open it:

The example loads configuration, connects to the Ignite cluster, sets up the schema, creates the GTFS feed service, fetches vehicle positions, displays sample data, and analyzes statistics.

The core part:

Expected output:

The actual output varies depending on the current state of the transit system.

Analyzing the Data

The ReportingService class implements data analysis. Open it:

The analyzeVehicleData() method provides statistics about the transit system:

Next Steps

This module built and tested a GTFS client that forms the data acquisition layer of the transit monitoring system. The client handles connecting to external data sources, parsing protocol buffer formats, and transforming the data into the domain model.

The next module implements a data ingestion service that uses this client to regularly fetch transit data and store it in the Ignite database.

This page is: Copyright © 2026 MariaDB. All rights reserved.

Last updated

Was this helpful?