我有一个方法,可以采用json对象并将其插入到aws弹性搜索服务中,但我想编写另一个方法,该方法采用多个json对象并将其插入到aws弹性搜索中/** * Create the Product in ElasticSearch * * @param product The Product * @return The response string * @throws JsonProcessingException Throws JsonProcessingException when response cannot be parsed */public String createNewProduct(Product product) throws JsonProcessingException { final ObjectMapper objectMapper = new ObjectMapper(); final String json = objectMapper.writeValueAsString(product); if (json != null) { AwsResponse response = createDocument(ElasticSearchConstants.PRODUCTS_INDEX, ElasticSearchConstants.PRODUCTS_DOCUMENT_TYPE, json, product.getPartNo()); // Creating a new document not seen before results in a 201 status, where as overwriting a previous document results in a 200 if (response != null && (response.getHttpResponse().getStatusCode() == HttpStatus.CREATED.value() || response.getHttpResponse().getStatusCode() == HttpStatus.OK.value())) { LOGGER.info("Successfully created new Product", product.getPartNo(), product.getLevelOne()); return product.getPartNo(); } } return null;}/** * Create new Products in ElasticSearch * @param products The product * @throws JsonProcessingException Throws JsonProcessingException when response cannot be parsed */public String createNewProducts(ArrayList<Product> products) throws JsonProcessingException{ final ObjectMapper objectMapper = new ObjectMapper(); products.stream() .map(product -> { try { return objectMapper.writeValueAsString(product); } catch (JsonProcessingException e) { return new JsonProcessingException(e){}; } })如您所见,createDocument方法需要4个参数,其中4个是字符串 第一种方法工作得很好,但第二种方法有问题。当我试图得到partNo's时,它给了我一个错误,因为正常的“void不是功能性界面”,我怎么能让它工作。
1 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
您不需要该操作,a可以在您的案例中工作。在这种情况下,s的使用也不是非常有效。您可以简单地编写一个循环并执行以下操作:mapforEachstreamfor
for (Product product : products) {
String json = objectMapper.writeValueAsString(product);
// handle the exception as well above
createDocument(ElasticSearchConstants.PRODUCTS_INDEX,
ElasticSearchConstants.PRODUCTS_DOCUMENT_TYPE,
json, product.getPartNo());
}
添加回答
举报
0/150
提交
取消