Search in sources :

Example 1 with CategoryPrice

use of org.apache.beam.sdk.nexmark.model.CategoryPrice in project beam by apache.

the class Query4Model method relevantResults.

@Override
protected Iterable<TimestampedValue<CategoryPrice>> relevantResults(Iterable<TimestampedValue<CategoryPrice>> results) {
    // Find the last (in processing time) reported average price for each category.
    Map<Long, TimestampedValue<CategoryPrice>> finalAverages = new TreeMap<>();
    for (TimestampedValue<CategoryPrice> obj : results) {
        Assert.assertTrue("have CategoryPrice", obj.getValue() instanceof CategoryPrice);
        CategoryPrice categoryPrice = (CategoryPrice) obj.getValue();
        if (categoryPrice.isLast) {
            finalAverages.put(categoryPrice.category, TimestampedValue.of(categoryPrice, obj.getTimestamp()));
        }
    }
    return finalAverages.values();
}
Also used : TimestampedValue(org.apache.beam.sdk.values.TimestampedValue) CategoryPrice(org.apache.beam.sdk.nexmark.model.CategoryPrice) TreeMap(java.util.TreeMap)

Example 2 with CategoryPrice

use of org.apache.beam.sdk.nexmark.model.CategoryPrice in project beam by apache.

the class Query4 method expand.

@Override
public PCollection<CategoryPrice> expand(PCollection<Event> events) {
    PCollection<AuctionBid> winningBids = events.apply(Filter.by(new AuctionOrBid())).apply(new WinningBids(name + ".WinningBids", configuration));
    // Monitor winning bids
    winningBids = winningBids.apply(name + ".WinningBidsMonitor", winningBidsMonitor.getTransform());
    return winningBids.apply(name + ".Rekey", ParDo.of(new DoFn<AuctionBid, KV<Long, Long>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Auction auction = c.element().auction;
            Bid bid = c.element().bid;
            c.output(KV.of(auction.category, bid.price));
        }
    })).apply(Window.into(SlidingWindows.of(Duration.standardSeconds(configuration.windowSizeSec)).every(Duration.standardSeconds(configuration.windowPeriodSec)))).apply(Mean.<Long, Long>perKey().withHotKeyFanout(configuration.fanout)).apply(name + ".Project", ParDo.of(new DoFn<KV<Long, Double>, CategoryPrice>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            c.output(new CategoryPrice(c.element().getKey(), Math.round(c.element().getValue()), c.pane().isLast()));
        }
    }));
}
Also used : Auction(org.apache.beam.sdk.nexmark.model.Auction) KV(org.apache.beam.sdk.values.KV) DoFn(org.apache.beam.sdk.transforms.DoFn) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid) CategoryPrice(org.apache.beam.sdk.nexmark.model.CategoryPrice) Bid(org.apache.beam.sdk.nexmark.model.Bid) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid)

Aggregations

CategoryPrice (org.apache.beam.sdk.nexmark.model.CategoryPrice)2 TreeMap (java.util.TreeMap)1 Auction (org.apache.beam.sdk.nexmark.model.Auction)1 AuctionBid (org.apache.beam.sdk.nexmark.model.AuctionBid)1 Bid (org.apache.beam.sdk.nexmark.model.Bid)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 KV (org.apache.beam.sdk.values.KV)1 TimestampedValue (org.apache.beam.sdk.values.TimestampedValue)1