Mime Magic With Apache Tika

24
MIME Magic with MIME Magic with Apache Tika Apache Tika Jukka Zitting Jukka Zitting

description

Apache Tika aims to make it easier to extract metadata and structured text content from all kinds of files. Tika is a subproject of Apache Lucene, and leverages libraries like Apache POI and Apache PDFBox to provide a powerful yet simple interface for parsing dozens of document formats. This makes Tika an ideal companion for Apache Lucene, or for any search engine that needs to be able to index metadata and content from many different types of files. This presentation introduces Apache Tika and shows how it's being used in projects like Apache Solr and Apache Jackrabbit. You will learn how to integrate Tika with your application and how to configure and extend Tika to best suit your needs.

Transcript of Mime Magic With Apache Tika

Page 1: Mime Magic With Apache Tika

MIME Magic withMIME Magic withApache TikaApache Tika

Jukka ZittingJukka Zitting

Page 2: Mime Magic With Apache Tika
Page 3: Mime Magic With Apache Tika

YOU don't know about me without you have read a book by the name of The Adventures of Tom Sawyer; but that ain't no matter. That book was made by Mr. Mark Twain, and he told the truth, mainly. There was things which he stretched, but mainly he told the truth. That is nothing. I never seen anybody but lied one time or another, without it was Aunt Polly, or the widow, or maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and Mary, and the Widow Douglas is all told about in that book, which is mostly a true book, with some stretchers, as I said before…

3 May. Bistritz.--Left Munich at 8:35 P.M., on 1st May, arriving at Vienna early next morning; should have arrived at 6:46, but train was an hour late. Buda-Pesth seems a wonderful place, from the glimpse which I got of it from the train and the little I could walk through the streets. I feared to go very far from the station, as we had arrived late and would start as near the correct time as possible. The impression I had was that we were leaving the West and entering the East; the most western of splendid bridges over the Danube, which is here of noble width and depth, took us among the traditions of Turkish rule…

To Sherlock Holmes she is always THE woman. I have seldom heard him mention her under any other name. In his eyes she eclipses and predominates the whole of her sex. It was not that he felt any emotion akin to love for Irene Adler. All emotions, and that one particularly, were abhorrent to his cold, precise but admirably balanced mind. He was, I take it, the most perfect reasoning and observing machine that the world has seen, but as a lover he would have placed himself in a false position. He never spoke of the softer passions, save with a gibe and a sneer. They were admirable things for the observer--excellent for drawing the veil from men's motives and actions. But for the trained reasoner to admit such intrusions into his own delicate and finely adjusted temperament was to introduce a distracting factor which might throw a doubt upon all his mental results. Grit in a sensitive instrument, or a crack in one of his own high-power lenses, would not be more disturbing than a strong emotion in a nature such as his. And yet there was but one woman to him, and that woman was the late Irene Adler, of dubious and questionable memory…

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her…

It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife. However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of some one or other of their daughters. "My dear Mr. Bennet," said his lady to him one day, "have you heard that Netherfield Park is let at last?" Mr. Bennet replied that he had not. "But it is," returned she; "for Mrs. Long has just been here, and she told me all about it." Mr. Bennet made no answer…

Page 4: Mime Magic With Apache Tika

TypeType

TextText

MetadataMetadata

Page 5: Mime Magic With Apache Tika

import org.apache.tika.Tika;

unreleased code alertunreleased code alert

Page 6: Mime Magic With Apache Tika

// Type detection

String type = Tika.detect(InputStream);

String type = Tika.detect(File);

String type = Tika.detect(URL);

String type = Tika.detect(String);

Page 7: Mime Magic With Apache Tika

// Text extraction

String content = Tika.parseToString(InputStream);

String content = Tika.parseToString(File);

String content = Tika.parseToString(URL);

Page 8: Mime Magic With Apache Tika

// Streaming text extraction

Reader reader = Tika.parse(InputStream);

Reader reader = Tika.parse(File);

