- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个目录中有两个大文件(约 200 MB),想在它们上建立索引,所以这是我的代码:
public class LuceneUtil {
private void indexDoc(IndexWriter indexWriter, Path file, long lastModified) throws IOException{
try (InputStream stream = Files.newInputStream(file)) {
Document document = new Document();
Field pathField = new StringField("path", file.toString(), Field.Store.YES);
document.add(pathField);
document.add(new LongField("modified", lastModified, Field.Store.NO));
document.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE_OR_APPEND) {
// new index
indexWriter.addDocument(document);
} else {
// update existing index
indexWriter.updateDocument(new Term("path", file.toString()), document);
}
}
}
private void indexDocs(final IndexWriter indexWriter, Path path) throws ExecutionException, InterruptedException, IOException {
if (Files.isDirectory(path)) {
ForkJoinPool FJ_POOL = new ForkJoinPool(3);
List<Path> files = FSUtils.findAllFiles(path.toString());
FJ_POOL.submit(() -> files.parallelStream().forEach(t -> {
try {
indexDoc(indexWriter, t, FSUtils.getFileBasicAttribute(t).lastModifiedTime().toMillis());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
})).get();
FJ_POOL.shutdown();
// Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
// @Override
// public FileVisitResult visitFile (Path file, BasicFileAttributes attrs) throws IOException {
// try {
//
// indexDoc(indexWriter, file, attrs.lastModifiedTime().toMillis());
// } catch (IOException ex) {
// ex.printStackTrace();
// }
// return FileVisitResult.CONTINUE;
// }
// });
} else {
indexDoc(indexWriter, path, Files.getLastModifiedTime(path).toMillis());
}
}
public void buildIndex(String pathToDocsDir, String pathToIndexDir) throws ExecutionException, InterruptedException, IOException{
Path docPath = Paths.get(pathToDocsDir);
Path indexPath = Paths.get(pathToIndexDir);
long start = System.currentTimeMillis();
try(Directory dir = FSDirectory.open(indexPath.toFile());
Analyzer analyzer = new StandardAnalyzer()) {
IndexWriterConfig iwc = new IndexWriterConfig(Version.LATEST, analyzer);
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
try (IndexWriter indexWriter = new IndexWriter(dir, iwc)) {
indexDocs(indexWriter, docPath);
}
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException{
LuceneUtils luceneUtils = new LuceneUtils();
String docPath = "/home/TestFolder";
String indexPath = "/home/IndexFolder";
try {
luceneUtils.buildIndex(docPath, indexPath);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
因此,从我的代码中您可以看到,我对两个文件使用一个 IndexWriter
对象,并尝试并行构建索引文件。几分钟后,当我的程序启动时,我收到下一个异常:
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1006) at com.service.utils.LuceneUtils.indexDocs(LuceneUtils.java:70) at com.service.utils.LuceneUtils.buildIndex(LuceneUtils.java:100) at com.service.utils.LuceneUtils.main(LuceneUtils.java:138) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: java.lang.OutOfMemoryError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:598) at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1005)
是否可以在并行模式下使用一个IndexWriter
?我该如何解决我的问题?
最佳答案
Lucene 有一个很好的并行索引过程的功能。如果您已在 RAMDirectory 或 FSDirectory 中对文件建立索引,则可以将它们合并到一个索引中。您必须使用addIndexes准备和使用forceMerge来完成合并。因此,您可以将文件分成单独的部分,并行索引它们,最后合并它们。
关于java - Lucene IndexWriter OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446908/
我正在调查我们的应用程序运行缓慢的问题,最终导致集群环境中的一个实例出现故障。几周前我遇到了以下错误: [#|2012-05-11T14:12:03.460-0400|SEVERE|sun-appse
我创建了一个应用程序,基本上使用机器人在客户端获取图像并每隔几秒发送一次到服务器,这样我就可以看到另一台 PC 上发生了什么。问题似乎是它一直将图像保存在数组或其他东西中,因为几秒钟后,它崩溃了。我只
我正在使用 universal-image-loader-1.6.2.jar(最新的)。我正在尝试使用此库下载并缓存该图像。我要从服务器下载 47 张图片,总共 5.22 Mb。我的最大图片尺寸为 7
我在玩 Scala 的惰性迭代器,但遇到了一个问题。我想要做的是读取一个大文件,进行转换,然后写出结果: object FileProcessor { def main(args: Array[S
当涉及到服务器环境的垃圾收集/内存限制时,.Net 框架的行为是否可能有所不同?我在具有 32gbs 物理内存的 64 位服务器计算机上显式运行 x86 编译的应用程序,并且内存不足(SystemOu
在我的应用程序启动时,我正在创建具有 75*10^6 容量的长哈希集。 Profiler 显示,该应用程序使用 1.4g。我尝试设置-Xmx1600m,但发现内存不足。 -Xmx2000m 相同。 -
我有一项关于使用数字列表构建金字塔的任务,但一项测试存在一个问题。在我的任务中,我需要对列表进行排序。我使用 Collections.sort(): Collections.sort(inputNum
在对我为 Windows Mobile 编写的类库进行一些最终测试时(使用 Compact Net Framework 2.0),我遇到了 OOM 异常。 基本上,我的库首先加载一个字典文件(一个带有
编辑:我将其重新表述为问题并将答案移至答案部分... 在一个相对复杂的多线程 .NET 应用程序中,我遇到了 OutOfMemoryException,即使在我认为没有理由的情况下也是如此。 情况:
当我尝试使用下面的方法将一些文本内容设置到我们心爱的窗口的剪贴板时 片段,它在 10-15MB 的范围内工作正常。但是超过这个大小,它会抛出一条错误消息 显示在最后。我们如何将 30+ MB 的 ut
我想在 Android 中将 PDF 图像发送到服务器。 服务器规范需要我应该使用 Base64 编码。 所以我应该将 PDF 图像文件转换为 Base64 字符串。 下面是 HTTP POST 请求
我正在解析设备上的二进制文件并将我关心的字段存储在数组中。这些文件可以生成大小为 100,000 的数组。自然地,java 告诉我内存不足(我认为 android 每个应用程序只允许 16MB)。 还
我正在尝试从一个 txt 文件(书籍)中读取,然后将它的每一行添加到一个链表中。但是,当我运行代码时,我在 l.add(line); 处遇到内存不足错误。你能告诉我这段代码做错了什么吗?或者,是否有更
我通过允许用户选择要显示的图片从 SD 卡加载位图。创建位图后,我在 ImageView 中设置位图: mBitmap = Bitmap.createBitmap(Media.getBitmap(th
我试图在 gridview 中显示很多图像。有两个 Activity 。它们都有 gridviews,其中有图像。当我只启动其中一个时。没有问题,但是当我启动另一个时,存在“outifmemory”问
我需要在我的应用程序中显示许多图像。这些是 jpg 和 png,我将它们加载到 ImageView 中,如下所示: tile.setImageResource(R.drawable.tile_high
我正在开发一个远程备份应用程序,有时我需要上传大文件,例如 15 MB,我在一些手机上测试过我遇到内存不足的错误 有没有办法使用这个函数来使用更少的内存? public int uploadFile(
我在模拟器和设备(acer 平板电脑和三星 galaxy)上运行我的项目时出现 OutOfMemory 异常,显示 the application launcher process com.andro
我有一个程序可以为目录(子)树中的每个文件创建一个对象。在磁盘越来越大的今天,没有办法知道会有多少文件,尤其是。不是几年(几个月?)从现在开始。 我的程序不是企业关键的;它是用户分析该子树的工具。所以
我已经使用以下命令从命令行启动了我的jar java -Xms1200m -Xmx1500m -jar xxx.jar 我正在创建大小为 12600 * 12600 的 BufferedImage 意
我是一名优秀的程序员,十分优秀!