This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k-examples.git
commit 9c695c1bc6a46ce42d2f95559f8e474b5aabca34 Author: Kuthumi Pepple <[email protected]> AuthorDate: Thu Jun 30 15:09:13 2022 +0100 fix errors and improve database example --- .../databases/PostgresDBAutoDatasource.java | 12 +++---- generic-examples/databases/README.md | 41 ++++++++++++++++++++-- generic-examples/databases/datasource.properties | 2 +- .../databases/postgres-deploy/README.md | 36 ++++++++++++++----- .../postgres-deploy/postgres-deployment.yaml | 2 +- 5 files changed, 75 insertions(+), 18 deletions(-) diff --git a/generic-examples/databases/PostgresDBAutoDatasource.java b/generic-examples/databases/PostgresDBAutoDatasource.java index 6984e33..5f66f0d 100644 --- a/generic-examples/databases/PostgresDBAutoDatasource.java +++ b/generic-examples/databases/PostgresDBAutoDatasource.java @@ -20,7 +20,7 @@ // -p quarkus.datasource.camel.jdbc.url=jdbc:postgresql://postgres:5432/test // -p quarkus.datasource.camel.username=postgresadmin // -p quarkus.datasource.camel.password=admin123 -// -d mvn:io.quarkus:quarkus-jdbc-postgresql:1.13.7.Final +// -d mvn:io.quarkus:quarkus-jdbc-postgresql:2.10.0.Final // // Alternatively, you can bundle your credentials as a secret properties file: // @@ -29,17 +29,17 @@ // kamel run PostgresDBAutoDatasource.java --dev // --build-property quarkus.datasource.camel.db-kind=postgresql // --config secret:my-datasource -// -d mvn:io.quarkus:quarkus-jdbc-postgresql:1.13.7.Final +// -d mvn:io.quarkus:quarkus-jdbc-postgresql:2.10.0.Final import org.apache.camel.builder.RouteBuilder; public class PostgresDBAutoDatasource extends RouteBuilder { @Override public void configure() throws Exception { - from("timer://foo?period=10000") - .setBody(constant("select * from test")) - .to("jdbc:camel") - .to("log:info"); + from("timer://foo?period=10000") + .setBody(constant("SELECT * FROM test")) + .to("jdbc:camel") + .to("log:info"); } } \ No newline at end of file diff --git a/generic-examples/databases/README.md b/generic-examples/databases/README.md index 301a077..7727428 100644 --- a/generic-examples/databases/README.md +++ b/generic-examples/databases/README.md @@ -1,3 +1,40 @@ -# Examples showing how to connect Camel K with databases +# Camel K with database example -Find useful examples about how to develop a Camel K integration connecting to a database. \ No newline at end of file +This example demonstrates how to use a database in a Camel K integration. + +You can find more information about Apache Camel and Apache Camel K on the [official Camel website](https://camel.apache.org). + +## Before you begin + +Read the general instructions in the [root README.md file](/README.md) for setting up your environment and the Kubernetes cluster before looking at this example. + +Make sure you've read the [installation instructions](https://camel.apache.org/camel-k/latest/installation/installation.html) for your specific +cluster before starting the example. + +## Additional Requirements for running this example + +**A PostgreSQL instance**: needed database for running the example. For installation instructions, see [How to deploy a simple Postgres DB to a Kubernetes cluster](./postgres-deploy/) for demo purposes. + +## Understanding the Example +- [`PostgresDBAutoDatasource.java`](./PostgresDBAutoDatasource.java) contains the integration code. It defines a route that periodically queries a database and logs the result. +- [`datasource.properties`](./datasource.properties) holds your credentials for connecting to the database. + +## Running the Example +You should have a PostgreSQL instance running in a namespace. If not see [How to deploy a simple Postgres DB to a Kubernetes cluster](./postgres-deploy/) + +Bundle your credentials as a secret: +``` +kubectl create secret generic my-datasource --from-file=datasource.properties +``` + +Run the integration: +``` +kamel run PostgresDBAutoDatasource.java --dev --build-property quarkus.datasource.camel.db-kind=postgresql --config secret:my-datasource -d mvn:io.quarkus:quarkus-jdbc-postgresql:2.10.0.Final +``` + +If successful, the query result: `hello` and `world`, should be logged to the terminal every 10 seconds: +```console +[1] 2022-06-30 09:30:56,313 INFO [info] (Camel (camel-1) thread #1 - timer://foo) Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body: [{data=hello}, {data=world}]] +[1] 2022-06-30 09:31:06,312 INFO [info] (Camel (camel-1) thread #1 - timer://foo) Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body: [{data=hello}, {data=world}]] +[1] 2022-06-30 09:31:16,313 INFO [info] (Camel (camel-1) thread #1 - timer://foo) Exchange[ExchangePattern: InOnly, BodyType: java.util.ArrayList, Body: [{data=hello}, {data=world}]] +``` \ No newline at end of file diff --git a/generic-examples/databases/datasource.properties b/generic-examples/databases/datasource.properties index 587ca7f..23a7134 100644 --- a/generic-examples/databases/datasource.properties +++ b/generic-examples/databases/datasource.properties @@ -1,3 +1,3 @@ -quarkus.datasource.camel.jdbc.url=jdbc:postgresql://postgres:5432/test +quarkus.datasource.camel.jdbc.url=jdbc:postgresql://postgres:5432/testdb quarkus.datasource.camel.username=postgresadmin quarkus.datasource.camel.password=admin123 \ No newline at end of file diff --git a/generic-examples/databases/postgres-deploy/README.md b/generic-examples/databases/postgres-deploy/README.md index 11b280f..aa8396c 100644 --- a/generic-examples/databases/postgres-deploy/README.md +++ b/generic-examples/databases/postgres-deploy/README.md @@ -1,8 +1,14 @@ -# How to deploy a simple Postgres DB to Kubernetes cluster +# How to deploy a simple Postgres DB to a Kubernetes cluster -This is a very simple example to show how to create a Postgres database. **Note**, this is not ready for any production purpose. +This is a simple guide on how to deploy and create a Postgres database. **Note**, this is not ready for any production purpose. ## Create a Kubernetes Deployment +**Note:** Openshift environments may first need to change the security context constraints to allow Postgres to perform root operations. To do this, run: +``` +oc adm policy add-scc-to-user anyuid -z default +``` + +To create a configmap, persist data, deploy Postgres to kubernetes and create a service, run the following commands: ``` kubectl create -f postgres-configmap.yaml kubectl create -f postgres-storage.yaml @@ -11,20 +17,34 @@ kubectl create -f postgres-service.yaml ``` ## Test the connection -Connection credentials available in the _postgres-configmap.yaml_ descriptor. +Connection credentials available in the [postgres-configmap.yaml](./postgres-configmap.yaml) descriptor. ``` kubectl get svc postgres -psql -h <IP> -U postgresadmin --password -p <PORT> postgresdb ``` -## Create a test database and table + +To connect to the PostgreSQL database, run the command below, changing the pod name: +``` +kubectl exec -it postgres-xxxx -- psql -h postgres -U postgresadmin --password -p 5432 postgresdb +``` +You will be prompted for password. Connection credentials are in the [postgres-configmap.yaml](./postgres-configmap.yaml) file. \ +After you enter your password, you should get a PostgreSQL shell + +## Create a `testdb` database with a `test` table +Run the command below in the PostgreSQL shell to both create a new database and switch to it. Enter the previous password when prompted: +``` +CREATE DATABASE testdb; +\c testdb; +``` + +To create a table and populate it, run: ``` -CREATE DATABASE test; CREATE TABLE test (data TEXT PRIMARY KEY); INSERT INTO test(data) VALUES ('hello'), ('world'); ``` -### Read the test database and table +### Read the `test` table from the `testdb` database ``` SELECT * FROM test; ``` - +You should see 2 rows containing 'hello' and 'world' respectively. Enter `exit` to exit the shell. \ +Our `testdb` database works fine and can now be used in a Camel K integration. diff --git a/generic-examples/databases/postgres-deploy/postgres-deployment.yaml b/generic-examples/databases/postgres-deploy/postgres-deployment.yaml index 5814b88..56489ad 100644 --- a/generic-examples/databases/postgres-deploy/postgres-deployment.yaml +++ b/generic-examples/databases/postgres-deploy/postgres-deployment.yaml @@ -31,7 +31,7 @@ spec: spec: containers: - name: postgres - image: postgres:10.4 + image: postgres:14.4 imagePullPolicy: "IfNotPresent" ports: - containerPort: 5432