Reader reader = Tika.parse(URL);

Page 9: Mime Magic With Apache Tika

// … with metadata

String type = Tika.detect(InputStream, Metadata);

String content = Tika.parseToString( InputStream, Metadata);

Reader reader = Tika.parse(InputStream, Metadata);

Page 10: Mime Magic With Apache Tika

How does the magic work?

Page 11: Mime Magic With Apache Tika

// Metadata handling

import org.apache.tika.metadata.Metadata;

Metadata metadata = new Metadata();

metadata.set( Metadata.RESOURCE_NAME_KEY, “…”);

String title = metadata.get(Metadata.TITLE);

XMP?

Page 12: Mime Magic With Apache Tika

// Type detection and the type registry

import org.apache.tika.detect.Detector;

Detector detector = TikaConfig.getDefaultConfig() .getMimeTypeRepository();

MediaType type = detector.detect( InputStream, Metadata);

// 1274 known types, 627 detectable// (927 globs, 280 magic byte patterns)

Page 13: Mime Magic With Apache Tika

// The parser API

import org.apache.tika.parser.Parser;

Parser parser = new AutoDetectParser();

parser.parse( InputStream, ContentHandler, Metadata, Map<String, Object>);// IOException, SAXException, TikaException

// asm, audio, epub, html, image, mbox,// ms, mp3, odf, pdf, pkg, rtf, txt, xml

Page 14: Mime Magic With Apache Tika

Parser API in detail

Page 15: Mime Magic With Apache Tika

// Document input stream

InputStream stream = …;try { parser.parse(stream, …);} finally { stream.close(); // Remember!}

// Streaming if possible, otherwise a// temporary file is automatically used

Page 16: Mime Magic With Apache Tika

// XHTML SAX events for structured text

ContentHandler handler = new BodyContentHandler(…); // etc.try { parser.parse(…, handler, …);} catch (SAXException e) { …;}

// <html>// <head>…<title>…</title>…</head>// <body>…</body>// </html>

Page 17: Mime Magic With Apache Tika

// Input and output metadata

Metadata metadata = new Metadata();metadata.set( Metadata.RESOURCE_NAME_KEY, “…”);

parser.parse(…, metadata, …);

String title = metadata.get(Metadata.TITLE);

// See parser implementations for// supported metadata keys (TODO: XMP, DC)

Page 18: Mime Magic With Apache Tika

// Parse context

Map<String, Object> context = new HashMap<String, Object>();context.put( Parser.class.getName(), new MyCustomComponentParser()); // hierarchy while streaming!

parser.parse(…, context);

// Also for Locale, XML parser, etc.

Page 19: Mime Magic With Apache Tika

Putting it all together

Page 20: Mime Magic With Apache Tika

// Tika components

tika-core - API/core classes, no depstika-parsers - parser librariestika-app - all in one jar + cli/gui

$ java –jar tika-app-0.x.jar < …$ java –jar tika-app-0.x.jar --text$ java –jar tika-app-0.x.jar --metadata$ java –jar tika-app-0.x.jar --help

Page 21: Mime Magic With Apache Tika

// Tika GUI

$ java –jar tika-app-0.x.jar --gui

Page 22: Mime Magic With Apache Tika

// Customizing Tika

org/apache/tika/tika-config.xmlorg/apache/tika/mime/tika-mimetypes.xml

// … or add custom components at runtime

org.apache.tika.detect.CompositeDetectororg.apache.tika.parser.CompositeParser

// … or submit a patch!

// In the future: OSGi support?

Page 23: Mime Magic With Apache Tika

// Tika integration

Lucene Javadocument.add( new Field(“content”, Tika.parse(…)); // see also the ParsingReader class

Solr Cell, in Solr 1.4 http://wiki.apache.org/solr/ExtractingRequestHandler

TODO: Nutch, ports (but see Tika cli)

Page 24: Mime Magic With Apache Tika

Questions?http://lucene.apache.org/tika/