Springboot Debezium Example Code

What is Debezium?

Debezium is an open-source distributed platform that provides change data capture (CDC) functionality. It allows you to monitor and capture changes in data stores, such as databases, and stream those changes to other systems in real-time.

Debezium is built on top of Apache Kafka and uses Kafka Connect to monitor and capture changes. It provides connectors for various data stores, including MySQL, PostgreSQL, Oracle, MongoDB, and more.

Using Debezium, you can easily set up CDC for your data stores and stream the changes to other systems for further processing or analysis. This can be useful for a variety of use cases, such as synchronizing data between systems, data replication, and event-driven architectures.

SpringBoot Debezium MySQL Example

Here is an example of how you could use Debezium with Spring Boot to monitor and capture changes in a database:

First, you will need to add the Debezium dependencies to your project’s pom.xml file:\


    io.debezium
    debezium-core
    1.4.0.Final


    io.debezium
    debezium-connector-mysql
    1.4.0.Final

Next, you will need to configure the Debezium connector in your application’s application.properties file:

# Debezium MySQL connector configuration
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=user
spring.datasource.password=password

# Debezium configuration
debezium.name=my-connector
debezium.connector.class=io.debezium.connector.mysql.MySqlConnector
debezium.offset.storage=org.apache.kafka.connect.storage.FileOffsetBackingStore
debezium.offset.storage.file.filename=/tmp/debezium/offsets.dat
debezium.offset.flush.interval.ms=60000
debezium.config.common.key.converter=org.apache.kafka.connect.json.JsonConverter
debezium.config.common.value.converter=org.apache.kafka.connect.json.JsonConverter
debezium.config.common.key.converter.schemas.enable=true
debezium.config.common.value.converter.schemas.enable=true
debezium.config.connector.mysql.server.id=12345
debezium.config.connector.mysql.server.name=my-server
debezium.config.connector.mysql.hostname=localhost
debezium.config.connector.mysql.port=3306
debezium.config.connector.mysql.user=user
debezium.config.connector.mysql.password=password
debezium.config.connector.mysql.database.whitelist=test
debezium.config.connector.mysql.table.whitelist=test.customers

Then, you can create a DebeziumConfig class to initialize the Debezium connector:

@Configuration
public class DebeziumConfig {

