DB2API is a lightweight Spring Boot service that provides a uniform REST API
for executing queries against different databases such as:
- SQL databases (MySQL, PostgreSQL, Oracle, SQL Server, etc.)
- MongoDB
- Redis
With DB2API, applications can access heterogeneous databases via a single, consistent HTTP interface.
DB2API currently supports the following SQL and NoSQL engines:
- MySQL / Connector/J
- MariaDB
- PostgreSQL / Postgres / pgjdbc_ng
- Microsoft SQL Server / MSSQL
- Oracle
- Apache Derby
- H2
- HSQLDB
- Firebird / Jaybird
- IBM DB2 / ibm_jcc
- IBM Informix / informix
- SAP HANA
- SQLite / Xerial
- OrientDB
- MongoDB
- Redis
These are the engines that can currently be used with DB2API.
-
Clone the repository:
git clone https://github.com/opencelium/db2api.git cd db2api -
Build the project with Maven:
mvn clean package
-
Activate the systemd file and add it to the Autostart:
ln -s "$(pwd)"/conf/db2api.service /etc/systemd/system/db2api.service systemctl daemon-reload systemctl enable db2api
-
Start the service:
systemctl start db2api
The service runs by default on port 8080.
Note:
If you want to use another port, add in thedb2api.serverconfig at the end of theExecStartcommand:
--server.port=9090Example:
ExecStart=/usr/bin/java -jar /opt/opencelium/services/db2api/target/db2api-1.0.0.jar --server.port=9000Don’t forget to reload the systemd config:
systemctl daemon-reload systemctl restart db2api
GET /connect-testRequired HTTP headers:
X-DB-Engine→ Database engine (mysql, postgres, mongodb, redis, …)X-DB-Host→ Database hostX-DB-Port→ (optional) Database portX-DB-Name→ Database/schema nameX-DB-User→ (optional) UsernameX-DB-Password→ (optional) PasswordX-DB-Options→ (optional) Extra connection parameters (e.g. encrypt=false;trustServerCertificate=true to disable ssl verification)
Example response:
{
"success": true,
"engine": "postgres",
"databaseProductName": "PostgreSQL",
"databaseProductVersion": "14.5"
}POST /query
Content-Type: application/jsonRequest body:
{
"query": "SELECT * FROM users WHERE id = ?",
"params": [123],
"maxRows": 100
}Response example (SQL):
{
"success": true,
"rowCount": 1,
"rows": [
{
"id": 123,
"name": "Alice"
}
]
}query= collection nameparams= filter as JSON objectfields= list of fields to returnmaxRows= max number of documents
Example request:
{
"query": "users",
"params": [
{ "age": { "$gt": 25 } }
],
"fields": ["name", "email", "_id"],
"maxRows": 10
}Example response:
{
"success": true,
"rowCount": 2,
"rows": [
{ "name": "Alice", "email": "alice@example.com", "_id": "64e..." },
{ "name": "Bob", "email": "bob@example.com", "_id": "64f..." }
]
}query= Redis keyparamsare ignored
Example request:
{
"query": "myKey"
}Example response:
{
"success": true,
"key": "myKey",
"value": "Hello World"
}The service logs can be viewed via journalctl:
journalctl -xe -u db2api -o cat -fThis command shows the live logs (-f) of the db2api service.
DB2API is released under the Apache-2.0 License.