elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

Java ElasticSearch中的内存不足

Java ElasticSearch中的内存不足,java,elasticsearch,Java,elasticsearch,我试图索引ES中的一些数据,但收到内存不足异常: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at org.elasticsearch.common.jackson.core.util.BufferRecycler.balloc(BufferRecycler.java:155) at org.elasticsearch.common.jackson.core.util.BufferR

我试图索引ES中的一些数据,但收到内存不足异常:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at org.elasticsearch.common.jackson.core.util.BufferRecycler.balloc(BufferRecycler.java:155)
    at org.elasticsearch.common.jackson.core.util.BufferRecycler.allocByteBuffer(BufferRecycler.java:96)
    at org.elasticsearch.common.jackson.core.util.BufferRecycler.allocByteBuffer(BufferRecycler.java:86)
    at org.elasticsearch.common.jackson.core.io.IOContext.allocWriteEncodingBuffer(IOContext.java:152)
    at org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.<init>(UTF8JsonGenerator.java:123)
    at org.elasticsearch.common.jackson.core.JsonFactory._createUTF8Generator(JsonFactory.java:1284)
    at org.elasticsearch.common.jackson.core.JsonFactory.createGenerator(JsonFactory.java:1016)
    at org.elasticsearch.common.xcontent.json.JsonXContent.createGenerator(JsonXContent.java:68)
    at org.elasticsearch.common.xcontent.XContentBuilder.<init>(XContentBuilder.java:96)
    at org.elasticsearch.common.xcontent.XContentBuilder.builder(XContentBuilder.java:77)
    at org.elasticsearch.common.xcontent.json.JsonXContent.contentBuilder(JsonXContent.java:38)
    at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:122)
    at org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder(XContentFactory.java:49)
    at EsController.importProductEs(EsController.java:60)
    at Parser.fromCsvToJson(Parser.java:120)
    at CsvToJsonParser.parseProductFeeds(CsvToJsonParser.java:43)
    at MainParser.main(MainParser.java:49)
然后我运行批量请求:

// for each product in the list, we need to include the fields in the bulk request 
for(HashMap<String, String> productfields : products)
        try {
            bulkRequest.add(client.prepareIndex(index,type,productfields.get("Product_Id"))
                    .setSource(jsonBuilder()
                                .startObject()
                                    .field("Name",productfields.get("Name") )
                                    .field("Quantity",productfields.get("Quantity"))
                                    .field("Make", productfields.get("Make"))
                                    .field("Price", productfields.get("Price"))
                                .endObject()
                              )
                    );                  

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//execute the bulk request
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
    // process failures by iterating through each bulk response item
}
//对于列表中的每个产品,我们需要在批量请求中包含字段
for(HashMap productfields:products)
试一试{
bulkRequest.add(client.prepareIndex(索引、类型、productfields.get(“产品Id”))
.setSource(jsonBuilder()
.startObject()
.field(“名称”,productfields.get(“名称”))
.field(“数量”,productfields.get(“数量”))
.field(“Make”,productfields.get(“Make”))
.field(“价格”,productfields.get(“价格”))
.endObject()
)
);                  
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//执行批量请求
BulkResponse BulkResponse=bulkRequest.execute().actionGet();
if(bulkResponse.hasFailures()){
//通过迭代每个批量响应项来处理失败
}
我正在尝试索引来自不同商店的产品。每个商店都有不同的索引。当我到达第六家商店,里面有大约60000种产品时,我得到了上述例外情况。我将批量请求分成10000个块,试图避免内存不足问题。 我不明白瓶颈到底在哪里。如果我以某种方式刷新批量请求或重新启动客户端,会有帮助吗?? 我也看到过类似的帖子,但对我来说不适用

编辑

当我每次处理一个新的批量请求时都实例化一个新的客户机时,我不会得到内存不足异常。但是每次实例化一个新客户机似乎都不对


谢谢

所以我发现了问题所在

每一个新的批量请求都与前一个请求相加,最终导致内存不足

现在,在启动新的批量请求之前,我运行
bulkRequest=client.prepareBulk()
刷新上一个请求


谢谢你们的评论

导入的数据是什么样子的?考虑到此错误发生在客户端,而不是实际的ES节点上,您可能一次处理了两个多产品。@9000-{“名称”:“无线形状鼠标”,“数量”:“100”,“制造”:“索尼”,“价格”:“23.73”}。这是一个产品系列。@Bax,我正在处理每个请求的10.000,我将尝试将其减少到5000。或者尝试通过-Xmx VM选项增加客户端内存。
// for each product in the list, we need to include the fields in the bulk request 
for(HashMap<String, String> productfields : products)
        try {
            bulkRequest.add(client.prepareIndex(index,type,productfields.get("Product_Id"))
                    .setSource(jsonBuilder()
                                .startObject()
                                    .field("Name",productfields.get("Name") )
                                    .field("Quantity",productfields.get("Quantity"))
                                    .field("Make", productfields.get("Make"))
                                    .field("Price", productfields.get("Price"))
                                .endObject()
                              )
                    );                  

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//execute the bulk request
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
    // process failures by iterating through each bulk response item
}