    @Bean
    public EmbeddedEngine embeddedEngine(@Value("${debezium.name}") String connectorName,
                                        @Value("${debezium.config.common.key.converter}") String keyConverter,
                                        @Value("${debezium.config.common.value

SpringBoot Debezium PostgreSQL example

Here is an example of how you could use Debezium with Spring Boot to monitor and capture changes in a PostgreSQL database:

First, you will need to add the Debezium dependencies to your project’s pom.xml file:


    io.debezium
    debezium-core
    1.4.0.Final


    io.debezium
    debezium-connector-postgresql
    1.4.0.Final


Next, you will need to configure the Debezium connector in your application’s application.properties file:

# Debezium PostgreSQL connector configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=user
spring.datasource.password=password

# Debezium configuration
debezium.name=my-connector
debezium.connector.class=io.debezium.connector.postgresql.PostgresConnector
debezium.offset.storage=org.apache.kafka.connect.storage.FileOffsetBackingStore
debezium.offset.storage.file.filename=/tmp/debezium/offsets.dat
debezium.offset.flush.interval.ms=60000
debezium.config.common.key.converter=org.apache.kafka.connect.json.JsonConverter
debezium.config.common.value.converter=org.apache.kafka.connect.json.JsonConverter
debezium.config.common.key.converter.schemas.enable=true
debezium.config.common.value.converter.schemas.enable=true
debezium.config.connector.postgresql.server.name=my-server
debezium.config.connector.postgresql.hostname=localhost
debezium.config.connector.postgresql.port=5432
debezium.config.connector.postgresql.user=user
debezium.config.connector.postgresql.password=password
debezium.config.connector.postgresql.database.whitelist=test
debezium.config.connector.postgresql.table.whitelist=test.customers

Then, you can create a DebeziumConfig class to initialize the Debezium connector:

@Configuration
public class DebeziumConfig {

    @Bean
    public EmbeddedEngine embeddedEngine(@Value("${debezium.name}") String connectorName,
                                        @Value("${debezium.config.common.key.converter}") String keyConverter,
                                        @Value("${debezium.config.common.value.converter}") String value

SpringBoot Debezium AWS RDS Example

Here is an example of how you could use Debezium with Spring Boot to monitor and capture changes in an Amazon RDS database:

First, you will need to add the Debezium dependencies to your project’s pom.xml file:


    io.debezium
    debezium-core
    1.4.0.Final


    io.debezium
    debezium-connector-mysql
    1.4.0.Final


Next, you will need to configure the Debezium connector in your application’s application.properties file:

# Debezium MySQL connector configuration
spring.datasource.url=jdbc:mysql://:/
spring.datasource.username=
spring.datasource.password=

# Debezium configuration
debezium.name=my-connector
debezium.connector.class=io.debezium.connector.mysql.MySqlConnector
debezium.offset.storage=org.apache.kafka.connect.storage.FileOffsetBackingStore
debezium.offset.storage.file.filename=/tmp/debezium/offsets.dat
debezium.offset.flush.interval.ms=60000
debezium.config.common.key.converter=org.apache.kafka.connect.json.JsonConverter
debezium.config.common.value.converter=org.apache.kafka.connect.json.JsonConverter
debezium.config.common.key.converter.schemas.enable=true
debezium.config.common.value.converter.schemas.enable=true
debezium.config.connector.mysql.server.id=12345
debezium.config.connector.mysql.server.name=my-server
debezium.config.connector.mysql.hostname=
debezium.config.connector.mysql.port=
debezium.config.connector.mysql.user=
debezium.config.connector.mysql.password=
debezium.config.connector.mysql.database.whitelist=
debezium.config.connector.mysql.table.whitelist=.customers

Replace , , , , and with the appropriate values for your Amazon RDS database.

Elixir restapi convert database record: MySQL + PostgreSQL + MongoDB to File Excel

Example 1: Elixir convert MySQL record to Excel file

To convert a MySQL database record to an Excel file in an Elixir REST API, you can use a library like Ecto to query the database and retrieve the record, and then use a library like ElixirExcel to generate the Excel file.

Here’s an example of how you might do this:

Add the necessary dependencies to your Elixir project:

defp deps do
  [
    {:ecto, "~> 3.4"},
    {:elixir_excel, "~> 1.3"}
  ]
end

In your controller, use Ecto to query the database and retrieve the record:

import Ecto

def export_to_excel(conn, params) do
  # Connect to the database
  {:ok, repo} = Ecto.Repo.start_link(:my_app)

  # Retrieve the record from the database
  record = Repo.get(MyApp.Object, params["id"])

  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

In your view, you can use the ElixirExcel library to generate an Excel file from the record and send it to the client:

import ElixirExcel

def export_to_excel(record) do
  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

That’s a basic example of how you can convert a MySQL database record to an Excel file in an Elixir REST API. You can customize the Excel layout and formatting using the options provided by the ElixirExcel library.

Example 2: Exlix Restapi convert PostgreSQL to Excel file

To convert a PostgreSQL database record to an Excel file in an Elixir REST API, you can use a library like Ecto to query the database and retrieve the record, and then use a library like ElixirExcel to generate the Excel file.

Here’s an example of how you might do this:

Add the necessary dependencies to your Elixir project:

defp deps do
  [
    {:ecto, "~> 3.4"},
    {:elixir_excel, "~> 1.3"}
  ]
end

In your controller, use Ecto to query the database and retrieve the record:

import Ecto

def export_to_excel(conn, params) do
  # Connect to the database
  {:ok, repo} = Ecto.Repo.start_link(:my_app)

  # Retrieve the record from the database
  record = Repo.get(MyApp.Object, params["id"])

  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

In your view, you can use the ElixirExcel library to generate an Excel file from the record and send it to the client:

import ElixirExcel

def export_to_excel(record) do
  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

Example 3: Elixir restapi convert Mongodb database document to file excel

o convert a MongoDB database document to an Excel file in an Elixir REST API, you can use a library like MongoDB Elixir to query the database and retrieve the document, and then use a library like ElixirExcel to generate the Excel file.

Here’s an example of how you might do this:

Add the necessary dependencies to your Elixir project:

defp deps do
  [
    {:mongodb_elixir, "~> 0.4"},
    {:elixir_excel, "~> 1.3"}
  ]
end

In your controller, use MongoDB Elixir to query the database and retrieve the document:

import MongoDB

def export_to_excel(conn, params) do
  # Connect to the database
  {:ok, client} = MongoDB.Client.start_link(["mongodb://localhost:27017"])

  # Retrieve the document from the database
  document = MongoDB.Collection.find_one(client, "my_app", "objects", {"_id" => params["id"]})

  # Generate the Excel file from the document using ElixirExcel
  excel = ElixirExcel.generate(document)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

In your view, you can use the ElixirExcel library to generate an Excel file from the document and send it to the client:

import ElixirExcel

def export_to_excel(document) do
  # Generate the Excel file from the document using ElixirExcel
  excel = ElixirExcel.generate(document)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

Elixir create RestApi Model: One to one example: PostgreSQL + MySQL + MongoDB

Example 1: Exilir + PostgreSQL: One-to-One

Here’s an example of how you might create a REST API in Elixir that uses a PostgreSQL database and models a one-to-one relationship between two entities, using the Plug and Ecto libraries:

defmodule MyApi do
  use Plug.Router
  use Ecto.Repo, otp_app: :my_app

  plug :match
  plug :dispatch

  get "/users/:user_id/profile" do
    user_id = Integer.parse(conn.params["user_id"])
    user = Repo.get(User, user_id)
    profile = Repo.get(Profile, user.profile_id)
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(200, Poison.encode!(%{user: user, profile: profile}))
  end
end

This defines a single endpoint, GET /users/:user_id/profile, which retrieves a user and their associated profile from the users and profiles tables in the database, respectively, and returns them as a JSON object. The user_id is passed as a URL parameter.

To run this API, you’ll need to set up a database connection using the Ecto library. Here’s an example of how you might do this in your config.exs file:

config :my_app, MyApi.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "my_database",
  username: "postgres",
  password: "postgres",
  hostname: "localhost"

You can then use the Plug.Adapters.Cowboy adapter to run the API as a standalone web server:

Plug.Adapters.Cowboy.http MyApi, []

This is just a very simple example, but you can use the Plug and Ecto libraries to build more complex APIs with multiple endpoints, various HTTP methods, and more advanced database queries.

For more information, you can check out the documentation for the Plug and Ecto libraries:

Plug: https://hexdocs.pm/plug/readme.html
Ecto: https://hexdocs.pm/ecto/readme.html

Example 2: Elixir MySQL One-to-One RestAPI

Here’s an example of how you might create a REST API in Elixir that uses a MySQL database and models a one-to-one relationship between two entities, using the Plug and Ecto libraries:

defmodule MyApi do
  use Plug.Router
  use Ecto.Repo, otp_app: :my_app

  plug :match
  plug :dispatch

  get "/users/:user_id/profile" do
    user_id = Integer.parse(conn.params["user_id"])
    user = Repo.get(User, user_id)
    profile = Repo.get(Profile, user.profile_id)
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(200, Poison.encode!(%{user: user, profile: profile}))
  end
end

This defines a single endpoint, GET /users/:user_id/profile, which retrieves a user and their associated profile from the users and profiles tables in the database, respectively, and returns them as a JSON object. The user_id is passed as a URL parameter.

To run this API, you’ll need to set up a database connection using the Ecto library. Here’s an example of how you might do this in your config.exs file:

config :my_app, MyApi.Repo,
  adapter: Ecto.Adapters.MySQL,
  database: "my_database",
  username: "root",
  password: "root",
  hostname: "localhost"

You’ll also need to install the mysql_ecto library, which provides the MySQL adapter for Ecto. To do this, add mysql_ecto to your list of dependencies in your mix.exs file:

defp deps do
  [
    {:mysql_ecto, "~> 2.0"}
  ]
end

Then run mix deps.get to install the dependencies.

You can then use the Plug.Adapters.Cowboy adapter to run the API as a standalone web server:

Plug.Adapters.Cowboy.http MyApi, []

Example 3: Elixir + MongoDB + One-to-One

Here’s an example of how you might create a REST API in Elixir that uses a MongoDB database and models a one-to-one relationship between two entities, using the Plug and Mongo libraries:

defmodule MyApi do
  use Plug.Router
  use Mongo.Connection

  plug :match
  plug :dispatch

  get "/users/:user_id/profile" do
    user_id = conn.params["user_id"]
    user = Mongo.find_one("users", %{"_id" => user_id})
    profile = Mongo.find_one("profiles", %{"user_id" => user_id})
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(200, Poison.encode!(%{user: user, profile: profile}))
  end
end

This defines a single endpoint, GET /users/:user_id/profile, which retrieves a user and their associated profile from the users and profiles collections in the database, respectively, and returns them as a JSON object. The user_id is passed as a URL parameter.

To run this API, you’ll need to set up a database connection using the Mongo library. Here’s an example of how you might do this in your config.exs file:

config :my_app, MyApi.Mongo,
  hostname: "localhost",
  database: "my_database"

You’ll also need to install the mongodb library, which provides the MongoDB driver for Elixir. To do this, add `mongod

Angular 14 Spring Boot JWT Authentication example | Angular 14 + Spring Security + MySQL Full Stack – Part 3: Build Frontend

The tutorial is Part 3 of the series: Angular Spring Boot JWT Authentication example | Angular 14 + Spring Security + MySQL Full Stack. Today we’re gonna build a Angular HTTP Client App that can interact with SpringBoot JWT Authentication Server.

Part 1: Overview and Architecture.
Part 2: Build SpringBoot Backend

Continue reading “Angular 14 Spring Boot JWT Authentication example | Angular 14 + Spring Security + MySQL Full Stack – Part 3: Build Frontend”

Spring Boot + Angular 14 example | Spring Data JPA + REST + MySQL CRUD example

In this tutorial, we show you Angular 14 Http Client & Spring Boot Server example that uses Spring JPA to do CRUD with MySQL and Angular 14 as a front-end technology to make request and receive response.

Related Posts:
How to use Spring JPA MySQL | Spring Boot

Update for Angular 8:
Spring Boot 2.1 + Angular 8 + MySQL example | Angular HTTP Client + RestAPIs + Spring JPA CRUD + MySQL tutorial

You may like this series:
Angular Spring Boot JWT Authentication example | Angular 14 + Spring Security + MySQL Full Stack

Continue reading “Spring Boot + Angular 14 example | Spring Data JPA + REST + MySQL CRUD example”