Publish data

This page explains how to publish data to GlassFlow pipelines.

The Python SDK provides a programmatic way to interact with GlassFlow pipelines and the library is used to publish or consume data.

Create a data publisher for the mobility project

In this section, we'll explore how to generate data for the mobility project and publish it to the data pipeline in GlassFlow using the Python SDK. We created a data pipeline with a transformation function in the previous section.

Prerequisites

We assume that you have already completed the following before proceeding with the tutorial:

Install required libraries

Install required libraries including GlassFlow SDK listed in the requirements.txt file using the pipcommand in a terminal.

pip install -r requirements.txt

Publish real-time API events to the pipeline

Create a new Python script file called producer_api.py in your project root directory and insert the below code. This Python script serves as a data producer, fetching mobility events data from a mock API server and publishing it to a GlassFlow pipeline.

In the above Python code, there are a few things to consider like initializing the GlassFlow client, interacting with the pipeline, and publishing data to the pipeline.

  1. Initializes a client for interacting with the GlassFlow platform. It provides access to various functionalities, including pipeline management, data publishing, and data pulling.

client = glassflow.GlassFlowClient()
  1. Creates a pipeline client object specifically for interacting with the particular pipeline within a specified space. It takes three parameters: space_id (the ID of the space where the pipeline resides) , pipeline_id (the ID of the pipeline) and pipeline_access_token (the access token required for authentication). It returns a client object that can be used to publish data to the specified pipeline.

pipeline_client = client.pipeline_client(space_id=space_id, 
                                         pipeline_id=pipeline_id, 
                                         pipeline_access_token=token)
  1. Publishes a new event into the pipeline. It takes one parameter: request_body (the data to be published) . Under the hood, it sends a request to the GlassFlow API endpoint with the provided data to publish the new event to the given pipeline.

pipeline_client.publish(request_body)

Refer to Python SDK documentation for more details.

Run the script

Run the Python script producer_api.py

python producer_api.py

This script continuously fetches mock mobility events data from a mock API server and publishes it to the specified GlassFlow pipeline.

Next

After successfully producing, and publishing data into the pipeline, and transforming it, you can now consume the transformed data.

Last updated

© 2023 GlassFlow