Tika 简明教程

TIKA - Metadata Extraction

除了内容之外,Tika 还从文件中提取元数据。元数据只不过是随文件提供的附加信息。如果我们考虑一个音频文件,艺术家姓名、专辑名称、标题都属于元数据。

Besides content, Tika also extracts the metadata from a file. Metadata is nothing but the additional information supplied with a file. If we consider an audio file, the artist name, album name, title comes under metadata.

XMP Standards

可扩展元数据平台 (XMP) 是处理和存储与文件内容相关的信息的标准。它由 Adobe Systems Inc 创建。XMP 提供了用于定义、创建和处理 metadata 的标准。您可以将此标准嵌入到 PDFJPEGJPEGGIFjpgHTML 等多种文件格式中。

The Extensible Metadata Platform (XMP) is a standard for processing and storing information related to the content of a file. It was created by Adobe Systems Inc. XMP provides standards for defining, creating, and processing of metadata. You can embed this standard into several file formats such as PDF, JPEG, JPEG, GIF, jpg, HTML etc.

Property Class

Tika 使用 Property 类遵循 XMP 财产定义。它提供了 PropertyTypeValueType 枚举来捕获元数据的名称和值。

Tika uses the Property class to follow XMP property definition. It provides the PropertyType and ValueType enums to capture the name and value of a metadata.

Metadata Class

此类实现了 ClimateForcast 、CativeCommons、 Geographic 、TIFF 等各种接口,以提供对各种元数据模型的支持。此外,此类还提供了从文件中提取内容的各种方法。

This class implements various interfaces such as ClimateForcast, CativeCommons, Geographic, TIFF etc. to provide support for various metadata models. In addition, this class provides various methods to extract the content from a file.

Metadata Names

我们可以使用 names() 方法从文件的元数据对象中提取该文件所有元数据名称的列表。它以字符串数组的形式返回所有名称。使用元数据的名称,我们可以使用 get() 方法获取值。它获取一个元数据名称并返回与之关联的值。

We can extract the list of all metadata names of a file from its metadata object using the method names(). It returns all the names as a string array. Using the name of the metadata, we can get the value using the get() method. It takes a metadata name and returns a value associated with it.

String[] metadaNames = metadata.names();

String value = metadata.get(name);

Extracting Metadata using Parse Method

每当我们使用 parse() 解析文件时,我们都会将一个空元数据对象作为其中一个参数传递。此方法提取给定文件(如果该文件包含任何文件)的元数据,并将它们放置在元数据对象中。因此,在使用 parse() 解析文件后,我们可以从该对象中提取元数据。

Whenever we parse a file using parse(), we pass an empty metadata object as one of the parameters. This method extracts the metadata of the given file (if that file contains any), and places them in the metadata object. Therefore, after parsing the file using parse(), we can extract the metadata from that object.

Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();   //empty metadata object
FileInputStream inputstream = new FileInputStream(file);
ParseContext context = new ParseContext();
parser.parse(inputstream, handler, metadata, context);

// now this metadata object contains the extracted metadata of the given file.
metadata.metadata.names();

以下是从文本文件中提取元数据的完整程序。

Given below is the complete program to extract metadata from a text file.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;

import org.xml.sax.SAXException;

public class GetMetadata {

   public static void main(final String[] args) throws IOException, TikaException {

      //Assume that boy.jpg is in your current directory
      File file = new File("boy.jpg");

      //Parser method parameters
      Parser parser = new AutoDetectParser();
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputstream = new FileInputStream(file);
      ParseContext context = new ParseContext();

      parser.parse(inputstream, handler, metadata, context);
      System.out.println(handler.toString());

      //getting the list of all meta data elements
      String[] metadataNames = metadata.names();

      for(String name : metadataNames) {
         System.out.println(name + ": " + metadata.get(name));
      }
   }
}

将以上代码保存为 GetMetadata.java,并使用以下命令从命令提示符运行它 -

Save the above code as GetMetadata.java and run it from the command prompt using the following commands −

javac  GetMetadata .java
java  GetMetadata

以下是 boy.jpg 的快照。

Given below is the snapshot of boy.jpg

boy

如果您执行上述程序,它将为您提供以下输出 -

If you execute the above program, it will give you the following output −

X-Parsed-By: org.apache.tika.parser.DefaultParser
Resolution Units: inch
Compression Type: Baseline
Data Precision: 8 bits
Number of Components: 3
tiff:ImageLength: 3000
Component 2: Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Component 1: Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Image Height: 3000 pixels
X Resolution: 300 dots
Original Transmission Reference:
   53616c7465645f5f2368da84ca932841b336ac1a49edb1a93fae938b8db2cb3ec9cc4dc28d7383f1
Image Width: 4000 pixels
IPTC-NAA record: 92 bytes binary data
Component 3: Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
tiff:BitsPerSample: 8
Application Record Version: 4
tiff:ImageWidth: 4000
Content-Type: image/jpeg
Y Resolution: 300 dots

我们还可以获取我们需要的元数据值。

We can also get our desired metadata values.

Adding New Metadata Values

可以使用 metadata 类的 add() 方法添加新元数据值。以下是此方法的语法。此处我们正在添加作者姓名。

We can add new metadata values using the add() method of the metadata class. Given below is the syntax of this method. Here we are adding the author name.

metadata.add(“author”,”Tutorials point”);

