Quarkus - Using Apache Tika

This guide explains how your Quarkus application can use Apache Tika to parse the documents.

Apache Tika is a content analysis toolkit which is used to parse the documents in PDF, Open Document, Excel and many other well known binary and text formats using a simple uniform API. Both the document text and properties (metadata) are available once the document has been parsed.

If you are planning to run the application as a native executable and parse documents that may have been created with charsets different than the standard ones supported in Java such as UTF-8 then you should configure Quarkus Maven Plugin to get the native image generator include all the charsets available to the JVM:

  1. <plugin>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-maven-plugin</artifactId>
  4. <executions>
  5. <execution>
  6. <id>native-image</id>
  7. <goals>
  8. <goal>native-image</goal>
  9. </goals>
  10. <configuration>
  11. <addAllCharsets>true</addAllCharsets>
  12. </configuration>
  13. </execution>
  14. </executions>
  15. </plugin>

Prerequisites

To complete this guide, you need:

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Clone the Git repository: git clone [https://github.com/quarkusio/quarkus-quickstarts.git](https://github.com/quarkusio/quarkus-quickstarts.git), or download an archive.

The solution is located in the tika-quickstart directory.

The provided solution contains a few additional elements such as tests and testing infrastructure.

Creating the Maven Project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.7.6.Final:create \
  2. -DprojectGroupId=org.acme.example \
  3. -DprojectArtifactId=tika-quickstart \
  4. -DclassName="org.acme.tika.TikaParserResource" \
  5. -Dpath="/parse" \
  6. -Dextensions="tika,resteasy"
  7. cd tika-quickstart

This command generates a Maven project, importing the tika and resteasy extensions.

If you already have your Quarkus project configured you can add the tika and resteasy extensions to your project by running the following command in your project base directory.

  1. ./mvnw quarkus:add-extension -Dextensions="tika,resteasy"

This will add the following to your pom.xml:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-tika</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.quarkus</groupId>
  7. <artifactId>quarkus-resteasy</artifactId>
  8. </dependency>

Examine the generated JAX-RS resource

Open the src/main/java/org/acme/tika/TikaParserResource.java file and see the following content:

  1. package org.acme.tika;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6. @Path("/parse")
  7. public class TikaParserResource {
  8. @GET
  9. @Produces(MediaType.TEXT_PLAIN)
  10. public String hello() {
  11. return "hello";
  12. }
  13. }

Update the JAX-RS resource

Next update TikaParserResource to accept and parse PDF and OpenDocument format documents:

  1. package org.acme.tika;
  2. import java.io.InputStream;
  3. import java.time.Duration;
  4. import java.time.Instant;
  5. import javax.inject.Inject;
  6. import javax.ws.rs.Consumes;
  7. import javax.ws.rs.POST;
  8. import javax.ws.rs.Path;
  9. import javax.ws.rs.Produces;
  10. import javax.ws.rs.core.MediaType;
  11. import io.quarkus.tika.TikaParser;
  12. import org.jboss.logging.Logger;
  13. @Path("/parse")
  14. public class TikaParserResource {
  15. private static final Logger log = Logger.getLogger(TikaParserResource.class);
  16. @Inject
  17. TikaParser parser;
  18. @POST
  19. @Path("/text")
  20. @Consumes({"application/pdf", "application/vnd.oasis.opendocument.text"})
  21. @Produces(MediaType.TEXT_PLAIN)
  22. public String extractText(InputStream stream) {
  23. Instant start = Instant.now();
  24. String text = parser.getText(stream);
  25. Instant finish = Instant.now();
  26. log.info(Duration.between(start, finish).toMillis() + " mls have passed");
  27. return text;
  28. }
  29. }

As you can see the JAX-RS resource method was renamed to extractText, @GET annotation was replaced with POST and @Path(/text) annotation was added, and @Consumes annotation shows that PDF and OpenDocument media type formats can now be accepted. An injected TikaParser is used to parse the documents and report the extracted text. It also measures how long does it take to parse a given document.

Run the application

Now we are ready to run our application. Use:

  1. ./mvnw compile quarkus:dev

and you should see output similar to:

quarkus:dev Output

  1. $ ./mvnw clean compile quarkus:dev
  2. [INFO] Scanning for projects...
  3. [INFO]
  4. INFO] --------------------< org.acme.example:apache-tika >--------------------
  5. [INFO] Building apache-tika 1.0-SNAPSHOT
  6. [INFO] --------------------------------[ jar ]---------------------------------
  7. ...
  8. Listening for transport dt_socket at address: 5005
  9. 2019-10-15 14:23:26,442 INFO [io.qua.dep.QuarkusAugmentor] (main) Beginning quarkus augmentation
  10. 2019-10-15 14:23:26,960 INFO [io.qua.resteasy] (build-15) Resteasy running without servlet container.
  11. 2019-10-15 14:23:26,960 INFO [io.qua.resteasy] (build-15) - Add quarkus-undertow to run Resteasy within a servlet container
  12. 2019-10-15 14:23:26,991 INFO [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 549ms
  13. 2019-10-15 14:23:27,637 INFO [io.quarkus] (main) Quarkus 999-SNAPSHOT started in 1.361s. Listening on: http://0.0.0.0:8080
  14. 2019-10-15 14:23:27,638 INFO [io.quarkus] (main) Profile dev activated. Live Coding activated.
  15. 2019-10-15 14:23:27,639 INFO [io.quarkus] (main) Installed features: [cdi, resteasy, tika]

Now that the REST endpoint is running, we can get it to parse PDF and OpenDocument documents using a command line tool like curl:

  1. $ curl -X POST -H "Content-type: application/pdf" --data-binary @target/classes/quarkus.pdf http://localhost:8080/parse/text
  2. Hello Quarkus

and

  1. $ curl -X POST -H "Content-type: Content-type: application/vnd.oasis.opendocument.text" --data-binary @target/classes/quarkus.odt http://localhost:8080/parse/text
  2. Hello Quarkus

Building a native executable

You can build a native executable with the usual command ./mvnw package -Pnative. Running it is as simple as executing ./target/tika-quickstart-1.0-SNAPSHOT-runner.

Configuration Reference