将通量排放与重复合并?

时间:2018-10-26 11:25:55

标签: project-reactor

我有一个Flux可以发射物品:

data class Item(
  val isEgg: Boolean,
  val isBasket: Boolean
)

考虑2个“篮子”和4个“鸡蛋”的排放量。我想将这些排放合并为两个:每个排放包含一个“篮子”和四个“鸡蛋”:

Flux emissions and it's transformation

有人知道这样的转变吗?通量是有限的,不能超过1K个项目。

编辑:

到目前为止,我取得了什么成就-我将排放量分为GroupedFlux。现在,我需要将包含GroupedFluxBasket1的{​​{1}}与包含第二个“ Eggs”的第二个相结合,以产生两个篮子,每个篮子中都有“重复的”鸡蛋。

Basket2

期望的通量: val flux = Flux.just("Egg1", "Egg2", "Basket1", "Egg3", "Egg4", "Basket2") val block = flux.groupBy { it.startsWith("Egg") }

2 个答案:

答案 0 :(得分:3)

您可以使用flatMapreduce来获得此结果:

void combine() {
  Flux<String> flux = 
    Flux.just("Egg1", "Egg2", "Basket1", "Egg3", "Egg4", "Basket2");
  Flux<String> eggs = flux.filter(str -> str.startsWith("Egg"));
  Flux<String> basketNames = flux.filter(str -> str.startsWith("Basket"));
  basketNames.flatMap(basketName -> eggs.reduce(
      new Basket(basketName),
      (basket, egg) -> {
        basket.add(egg);
        return basket;
      })
  );
}

class Basket {

  private final String name;
  private final List<String> eggs;

  Basket(final String name) {
    this.name = name;
    this.eggs = new ArrayList<>();
  }

  public void add(String egg) {
    eggs.add(egg);
  }
}

答案 1 :(得分:0)

您的问题不是很清楚。我要说的是检出merge()和concat()运算符。那应该有帮助。

编辑: 根据提供的其他详细信息,问题现在很清楚。 下面给出了一种可能的解决方案:

@Test
public void testBasket() {

    Egg eggA = new Egg("A");
    Egg eggB = new Egg("B");
    Egg eggC = new Egg("C");
    Egg eggD = new Egg("D");

    Basket basket1 = new Basket("basket1");
    Basket basket2 = new Basket("basket2");

    Sorter sorter = new Sorter();
    Sorter updatedSorter = Flux .just((Item) basket1, (Item) basket2, (Item) eggA, (Item) eggB, (Item) eggC,
            (Item) eggD)
                                .map(sorter::add)
                                .blockLast();

    updatedSorter.process();

    Flux<Basket> fluxBasket = Flux.fromStream(sorter.baskets.stream());

    fluxBasket.subscribe(d -> System.out.println("data:" + d));

}

class Sorter {
    List<Egg> eggs = new ArrayList<Egg>();
    List<Basket> baskets = new ArrayList<Basket>();

    public Sorter add(Item item) {
        if (item.isBasket)
            baskets.add((Basket) item);
        else
            eggs.add((Egg) item);

        return this;
    }

    public Sorter process() {
        System.out.println("---- Processing Eggs ----");

        for (Basket basket : baskets) {
            basket.addEggs(eggs);
        }

        System.out.println("---- Processing Done ----");

        System.out.println(this.toString());

        return this;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        for (Basket basket : baskets) {
            sb.append(basket.toString() + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");
        return sb.toString();
    }

}

class Item {
    boolean isEgg;
    boolean isBasket;
}

class Basket extends Item {
    public Basket(String name) {
        this.name = name;
        isBasket = true;
    }

    String name;

    List<Egg> eggs = new ArrayList<Egg>();

    public void addEggs(List<Egg> eggs) {
        this.eggs = eggs;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(this.name);
        sb.append("(");
        for (Egg egg : eggs) {
            sb.append(egg.toString() + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");
        return sb.toString();
    }
}

class Egg extends Item {
    public Egg(String name) {
        this.name = name;
        isEgg = true;
    }

    String name;

    public String toString() {
        return this.name;
    }
}

输出:

---- Processing Eggs ----
---- Processing Done ----
(basket1(A,B,C,D),basket2(A,B,C,D))
data:basket1(A,B,C,D)
data:basket2(A,B,C,D)

Edit2:

另一种解决方案,没有阻止呼叫:

@测试     公共无效testBasket(){

    Egg eggA = new Egg("A");
    Egg eggB = new Egg("B");
    Egg eggC = new Egg("C");
    Egg eggD = new Egg("D");

    Basket basket1 = new Basket("basket1");
    Basket basket2 = new Basket("basket2");

    Sorter sorter = new Sorter();
    Mono<Sorter> bucketsMono = Flux .just((Item) basket1, (Item) basket2, (Item) eggA, (Item) eggB, (Item) eggC,
            (Item) eggD)
                                    .map(sorter::add)
                                    .reduce((sorter1, sorter2) -> sorter.process());

    bucketsMono.subscribe(d -> System.out.println("data:" + d));


}

输出:

(basket1),basket2))
---- Processing Eggs ----
---- Processing Done ----
(basket1(A),basket2(A))
---- Processing Eggs ----
---- Processing Done ----
(basket1(A,B),basket2(A,B))
---- Processing Eggs ----
---- Processing Done ----
(basket1(A,B,C),basket2(A,B,C))
---- Processing Eggs ----
---- Processing Done ----
(basket1(A,B,C,D),basket2(A,B,C,D))
data:(basket1(A,B,C,D),basket2(A,B,C,D))