【发布时间】:2015-04-03 20:43:42
【问题描述】:
我有以下用于索引的 Lucene 代码,当我使用 100 万条记录运行此代码时 - 它运行速度很快(在 15 秒内建立索引(本地和服务器都具有高配置))。
当我尝试索引 2000 万条记录时,完成索引大约需要 10 分钟。
我在超过 100 GB RAM 的 Linux 服务器上运行这 2000 万条记录。在这种情况下,设置更多的 RAM 缓冲区大小会有所帮助吗?如果是的话,在我的情况下可以设置多少 RAM 大小(我的 RAM 超过 100 GB)
我在本地机器(8 GB RAM)上尝试了相同的 2000 万条记录,花费了相同的 10 分钟,我尝试在本地设置 1 GB RAM 缓冲区大小相同 10 分钟,而没有设置任何 RAM 缓冲区也相同 10 分钟在我的本地机器中记录了 2000 万条记录。
我尝试在linux中不设置RAM缓冲区大小,2000万条记录大约需要8分钟。
final File docDir = new File(docsPath.getFile().getAbsolutePath());
LOG.info("Indexing to directory '" + indexPath + "'...");
Directory dir = FSDirectory.open(new File(indexPath.getFile().getAbsolutePath()));
Analyzer analyzer = null;
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
iwc.setRAMBufferSizeMB(512.0);
IndexWriter indexWriter = new IndexWriter(dir, iwc);
if (docDir.canRead()) {
if (docDir.isDirectory()) {
String[] files = docDir.list();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = new File(docDir, files[i]);
String filePath = file.getPath();
String delimiter = BatchUtil.getProperty("file.delimiter");
if (filePath.indexOf("ecid") != -1) {
indexEcidFile(indexWriter, file, delimiter);
} else if (filePath.indexOf("entity") != -1) {
indexEntityFile(indexWriter, file, delimiter);
}
}
}
}
}
indexWriter.forceMerge(2);
indexWriter.close();
以及用于索引的方法之一:
private void indexEntityFile(IndexWriter writer, File file, String delimiter) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
Document doc = new Document();
Field four_pk_Field = new StringField("four_pk", "", Field.Store.NO);
doc.add(four_pk_Field);
Field cust_grp_cd_Field = new StoredField("cust_grp_cd", "");
Field cust_grp_mbrp_id_Field = new StoredField("cust_grp_mbrp_id", "");
doc.add(cust_grp_cd_Field);
doc.add(cust_grp_mbrp_id_Field);
String line = null;
while ((line = br.readLine()) != null) {
String[] lineTokens = line.split("\\" + delimiter);
four_pk_Field.setStringValue(four_pk);
String cust_grp_cd = lineTokens[4];
cust_grp_cd_Field.setStringValue(cust_grp_cd);
String cust_grp_mbrp_id = lineTokens[5];
cust_grp_mbrp_id_Field.setStringValue(cust_grp_mbrp_id);
writer.addDocument(doc);
}
br.close();
} catch (FileNotFoundException fnfe) {
LOG.error("", fnfe);
} catch (IOException ioe) {
LOG.error("", ioe);
} finally {
try {
fis.close();
} catch (IOException e) {
LOG.error("", e);
}
}
}
有什么想法吗?
【问题讨论】:
标签: java linux performance indexing lucene