This example shows how to connect a Vaadin application to MongoDB databases. This file also shows how to use MaxScale to connect the same application to a MariaDB backend using the NoSQL protocol without modifying the app.
You need MongoDB listening on port 27017 or a MariaDB database and a MaxScale instance with a Listener using the nosqlprotocol
protocol.
Clone this repository:
git clone https://github.com/alejandro-du/mongodb-vaadin-demo.git
Spin up a MariaDB database and a database proxy (MaxScale):
cd mongodb-vaadin-demo
docker compose up -d
WARNING! These Docker images are not suitable for production!
Build the Java web application using Maven:
mvn package
Run the application:
java -jar target/mongodb-vaadin-demo.jar
Access the application in your browser at http://localhost:9090. Insert some data.
Access the MaxScale GUI at http://localhost:8989/ using the following credentials:
- Username:
admin
- Password:
mariadb
In the menu, go to the query editor and connect to the database using:
- Listener name:
query_router_listener
- Username:
user
- Password:
password
Inspect the database and query the student
table:
select * from student
Or use MariaDB's JSON functions:
select
json_value(doc, '$.firstName') as firstName,
json_value(doc, '$.lastName') as lastName,
id as regNumber
from student
Or if you want to join data with other tables in the database:
select s.*, st.id regNumber
from student st,
json_table(st.doc, '$'
columns(
firstName varchar(255) path '$.firstName',
lastName varchar(255) path '$.lastName'
)
) as s
To shutdown the database cluster run:
docker compose down
Add -v
to the above command if you want to remove the related Docker volumes as well (you'll lose all the configuration and data).