In the world of data-driven applications, accessing and manipulating data in real-time is crucial. Dozer is a powerful tool that helps you create low-latency data APIs (gRPC and REST) from any data source, enabling seamless integration with frontend applications. In this tutorial, we'll walk you through setting up Dozer with Docker and connecting it to PostgreSQL using Docker.
π Why Dozer?
Dozer addresses the challenges of accessing and manipulating data in real-time by providing an easy-to-use solution to create efficient data APIs. With Dozer, you can quickly set up APIs that can be easily integrated with your frontend applications. Furthermore, using Docker allows you to containerize your application, making it portable and easy to manage.
In this step-by-step tutorial, we'll guide you through connecting PostgreSQL with Dozer using Docker. Let's get started!
π§ Prerequisites:
- Docker/Docker desktop installed on your machine
- Basic knowledge of Docker and PostgreSQL
π Overall steps involved:
- Create a
docker-compose
file for PostgreSQL - Create a Dozer config with PostgreSQL connector and API configuration
- Run PostgreSQL (via Docker)
- Run Dozer (via Docker)
- Query your API
πΉ Step 1: Create a docker-compose.yml
file
First, create a docker-compose.yml
file and copy the following configuration to it or download the file here:
version: '3.9'
services:
postgres:
platform: linux/amd64
container_name: quick-start-postgres
image: public.ecr.aws/getdozer/dozer-samples-pg-stocks:latest
command: postgres -c hba_file=/var/lib/stock-sample/pg_hba.conf
environment:
POSTGRES_DB: stocks
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ALLOW_IP_RANGE: 0.0.0.0/0
ports:
- '5434:5432'
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres -h 0.0.0.0 -d stocks" ]
interval: 5s
timeout: 5s
retries: 5
volumes:
sample-data:
πΉ Step 2: Create a Dozer config file
Next, create a dozer-config.yaml
file with the following content:
app_name: 1-hypercharge-postgres-sample
connections:
- config: !Postgres
user: postgres
password: postgres
host: host.docker.internal
port: 5434
database: stocks
name: stocks
sources:
- name: stocks
table_name: stocks
columns:
- id
- ticker
- date
- open
- high
- low
- close
- adj_close
- volume
connection: !Ref stocks
- name: stocks_meta
table_name: stocks_meta
columns:
- symbol
- security_name
connection: !Ref stocks
endpoints:
- name: stocks
path: /stocks
table_name: stocks
index:
primary_key:
- id
- id: null
name: stocks_meta
path: /stocks-meta
table_name: stocks_meta
index:
primary_key:
- symbol
πΉ Step 3: Run PostgreSQL with sample stock data
Open a terminal or command prompt and navigate to the directory containing the docker-compose.yml
file. Run the following command to start PostgreSQL with sample stock data:
docker-compose up
πΉ Step 4: Run Dozer with PostgreSQL connector (via Docker)
Open another terminal or command prompt and navigate to the directory containing the dozer-config.yaml
file. To run Dozer as a Docker container, execute the following command:
docker run -it \
-v "$PWD":/usr/dozer \
-p 8080:8080 \
-p 50051:50051 \
--platform linux/amd64 \
public.ecr.aws/k7k6x1d4/dozer \
dozer
Congratulations π₯³ , you should see Dozer up and running!
π Query your API
Dozer automatically generates APIs in both REST and gRPC formats, along with documentation, OpenAPI, and proto files. In the following sections, we'll showcase some possible queries. Refer to the APIs section for more details.
πΈ Query Using gRPC:
You can use gRPCurl or Postman to interact with gRPC APIs.
gRPC query example:
grpcurl -plaintext localhost:50051 dozer.generated.stocks_meta.StocksMetas/query
πΈ Query Using REST:
You can use curl or Postman to interact with REST APIs.
REST query example:
curl --location --request GET 'localhost:8080/stocks-meta'
That's it! You've successfully set up Dozer with Docker and connected it to PostgreSQL. Now you can easily create APIs for your data and integrate them into your frontend applications.
If you're interested in learning more about how to use Dozer with applications, we recommend reading our previous blog post: "Building a Real-Time Data App with Dozer, React, and PostgreSQL". In this blog, you'll find a detailed tutorial that demonstrates how to integrate Dozer with a React frontend application and PostgreSQL as a data source. This comprehensive guide will give you a better understanding of how to build powerful, real-time data-driven applications using Dozer.
Happy coding, Happy Data APIng! ππ©βπ»π¨βπ»
Top comments (0)