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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Determining a File Type In Java

Trending

  • Navigating Double and Triple Extortion Tactics
  • Designing for Sustainability: The Rise of Green Software
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  • Optimizing Software Performance for High-Impact Asset Management Systems

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 user avatar
Ali ZHAGPAROV
·
Sep. 18, 21 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
13.9K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Determining a File Type In Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!