Tuesday, January 01, 2019

fahd.blog in 2018

Happy 2019, everyone!

I'd like to wish everyone a great start to an even greater new year!

In keeping with tradition, here's one last look back at fahd.blog in 2018.

During 2018, I posted 14 new entries on fahd.blog. I am also thrilled that I have more readers from all over the world! Thanks for reading and especially for giving feedback.

Top 5 posts of 2018:

I'm going to be writing a lot more this year, so stay tuned for more great techie tips, tricks and hacks! :)

Related posts:

Saturday, December 29, 2018

Java 11: Running single-file programs and "shebang" scripts

In Java 11, the java launcher has been enhanced to run single-file source code programs directly, without having to compile them first.

For example, consider the following class that simply adds its arguments:

import java.util.*;
public class Add {
  public static void main(String[] args) {
    System.out.println(Arrays.stream(args)
      .mapToInt(Integer::parseInt)
      .sum());
  }
}

In previous versions of Java, you would first have to compile the source file and then run it as follows:

$ javac Add.java
$ java Add 1 2 3
6

In Java 11, there is no need to compile the file! You can run it directly as follows:

$ java Add.java 1 2 3
6

It's not even necessary to have the ".java" extension on your file. You can call the file whatever you like but, if the file does not have the ".java" extension, you need to specify the --source option in order to tell the java launcher to use source-file mode. In the example below, I have renamed my file to MyJava.code and run it with --source 11:

$ java --source 11 MyJava.code 1 2 3
6

It gets even better! It is also possible to run a Java program directly on Unix-based systems using the shebang (#!) mechanism.

For example, you can take the code from Add.java and put it in a file called add, with the shebang at the start of the file, as shown below:

#!/path/to/java --source 11
import java.util.*;
public class Add {
  public static void main(String[] args) {
    System.out.println(Arrays.stream(args)
      .mapToInt(Integer::parseInt)
      .sum());
  }
}

Mark the file as executable using chmod and run it as follows:

$ chmod +x add
$ ./add 1 2 3
6

Tuesday, December 25, 2018

Java 11: Converting a Collection to an Array

In Java 11, a new default method, toArray(IntFunction), has been added to the java.util.Collection interface, which allows the collection's elements to be transferred to a newly created array of a desired runtime type.

For example:

// Java 11
List<String> list = Arrays.asList("foo","bar","baz");
String[] array = list.toArray(String[]::new);

// The above is equivalent to:
String[] array2 = list.toArray(new String[0]);

Sunday, December 23, 2018

Java 11: New HTTP Client API

In Java 11, the incubated HTTP Client API first introduced in Java 9, has been standardised. It makes it easier to connect to a URL, manage request parameters, cookies and sessions, and even supports asynchronous requests and websockets.

To recap, this is how you would read from a URL using the traditional URLConnection approach:

var url = new URL("http://www.google.com");
var conn = url.openConnection();
try (var in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
 in.lines().forEach(System.out::println);
}

Here is how you can use HttpClient instead:

var httpClient = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder(URI.create("http://www.google.com")).build();
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

The HTTP Client API also supports asynchonous requests via the sendAsync method which returns a CompletableFuture, as shown below. This means that the thread executing the request doesn't have to wait for the I/O to complete and can be used to run other tasks.

var httpClient = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder(URI.create("http://www.google.com")).build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
 .thenApply(HttpResponse::body)
 .thenAccept(System.out::println);

It's also very easy to make a POST request containing JSON from a file:

var httpClient = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder(URI.create("http://www.google.com"))
 .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofFile(Paths.get("data.json")))
    .build();

Saturday, December 22, 2018

Java: Streaming a JDBC ResultSet as CSV

In my previous post, I showed how to convert a java.sql.ResultSet to JSON and stream it back to the caller. This post, is about streaming it in CSV format instead. Streaming allows you to transfer the data, little by little, without having to load it all into the server's memory.

For example, consider the following ResultSet:

+---------+-----+
| Name    | Age |
+---------+-----+
| Alice   |  20 |
| Bob     |  35 |
| Charles |  50 |
+---------+-----+

The corresponding CSV is:

name,age
Alice,20
Bob,35
Charles,50

The following class (also available in my GitHub Repository) can be used to convert the ResultSet to CSV. Note that this class implements Spring's ResultSetExtractor, which can be used by a JdbcTemplate to extract results from a ResultSet.

/**
 * Streams a ResultSet as CSV.
 */
public class StreamingCsvResultSetExtractor
                         implements ResultSetExtractor<Void> {

  private static char DELIMITER = ',';

  private final OutputStream os;

  /**
   * @param os the OutputStream to stream the CSV to
   */
  public StreamingCsvResultSetExtractor(final OutputStream os) {
    this.os = os;
  }

  @Override
  public Void extractData(final ResultSet rs) {
    try (var pw = new PrintWriter(os, true)) {
      final var rsmd = rs.getMetaData();
      final var columnCount = rsmd.getColumnCount();
      writeHeader(rsmd, columnCount, pw);
      while (rs.next()) {
        for (var i = 1; i <= columnCount; i++) {
          final var value = rs.getObject(i);
          pw.write(value == null ? "" : value.toString());
          if (i != columnCount) {
            pw.append(DELIMITER);
          }
        }
        pw.println();
      }
      pw.flush();
    } catch (final SQLException e) {
      throw new RuntimeException(e);
    }
    return null;
  }

  private static void writeHeader(final ResultSetMetaData rsmd,
      final int columnCount, final PrintWriter pw) throws SQLException {
    for (var i = 1; i <= columnCount; i++) {
      pw.write(rsmd.getColumnName(i));
      if (i != columnCount) {
        pw.append(DELIMITER);
      }
    }
    pw.println();
  }
}

To use this in a web service with JAX-RS:

import javax.ws.rs.core.StreamingOutput;

@GET
@Path("runQuery")
@Produces("text/csv")
public StreamingOutput runQuery() {
  return new StreamingOutput() {
    @Override
    public void write(final OutputStream os)
        throws IOException, WebApplicationException {
      jdbcTemplate.query("select name, age from person",
                   new StreamingCsvResultSetExtractor(os));
    }
  };
}
Related posts:
Streaming a JDBC ResultSet as JSON