弹性搜索批量索引Java API无法正常工作

时间:2016-09-24 18:01:10

标签: java elasticsearch bulk

我正在使用Elastic search Java API 2.4开发批量插入 这是代码

    Client client = null;
    try
    {
        Settings settings = Settings.settingsBuilder().put("cluster.name", CLUSTER_NAME)
                .put("path.home", ELASTICSEARCH_PATH).build();
        client = NodeBuilder.nodeBuilder().settings(settings).node().client();
        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = CsvSchema.builder()
                .addColumn("code", CsvSchema.ColumnType.STRING)
                .addColumn("category", CsvSchema.ColumnType.STRING)
                .addColumn("name", CsvSchema.ColumnType.STRING)
                .addColumn("quantity", CsvSchema.ColumnType.NUMBER)
                .build()
                .withHeader();
        MappingIterator<OnlineProduct> onlineProductIter = csvMapper.readerFor(OnlineProduct.class).with(csvSchema)
                .readValues(new FileReader(sources[0]));
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        while (onlineProductIter.hasNext())
        {
            OnlineProduct onlineProduct = onlineProductIter.nextValue();
            XContentBuilder json = jsonBuilder()
                    .startObject()
                    .field("code", onlineProduct.getCode())
                    .field("category", onlineProduct.getCategory())
                    .field("name", onlineProduct.getName())
                    .field("quantity", onlineProduct.getQuantity())
                    .endObject();
            bulkRequest.add(client.prepareIndex("onlineshop", "product")
                    .setSource(json));
        }
        BulkResponse bulkResponse = bulkRequest.execute().actionGet();

BulkResponse没有失败,但每个BulkItemResnonse都包含此堆栈跟踪

IndexResponse[index=onlineshop,type=product,id=AVddUhuw-JSq2gJmvjpI,version=1,created=true,shards=Error building toString out of XContent: com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1886)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:235)
at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:152)
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:264)
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:255)
at org.elasticsearch.common.xcontent.XContentBuilder.startObject(XContentBuilder.java:169)
at org.elasticsearch.action.ActionWriteResponse$ShardInfo.toXContent(ActionWriteResponse.java:147)
at org.elasticsearch.common.Strings.toString(Strings.java:1103)
at org.elasticsearch.common.Strings.toString(Strings.java:1089)
at org.elasticsearch.action.ActionWriteResponse$ShardInfo.toString(ActionWriteResponse.java:164)
at java.lang.String.valueOf(String.java:2982)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at org.elasticsearch.action.index.IndexResponse.toString(IndexResponse.java:118)
at com.hophman.onlineshop.behavior.impex.DefaultProductImporter.importCSV(DefaultProductImporter.java:69)
at OnlineShopApplication.main(OnlineShopApplication.java:13)

有没有人知道发生了什么?

1 个答案:

答案 0 :(得分:2)

尝试从弹性搜索依赖项中排除<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId>。然后使用2.7.8版本单独添加jackson-core依赖项

我的pom.xml看起来:

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.4.1</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.8</version>
    </dependency>

希望这可以帮助你:)