Skip to content

YugabyteDB-Samples/market-orders-app

Repository files navigation

Market Orders Streaming to YugabyteDB

The application subscribes to the PubNub's market orders stream and stores the market trades in an underlying database. You can switch between MySQL, PostgreSQL and YugabyteDB with no need to update the application logic.

Start Database

  • Start a MySQL instance and load sample data:

    docker-compose -f docker-compose-mysql.yml up

    Use port 3307 for connections from the host machine.

  • or start a PostgreSQL database instead:

    docker-compose -f docker-compose-postgres.yml up

    Use port 5438 for connections from the host machine.

  • or start a YugabyteDB Managed instance, and load the schema (./schema/schema_postgres.sql) and sample data (./schema/data.sql).

Build and run Java app

  • Build and package the app:

    mvn clean package 
  • Run the app by connecting to the selected database:

    • Connect to Postgres (./properties/postgres.properties):
    java -jar target/market-orders-app.jar connectionProps=./properties/postgres.properties loadScript=./schema/schema_postgres.sql tradeStatsInterval=2000
    • Connect to MySQL (./properties/mysql.properties):
    java -jar target/market-orders-app.jar connectionProps=./properties/mysql.properties loadScript=./schema/schema_mysql.sql tradeStatsInterval=2000
    • Connect to YugabyteDB Managed after providing connecting settings in the ./properties/yugabyte-template.properties file:
    java -jar target/market-orders-app.jar connectionProps=./properties/yugabyte.properties loadScript=./schema/schema_postgres.sql tradeStatsInterval=2000

The tradeStatsInterval (measured in milliseconds) instructs the TradeStats.java service to query trade-related statistics from the database within the specified interval. If the interval is <= 0 or not set, then the statistics will not be collected.

Advanced Demo

The app can be used to demonstrate the migration from PostreSQL/MySQL to YugabyteDB as well as high-avalability and scalability capabilities of YugabyteDB. Follow this page for more details.

Run in Docker

  1. Build the app:

    mvn clean package 
  2. Create an image:

    docker rmi market-orders-app
    docker build -t market-orders-app .
  3. Start the app inside a container:

    docker run --name market-orders-instance --net custom-network \
    market-orders-app:latest \
    java -jar /home/target/market-orders-app.jar \
    connectionProps=/home/yugabyte-docker.properties \
    loadScript=/home/schema_postgres.sql \
    tradeStatsInterval=2000