You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/17 11:00:32 UTC

[camel-kamelets-examples] 01/02: jbang pipe example

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 65ee61ee9db1491f3fc6bee0332c1f4e580c9a06
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 17 10:54:01 2022 +0200

    jbang pipe example
---
 jbang/pipe/.vscode/settings.json |  6 ++++
 jbang/pipe/README.adoc           | 60 ++++++++++++++++++++++++++++++++++++++++
 jbang/pipe/to_csv.java           | 21 ++++++++++++++
 jbang/pipe/words.txt             |  5 ++++
 4 files changed, 92 insertions(+)

diff --git a/jbang/pipe/.vscode/settings.json b/jbang/pipe/.vscode/settings.json
new file mode 100644
index 0000000..cf11b06
--- /dev/null
+++ b/jbang/pipe/.vscode/settings.json
@@ -0,0 +1,6 @@
+{
+    "java.project.referencedLibraries": [
+        "lib/**/*.jar",
+        "/Users/davsclaus/Library/Application Support/Code/User/globalStorage/redhat.vscode-camelk/java-dependencies-3.14.1/dependencies/*.jar"
+    ]
+}
\ No newline at end of file
diff --git a/jbang/pipe/README.adoc b/jbang/pipe/README.adoc
new file mode 100644
index 0000000..684f265
--- /dev/null
+++ b/jbang/pipe/README.adoc
@@ -0,0 +1,60 @@
+== Pipe
+
+This example shows how to use Camel with terminal scripting.
+The script is coded in Java in the `to_csv.java` file.
+
+The script reads from system:in (using the camel stream component)
+which automatic will split the input line by line. Each line
+is aggregated and assembled into a single comma separated output,
+that is routed to system:out.
+
+Yes this is overkill to use Camel to transform such kind of task,
+but we wanted to keep this example simple, but use EIPs instead
+of a basic from -> to route.
+
+
+=== 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 make the example runnable as a script, you must ensure the file
+has execution permission:
+
+[source,sh]
+----
+chmod +x to_csv.java
+----
+
+To run this example you can then run the script:
+
+[source,sh]
+----
+echo "Hello\nWorld" | ./to_csv.java
+----
+
+You can combine this with more pipes and filters scripting such as.
+For example from a file that has a set of words:
+
+[source,sh]
+----
+cat words.txt | sort | ./to_csv.java
+----
+
+=== 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/pipe/to_csv.java b/jbang/pipe/to_csv.java
new file mode 100755
index 0000000..88d9ad5
--- /dev/null
+++ b/jbang/pipe/to_csv.java
@@ -0,0 +1,21 @@
+///usr/bin/env jbang --quiet camel@apache/camel pipe "$0" "$@" ; exit $?
+// camel-k: language=java
+
+import org.apache.camel.builder.AggregationStrategies;
+import org.apache.camel.builder.RouteBuilder;
+
+public class to_csv extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        // read from system:in
+        from("stream:in")
+            // aggregate all lines together using comma as separator
+            .aggregate(constant("true")).completionTimeout(100)
+              .aggregationStrategy(AggregationStrategies.string(","))
+              // append new line to mark end of line
+              .setBody().simple("${body}\n")
+              // write to system:out
+              .to("stream:out");
+    }
+}
diff --git a/jbang/pipe/words.txt b/jbang/pipe/words.txt
new file mode 100644
index 0000000..61c22f5
--- /dev/null
+++ b/jbang/pipe/words.txt
@@ -0,0 +1,5 @@
+camel
+rocks
+again
+jbang
+awesome
\ No newline at end of file