亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何訪問流中的獲取器和設置器

如何訪問流中的獲取器和設置器

繁星coding 2022-09-28 14:37:46
我有一個方法,可以采用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());

}


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號