2016-09-24 95 views
0

我開發具有彈性搜索的Java API批量插入2.4 下面是代碼彈性搜索散貨指數的Java API不工作

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) 

]

有沒有人有一個想法是怎麼回事?

回答

2

嘗試從您的elasticsearch依賴項中排除<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId>。然後用版本另加傑克遜核心依賴2.7.8

我的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> 

希望這有助於你:)