1

Тема: Lucene, импорт данных из MySql

Здравствуйте, нашёл пример индексации простого файла в Lucene

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import org.apache.lucene.document.*;
import org.apache.lucene.document.Field.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.index.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.Version;

/**
 *
 * @author mark
 */
public class SimpleFileIndexer {

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            throw new Exception("Usage: java " + SimpleFileIndexer.class.getName());
        }
        File indexDir = new File(args[0]);
        File dataDir = new File(args[1]);
        long start = new Date().getTime();
        int numIndexed = index(indexDir, dataDir);
        long end = new Date().getTime();
        System.out.println("Indexing " + numIndexed + " files took "
                + (end - start) + " milliseconds");
    }

    public static int index(File indexDir, File dataDir)
            throws IOException {

        if (!dataDir.exists() || !dataDir.isDirectory()) {
            throw new IOException(dataDir
                    + " does not exist or is not a directory");
        }
        SimpleFSDirectory fsDir = new SimpleFSDirectory(indexDir);
        IndexWriter writer = new IndexWriter(fsDir, new StandardAnalyzer(Version.LUCENE_30), true, new IndexWriter.MaxFieldLength(200));
        writer.setUseCompoundFile(false);
        indexDirectory(writer, dataDir);
        int numIndexed = writer.numDocs();
        writer.optimize();
        writer.close();
        return numIndexed;
    }

    private static void indexDirectory(IndexWriter writer, File dir)
            throws IOException {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory()) {
                indexDirectory(writer, f);
            } else if (f.getName().endsWith(".txt")) {
                indexFile(writer, f);
            }
        }
    }

    private static void indexFile(IndexWriter writer, File f)
            throws IOException {
        if (f.isHidden() || !f.exists() || !f.canRead()) {
            return;
        }
        System.out.println("Indexing " + f.getCanonicalPath());
        Document doc = new Document();
        doc.add(new Field("contents", new FileReader(f)));
        doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
        writer.addDocument(doc);
    }
}

но не могу найти как тоже самое сделать с MySql, может кто нибудь помочь?

Отредактировано zhazhah (2011-06-16 21:02)