This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-kamelets-examples.git
commit 69eb4834a55b3076526171aae5212e747f0cfb58 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Jun 1 11:03:02 2024 +0200 Add SQL example --- jbang/sql/README.adoc | 83 ++++++++++++++++++++++++++++++++++++++ jbang/sql/UsingSql.java | 10 +++++ jbang/sql/application.properties | 4 ++ jbang/sql/db-init.sql | 9 +++++ jbang/sql/docker-compose.yml | 17 ++++++++ jbang/sql/insert.sql | 1 + jbang/sql/using-kamelet.camel.yaml | 18 +++++++++ 7 files changed, 142 insertions(+) diff --git a/jbang/sql/README.adoc b/jbang/sql/README.adoc new file mode 100644 index 0000000..2a0842e --- /dev/null +++ b/jbang/sql/README.adoc @@ -0,0 +1,83 @@ +== SQL + +This example shows how to use a SQL database with Camel. + +The example comes with a `docker-compose` file for running a local Postgres database. +The database is initialized with a `users` table with some sample data. + +There are two examples; + +- `UsingSql.java` - Java DSL using the SQL component +- `using-kamelet.camel.yaml` - Yaml DSL using a Postgres Kamelet + +There is also a `application.properties` configuration file that setup +a JDBC `DataSource` for connecting to the database. This configuration file +is only needed for the `UsingSql.java` example as the Kamelet comes with JDBC driver included. + +=== Install JBang + +First install JBang according to https://www.jbang.dev + +When JBang is installed then you should be able to run from a shell: + +[source,sh] +---- +$ jbang --version +---- + +This will output the version of JBang. + +To run this example you can either install Camel on JBang via: + +[source,sh] +---- +$ jbang app install camel@apache/camel +---- + +Which allows to run CamelJBang with `camel` as shown below. + +=== How to run + +Then run the Postgres database using Docker: + +[source,sh] +---- +$ docker-compose up +---- + +And then run Camel: + +[source,sh] +---- +$ camel run using-kamelet.camel.yaml +---- + +Or try the Java DSL by: + +[source,sh] +---- +$ camel run UsingSql.java application.properties +---- + +When the Camel integration is running, you can insert a new user, from another terminal: + +[source,sh] +---- +$ camel cmd send --endpoint=sql:file:insert.sql --body=empty +---- + +And you should see the 3rd user being logged by Camel. + +NOTE: To use `camel cmd send` then this requires to have `application.properties` included +if you run the kamelet example. + + +=== Help and contributions + +If you hit any problem using Camel or have some feedback, then please +https://camel.apache.org/community/support/[let us know]. + +We also love contributors, so +https://camel.apache.org/community/contributing/[get involved] :-) + +The Camel riders! diff --git a/jbang/sql/UsingSql.java b/jbang/sql/UsingSql.java new file mode 100644 index 0000000..6c3015a --- /dev/null +++ b/jbang/sql/UsingSql.java @@ -0,0 +1,10 @@ +import org.apache.camel.builder.RouteBuilder; + +public class UsingSql extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("sql:select * from users?repeatCount=1") + .log("${body}"); + } +} diff --git a/jbang/sql/application.properties b/jbang/sql/application.properties new file mode 100644 index 0000000..39c4bc9 --- /dev/null +++ b/jbang/sql/application.properties @@ -0,0 +1,4 @@ +spring.datasource.url=jdbc:postgresql://localhost:5432/test +spring.datasource.username=postgres +spring.datasource.password=postgres +spring.datasource.driverClassName=org.postgresql.Driver \ No newline at end of file diff --git a/jbang/sql/db-init.sql b/jbang/sql/db-init.sql new file mode 100644 index 0000000..62cc9dc --- /dev/null +++ b/jbang/sql/db-init.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS users; +CREATE TABLE users( + id INT, + name VARCHAR(20), + byear INT +); + +INSERT INTO users VALUES (1, 'user1', 1995); +INSERT INTO users VALUES (2, 'user2', 1996); diff --git a/jbang/sql/docker-compose.yml b/jbang/sql/docker-compose.yml new file mode 100644 index 0000000..0aed89d --- /dev/null +++ b/jbang/sql/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3.8' +services: + db: + image: postgres:14.1-alpine + restart: always + environment: + - POSTGRES_DB=test + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + ports: + - '5432:5432' + volumes: + - db:/var/lib/postgresql/data + - ./db-init.sql:/docker-entrypoint-initdb.d/create_tables.sql +volumes: + db: + driver: local \ No newline at end of file diff --git a/jbang/sql/insert.sql b/jbang/sql/insert.sql new file mode 100644 index 0000000..2295b27 --- /dev/null +++ b/jbang/sql/insert.sql @@ -0,0 +1 @@ +INSERT INTO users VALUES (3, 'user3', 2000); \ No newline at end of file diff --git a/jbang/sql/using-kamelet.camel.yaml b/jbang/sql/using-kamelet.camel.yaml new file mode 100644 index 0000000..56db116 --- /dev/null +++ b/jbang/sql/using-kamelet.camel.yaml @@ -0,0 +1,18 @@ +- route: + id: route-692e + nodePrefixId: route-2cf + from: + id: from-b8a7 + uri: kamelet:postgresql-source + parameters: + serverName: localhost + serverPort: 5432 + username: postgres + password: postgres + query: select * from users + databaseName: test + delay: 5000 + steps: + - log: + id: log-e4e1 + message: ${body}