Skip to content

Commit

Permalink
DB Client examples (#1329)
Browse files Browse the repository at this point in the history
* Fixed JDBC ResultSet column names metadata processing.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Added pokemon sample.
 - JDBC module is finished
 - MongoDB needs DB model update

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Added mongoDB support to pokemons demo example.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Checkstyle fixes.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Exception handling should be tha same.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Final code cleanup.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* README update

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Readme fix.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Review notes applied.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>

* Fixed checkstyle issues.

Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>
  • Loading branch information
Tomas-Kraus authored Jan 31, 2020
1 parent 5d039a7 commit 015daea
Show file tree
Hide file tree
Showing 19 changed files with 1,287 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static Map<Long, DbColumn> createMetadata(ResultSet rs) throws SQLException {
Map<Long, DbColumn> byNumbers = new HashMap<>();

for (int i = 1; i <= columnCount; i++) {
String name = metaData.getColumnName(i);
String name = metaData.getColumnLabel(i);
String sqlType = metaData.getColumnTypeName(i);
Class<?> javaClass = classByName(metaData.getColumnClassName(i));
DbColumn column = new DbColumn() {
Expand Down
2 changes: 1 addition & 1 deletion examples/dbclient/mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</parent>

<artifactId>helidon-examples-dbclient-mongodb</artifactId>
<name>Helidon Examples DB Mongo</name>
<name>Helidon Examples DB Client MongoDB</name>

<dependencies>
<dependency>
Expand Down
89 changes: 89 additions & 0 deletions examples/dbclient/pokemons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Helidon DB Client Pokemon Example with JDBC

This example shows how to run Helidon DB Client over JDBC.

Application provides REST service endpoint with CRUD operations on Pokemnons
database.

## Database

Database model contains two tables:

**Types**

| Column | Type | Integrity |
|--------|---------|-------------|
| id | integer | Primary key |
| name | varchar | &nbsp; |

**Pokemons**

| Column | Type | Integrity |
|---------|---------|-------------|
| id | integer | Primary key |
| name | varchar | &nbsp; |
| id_type | integer | Type(id) |

with 1:N relationship between *Types* and *Pokemons*

## Build

```
cd <project_root>/examples/dbclient/pokemons
mvn package
```

## Run

This example requires a MySQL database, start it using docker:
```
docker run --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=pokemon -e MYSQL_USER=user -e MYSQL_PASSWORD=password mysql:5.7
```

Then run the `io.helidon.examples.dbclient.pokemons.PokemonMain` class:
```
cd <project_root>/examples/dbclient/pokemons
java -jar target/helidon-examples-dbclient-pokemons.jar
```

### Run with MongoDB

It's possible to run example with MongoDB database. Start it using docker:
```
docker run --rm --name mongo -p 27017:27017 mongo
```

Then run the `io.helidon.examples.dbclient.pokemons.PokemonMain` class with `mongo` argument:
```
cd <project_root>/examples/dbclient/pokemons
java -jar target/helidon-examples-dbclient-pokemons.jar mongo
```

## Test Example

The application has the following endpoints:

- http://localhost:8079/db - the main business endpoint (see `curl` commands below)
- http://localhost:8079/metrics - the metrics endpoint (query adds application metrics)
- http://localhost:8079/health - has a custom database health check

Application also connects to zipkin on default address.
The query operation adds database trace.

`curl` commands:

- `curl http://localhost:8079/db/type | json_pp` - list all pokemon types in the database
- `curl http://localhost:8079/db/pokemon | json_pp` - list all pokemons in the database
- `curl http://localhost:8079/db/pokemon/2 | json_pp` - get a single pokemon by id
- `curl http://localhost:8079/db/pokemon/name/Squirtle | json_pp` - get a single pokemon by name
- `curl -i -X POST -d '{"id":7,"name":"Rattata","idType":1}' http://localhost:8079/db/pokemon` - add a new pokemon Rattata
- `curl -i -X PUT -d '{"id":7,"name":"Raticate","idType":2}' http://localhost:8079/db/pokemon` - rename pokemon with id 7 to Raticate
- `curl -i -X DELETE http://localhost:8079/db/pokemon/7` - delete pokemon with id 7

### Proxy

Make sure that `localhost` is not being accessed trough proxy when proxy is configured on your system:
```
export NO_PROXY='localhost'
```
135 changes: 135 additions & 0 deletions examples/dbclient/pokemons/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-se</artifactId>
<version>2.0-SNAPSHOT</version>
<relativePath>../../../applications/se/pom.xml</relativePath>
</parent>

<artifactId>helidon-examples-dbclient-pokemons</artifactId>
<name>Helidon Examples DB Client: Pokemons Database</name>

<properties>
<mainClass>io.helidon.examples.dbclient.pokemons.PokemonMain</mainClass>
<arg>jdbc</arg>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.tracing</groupId>
<artifactId>helidon-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.tracing</groupId>
<artifactId>helidon-tracing-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-jdbc</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-metrics-jdbc</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-health</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-jsonp</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-webserver-jsonp</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.media.jsonp</groupId>
<artifactId>helidon-media-jsonp-server</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.media.jsonb</groupId>
<artifactId>helidon-media-jsonb-server</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 015daea

Please sign in to comment.