/market-orders-app-python

This application subscribes to the PubNub Market Orders Stream via Python client and stores the market trades in YugabyteDB.

Primary LanguagePythonApache License 2.0Apache-2.0

Market Orders Streaming to YugabyteDB with Python

This application subscribes to the PubNub Market Orders Stream via Python client and stores the market trades in YugabyteDB.

Check this blog post for a quick app overview.

Run using YugabyteDB Managed

  1. Sign up for YugabyteDB Managed and create a free cluster. Additionally, follow this guide to download your cluster CA certificate and set up your cluster IP allow list.

  2. Run the following to create virtual environment for app and install application dependencies (first time only):

$ python3 -m venv market_order_app_venv  # create the virtual environment
$ source market_order_app_venv/bin/activate  # activate the virtual environment
$ cd market-orders-app-python/app/
$ pip install -r ../requirements.txt
  1. Configure your environment variables in the .env file (create a .env file if it doesn't exist):
DB_HOST=127.0.0.1
PORT=5433
DB_NAME=yugabyte
USERNAME=username
PASSWORD=password
  1. Run the following to initialize the database with schema:
$ python db.py

db.py will create tables and relations using schema/default_schema.sql

  1. Seed the user data in the User table. Run the following to insert the default user data for User table:
$ python seed_user_data.py

seed_user_data.py will create user data using the faker module and inserts it into the User table.

  1. Run the application:
$ python flask_app.py

You can see the app running on http://127.0.0.1:5001

  1. Test the application (You can use Postman or cURL to send the requests)

    • Subscribe to a channel - curl --location --request GET 'http://127.0.0.1:5001/subscription/add?channel=pubnub-market-orders'
    • Unsubscribe from a channel - curl --location --request GET 'http://127.0.0.1:5001/subscription/remove?channel=pubnub-market-orders'
    • Get subscribed channels - curl --location --request GET 'http://127.0.0.1:5001/subscription/list'
    • Ingest the market order stream to YugabyteDB - curl --location --request GET 'http://127.0.0.1:5001/ingeststreamdata'
    • Get Trade stats - curl --location --request GET 'http://127.0.0.1:5001/tradestats'

    Here is the screenshot of how to call the API request in Postman.

    • Subscribe to a channel: Subscribe to the channel
    • Get Trade stats: Get Trade stats

Run using YugabyteDB locally

  1. Install YugabyteDB locally.

  2. Follow the same instructions as above (from step 2)