Metadata 类具有预定义的属性,包括从 ClimateForcast 、CativeCommons、 Geographic 等类继承的属性,以支持各种数据模型。下面显示了从 Tika 实现的 TIFF 接口继承的 SOFTWARE 数据类型的用法,以便遵循针对 TIFF 图像格式的 XMP 元数据标准。

The Metadata class has predefined properties including the properties inherited from classes like ClimateForcast, CativeCommons, Geographic, etc., to support various data models. Shown below is the usage of the SOFTWARE data type inherited from the TIFF interface implemented by Tika to follow XMP metadata standards for TIFF image formats.

metadata.add(Metadata.SOFTWARE,"ms paint");

以下是演示如何向给定文件添加元数据值的完整程序。此处,元数据元素的列表显示在输出中,以便您可以在添加新值后观察列表的变化。

Given below is the complete program that demonstrates how to add metadata values to a given file. Here the list of the metadata elements is displayed in the output so that you can observe the change in the list after adding new values.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;

import org.xml.sax.SAXException;

public class AddMetadata {

   public static void main(final String[] args) throws IOException, SAXException, TikaException {

      //create a file object and assume sample.txt is in your current directory
      File file = new File("Example.txt");

      //Parser method parameters
      Parser parser = new AutoDetectParser();
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputstream = new FileInputStream(file);
      ParseContext context = new ParseContext();

      //parsing the document
      parser.parse(inputstream, handler, metadata, context);

      //list of meta data elements before adding new elements
      System.out.println( " metadata elements :"  +Arrays.toString(metadata.names()));

      //adding new meta data name value pair
      metadata.add("Author","Tutorials Point");
      System.out.println(" metadata name value pair is successfully added");

      //printing all the meta data elements after adding new elements
      System.out.println("Here is the list of all the metadata
         elements after adding new elements");
      System.out.println( Arrays.toString(metadata.names()));
   }
}

将以上代码保存为 AddMetadata.java 类,然后从命令提示符运行它 −

Save the above code as AddMetadata.java class and run it from the command prompt −

javac  AddMetadata .java
java  AddMetadata

以下是 Example.txt 的内容

Given below is the content of Example.txt

Hi students welcome to tutorialspoint

如果您执行上述程序,它将为您提供以下输出 -

If you execute the above program, it will give you the following output −

metadata elements of the given file :
[Content-Encoding, Content-Type]
enter the number of metadata name value pairs to be added 1
enter metadata1name:
Author enter metadata1value:
Tutorials point metadata name value pair is successfully added
Here is the list of all the metadata elements  after adding new elements
[Content-Encoding, Author, Content-Type]

Setting Values to Existing Metadata Elements

可以使用 set() 方法将值设为现有元数据元素。使用 set() 方法设置 date 属性的语法如下所示 −

You can set values to the existing metadata elements using the set() method. The syntax of setting the date property using the set() method is as follows −

metadata.set(Metadata.DATE, new Date());

您还可以使用 set() 方法向属性设置多个值。使用 set() 方法向 Author 属性设置多个值的语法如下所示 −

You can also set multiple values to the properties using the set() method. The syntax of setting multiple values to the Author property using the set() method is as follows −

metadata.set(Metadata.AUTHOR, "ram ,raheem ,robin ");

以下是演示 set() 方法的完整程序。

Given below is the complete program demonstrating the set() method.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.Date;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;

import org.xml.sax.SAXException;

public class SetMetadata {

   public static void main(final String[] args) throws IOException,SAXException, TikaException {

      //Create a file object and assume example.txt is in your current directory
      File file = new File("example.txt");

      //parameters of parse() method
      Parser parser = new AutoDetectParser();
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputstream = new FileInputStream(file);
      ParseContext context = new ParseContext();

      //Parsing the given file
      parser.parse(inputstream, handler, metadata, context);

      //list of meta data elements elements
      System.out.println( " metadata elements and values of the given file :");
      String[] metadataNamesb4 = metadata.names();

      for(String name : metadataNamesb4) {
    	  System.out.println(name + ": " + metadata.get(name));
      }

      //setting date meta data
      metadata.set(Metadata.DATE, new Date());

      //setting multiple values to author property
      metadata.set(Metadata.AUTHOR, "ram ,raheem ,robin ");

      //printing all the meta data elements with new elements
      System.out.println("List of all the metadata elements  after adding new elements ");
      String[] metadataNamesafter = metadata.names();

      for(String name : metadataNamesafter) {
         System.out.println(name + ": " + metadata.get(name));
      }
   }
}

将以上代码保存为 SetMetadata.java,然后从命令提示符运行它 −

Save the above code as SetMetadata.java and run it from the command prompt −

javac  SetMetadata.java
java  SetMetadata

以下是 example.txt 的内容。

Given below is the content of example.txt.

Hi students welcome to tutorialspoint

如果您执行上面的程序,它将为您提供以下输出。在输出中,您可以观察到新增的元数据元素。

If you execute the above program it will give you the following output. In the output, you can observe the newly added metadata elements.

metadata elements and values of the given file :
Content-Encoding: ISO-8859-1
Content-Type: text/plain; charset = ISO-8859-1
Here is the list of all the metadata elements  after adding new elements
date: 2014-09-24T07:01:32Z
Content-Encoding: ISO-8859-1
Author: ram, raheem, robin
Content-Type: text/plain; charset = ISO-8859-1