Pular para o conteúdo principal

Using jbang for ETL Scripts

As a Java programmer I prefer to use good old Java to extract/transform/load data to make it usable in tools I work for data visualization. However, it is a burden having to use maven just to write a single Java file. Hopefully there's a new tool called jbang that makes it easier to do scripting in Java.



More than simple scripts

What is impressive about jbang is that you are not restricted to standard Java APIs in fact you can use any library you want coming from Maven! What you will miss is the burden of having to deal with pom.xml and maven builds. Dependencies are simply declared in the Java file itself using a "DEPS" comment, for example:

//DEPS org.slf4j:slf4j-nop:1.7.25


Installation

To install jbang you just have to check the best way for you in downloads page.  In my case I have sdkman, so it was just a matter of running:


$ sdk install jbang


Hello World

The simplest hello world we can thing can run with jbang without any additional configuration, just install jbang and you should be able to run java files. So let's say I have a hello.java just like this


I can run it using: jbang hello.java. With an additional comment at the begging of the file I can make the file work as an executable file. Add the comment as the file first line (linux environments) and make the file executable:

///usr/bin/env jbang "$0" "$@" ; exit $? 

chmod +x hello.java

Then you should be able to run using ./hello.java. This comment can be added to existing files, but if you are starting a new script with jbang just use: $ jbang init hello.java


Real world script with external dependencies

In real world you will need external dependencies. I had a Java file I use to process some CSV files, it is in a maven project and this version can be seen here. To run it with jbang here' what I did:

  • Went to the Java file and added these comments to the beginning:

//usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS org.apache.commons:commons-csv:1.8

//JAVA 11


And that should be it... But for my project I did two additional steps:

  • Got rid of Maven stuff
  • Removed package declaration
My ETL folder is clear now and I can use Java and have contributions from others because they only need to have jbang installed to run my Java file. 

Development is easier, you will not lose content assist or IDE goodies with jbang. For example, I can edit a Java file in vscode by just using: code `jbang edit myfile.java`

Learning more


This is not all, specially with JavaFX, where all the setup is done by jbang! If you want to learn more I recommend you to check jbang examples and  this talk from  jbang father Max Andersen:




Comentários

Postagens mais visitadas deste blog

Dancing lights with Arduino - The idea

I have been having fun with Arduino these days! In this article I am going to show how did I use an electret mic with Arduino to create a Dancing Lights circuit. Dancing Lights   I used to be an eletronician before starting the IT college. I had my own electronics maintenance office to fix television, radios, etc. In my free time I used to create electronic projects to sell and I made a few "reais" selling a version of Dancing lights, but it was too limited: it simply animated lamps using a relay in the output of a 4017 CMOS IC. The circuit was a decimal counter  controlled by a 555. 4017 decimal counter. Source in the image When I met Arduino a few years ago, I was skeptical because I said: I can do this with IC, why should I use a microcontroller. I thought that Arduino was for kids. But now my pride is gone and I am having a lot of fun with Arduino :-) The implementation of Dancing Lights with Arduino uses an electret mic to capture the sound and light leds...

Simplest JavaFX ComboBox autocomplete

Based on this Brazilian community post , I've created a sample Combobox auto complete. What it basically does is: When user type with the combobox selected, it will work on a temporary string to store the typed text; Each key typed leads to the combobox to be showed and updated If backspace is type, we update the filter Each key typed shows the combo box items, when the combobox is hidden, the filter is cleaned and the tooltip is hidden:   The class code and a sample application is below. I also added the source to my personal github , sent me PR to improve it and there are a lot of things to improve, like space and accents support.

Creating Fat JARs for JavaFX Applications

A FAT JAR is a type of Java application distribution where a single JAR contains all other dependencies, so no additional file is required to run the JAR besides the Java Virtual Machine. For any Java maven based application, creating a FAR JAR could be solved by using Maven Shade Plugin . However, creating FAT Jars using JavaFX may be a challenge because JavaFX uses modules. Fortunately this subject was intensely discussed in the WEB, and a good explanation and a solution was provided by Jose Pereda in this StackOverflow response. In this post I want to briefly share the steps to make a FAT JAR and post an example on my github so I can point others to check the example. How to create a FAT JAR for a JavaFX application? 1- Create a main class that will run your application. This class must have the main method and call your actual application static launch method; 2- Add the Shade Plugin to your project. For those using Gradle notice that Jose Pereda also provided an answer about it i...