基于HBasene(HBase+Lucene)搭建搜索应用例子

https://github.com/akkumar/hbasene/wiki/hello-world
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;

public class HelloWorldClass {

  public static void main(final String[] args) throws IOException {
    final String indexName = "myindex";
    Configuration conf = HBaseConfiguration.create(); //hbase-site.xml in the classpath
      //with hbase.zookeeper.quorum, property configured.
    HBaseIndexStore.createLuceneIndexTable(indexName, conf, true);

    HTablePool tablePool = new HTablePool(conf, 10);

    //Write
    HBaseIndexStore hbaseIndex = new HBaseIndexStore(tablePool, indexName);
    HBaseIndexWriter writer = new HBaseIndexWriter(hbaseIndex, "id"); //Name of the primary key field.

    Document doc = getDocument();
    writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));

    //Read/Search
    IndexReader reader = new HBaseIndexReader(tablePool, indexName);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs docs = searcher.search(new TermQuery(new Term("content", "plays")),
        3);
    searcher.close();

  }


  private static Document getDocument() { 
    Document doc = new Document();
    doc.add(new Field("content", "some content", Field.Store.NO,
        Field.Index.ANALYZED_NO_NORMS));
    doc.add(new Field("id", "some id", Field.Store.YES, Field.Index.NO));
    return doc;

  }
}

你可能感兴趣的:(Lucene)