DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI-Powered Knowledge: LlamaIndex and Apache Tika for Enterprises

Trending

  • Part 3: End-to-End Tracing and Observability Across Goose, agentgateway, and Quarkus
  • How You Clear Your HTML5 Canvas Matters
  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Apache Tika

This short tutorial is all about extracting data with the Apache Tika library. Read below to find out how to extract your own data.

By 
Ali ZHAGPAROV
·
Sep. 18, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Overview

Apache Tika is a library that allows you to extract data from PDF, XLS, PDT, etc. Apache Tika using a decorator pattern so you can easily fit it to your needs.

Adding Dependency

XML
 
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>1.26</version>
</dependency>


Apache Tika in Action

Java
 
public class DzoneApacheTikaExample {

    public static void main(String[] args) throws TikaException, IOException, SAXException {
        DzoneApacheTikaExample example = new DzoneApacheTikaExample();
        String value = example.readFromPdf("test-reading.pdf");
        System.out.println("Data read from PDF: ");
        System.out.println(value);
    }

    public String readFromPdf(String readFromFileName) throws IOException, TikaException, SAXException {
        InputStream stream = this.getClass()
          		.getClassLoader()
                .getResourceAsStream(readFromFileName);

        Parser parser = new AutoDetectParser();
        ContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        ParseContext context = new ParseContext();

        parser.parse(stream, handler, metadata, context);
        return handler.toString();
    }
}


BodyContentHandler: Collects all information inside the body tag.

Metadata: All metadata collected during the parsing will be stored there.

ParseContext: Used to pass context information to Apache Tika parser.

ContentHandler: Based on decorator patter so we can make complex handling. 

For example, let's write parsed data into a file: 

Java
 
public class DzoneApacheTikaExample {

    public static void main(String[] args) throws TikaException, IOException, SAXException {
        DzoneApacheTikaExample example = new DzoneApacheTikaExample();
        example.writeParsedDataToFile("capitals.xlsx", "/Users/ali_zhagparov/Desktop/excel-content.txt");
    }

    public void writeParsedDataToFile(String readFromFileName, String writeToFileName) throws IOException, TikaException, SAXException {
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream(readFromFileName);

        File yourFile = new File(writeToFileName);
        yourFile.createNewFile(); // if file already exists, will do nothing
        FileOutputStream fileOutputStream = new FileOutputStream(yourFile, false);

        Parser parser = new AutoDetectParser();
        ContentHandler handler = new BodyContentHandler(new WriteOutContentHandler(fileOutputStream));
        Metadata metadata = new Metadata();
        ParseContext context = new ParseContext();

        parser.parse(stream, handler, metadata, context);
    }
}


Apache Tika

Opinions expressed by DZone contributors are their own.

Related

  • AI-Powered Knowledge: LlamaIndex and Apache Tika for Enterprises

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook