在Java中映射来自不同流结果集(不同数据库结果)的DTO对象的所有字段

问题描述 投票:0回答:1

我有一个 ItemDTO 对象,其中一些字段有来自 BigQuery 数据库的数据,但很少有来自 DB2 数据库的数据。我想使用streams检索数据并合并到DTO对象,但它不符合预期。

BigQuery 表包含的数据为

DB2 表数据

我使用流来执行相同的操作:

public static class ItemDTO {

        String itemNumber;
        String itemName;
        int postcode;
        String location;
        String price;

        //getter & setters 
    }

List<ItemDTO> bigQueryResults = //resultset from BigQueryDB
/*Retrieved bigquery results: [{
"itemNumber":"101"
"Name":"chair"
"Postcode":"4513"
"Location":null
"Price":null
},
{
"itemNumber":"102"
"Name":"Table"
"Postcode":"2341"
"Location":null
"Price":null
}]*/


List<ItemDTO> db2Results = //resultset from DB2
/*Retrieved db2 results: [{
"itemNumber":"102"
"Name":null
"Postcode":null
"Location":"New York"
"Price":"120"
},
{
"itemNumber":"101"
"Name":null
"Postcode":null
"Location":"Amsterdam"
"Price":"250"
}]*/

// retrieving a list of combined results from both db2 and BQ with key as itemNumber
 List<ItemDTO> resultList = db2List.stream()
                .filter(a -> bqList.stream().anyMatch(item -> item.getItem_nbr().equals(a.getItem_nbr())))
                .collect(Collectors.toList());

但是当我使用它时,我得到的行仅包含 BigQuery 字段的映射值。 DB2 中存在的字段填充为空。 请让我知道是否有其他方法来处理这种映射。任何建议将不胜感激。

/* 预期输出:[{ “商品编号”:“101” “名称”:“椅子” “邮政编码”:“4513” “地点”:“阿姆斯特丹” “价格”:“250” }, { “商品编号”:“102” “名称”:“表” “邮政编码”:“2341” “地点”:“纽约” “价格”:“120” }]*/

/* 结果列表输出:[{ “商品编号”:“101” “名称”:“椅子” “邮政编码”:“4513” “位置”:空 “价格”:空 }, { “商品编号”:“102” “名称”:“表” “邮政编码”:“2341” “位置”:空 “价格”:空 }]*/

java
1个回答
0
投票

这将合并具有相同 ItemNumber 的所有部分数据库提取。

  • 这首先使用项目编号作为键从
    bigq
    创建一个地图
  • 然后通过收集,然后进行映射、流式处理的后处理
    dbq
  • 对于具有相同 ID 的所有商品,位置和价格均从
    dbq
    商品设置。

打印输出是基于

toString()
类的
ItemDTO
显示的,此处未显示。

List<ItemDTO> bigQueryResults = List.of(
        new ItemDTO("101", "chair", 4513, "", ""),
        new ItemDTO("102", "Table", 2341, "", ""),
        new ItemDTO("103", "Television", 5607, "", ""),
        new ItemDTO("104", "Microwave", 2378, "", ""),
        new ItemDTO("105", "Sofa", 1278, "", ""));

List<ItemDTO> dbQueryResults = List.of(
        new ItemDTO("102", "", 0, "New York", "120"),
        new ItemDTO("106", "", 0, "Budapest", "300"),
        new ItemDTO("101", "", 0, "Amsterdam", "250)"));

// retrieving the db2 results for itemNumbers from bigQueryResults
List<ItemDTO> mergedResult = bigQueryResults.stream()
        .collect(Collectors.collectingAndThen(
           Collectors.groupingBy(ItemDTO::getItemNumber),
           mp -> dbQueryResults.stream().<ItemDTO>mapMulti((item, consumer) -> {
               List<ItemDTO> sameNumberList = mp.get(item.getItemNumber());
               if (sameNumberList != null) { // item number may not be
                                             // in map  
                  for (ItemDTO i : sameNumberList) {
                    i.setLocation(item.getLocation());
                    i.setPrice(item.getPrice());
                    consumer.accept(i); // put updated item on stream
                  }
               }
          }))).toList();

mergedResult.forEach(System.out::println);

为提供的数据打印以下内容

102, Table, 2341, New York, 120
101, chair, 4513, Amsterdam, 250)

    
© www.soinside.com 2019 - 2024. All rights reserved.