Search in sources :

Example 1 with IdNameReserve

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

the class Query8 method expand.

@Override
public PCollection<IdNameReserve> expand(PCollection<Event> events) {
    // Window and key new people by their id.
    PCollection<KV<Long, Person>> personsById = events.apply(NexmarkQueryUtil.JUST_NEW_PERSONS).apply("Query8.WindowPersons", Window.into(FixedWindows.of(Duration.standardSeconds(configuration.windowSizeSec)))).apply("PersonById", NexmarkQueryUtil.PERSON_BY_ID);
    // Window and key new auctions by their id.
    PCollection<KV<Long, Auction>> auctionsBySeller = events.apply(NexmarkQueryUtil.JUST_NEW_AUCTIONS).apply("Query8.WindowAuctions", Window.into(FixedWindows.of(Duration.standardSeconds(configuration.windowSizeSec)))).apply("AuctionBySeller", NexmarkQueryUtil.AUCTION_BY_SELLER);
    // Join people and auctions and project the person id, name and auction reserve price.
    return KeyedPCollectionTuple.of(NexmarkQueryUtil.PERSON_TAG, personsById).and(NexmarkQueryUtil.AUCTION_TAG, auctionsBySeller).apply(CoGroupByKey.create()).apply(name + ".Select", ParDo.of(new DoFn<KV<Long, CoGbkResult>, IdNameReserve>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            @Nullable Person person = c.element().getValue().getOnly(NexmarkQueryUtil.PERSON_TAG, null);
            if (person == null) {
                // Person was not created in last window period.
                return;
            }
            for (Auction auction : c.element().getValue().getAll(NexmarkQueryUtil.AUCTION_TAG)) {
                c.output(new IdNameReserve(person.id, person.name, auction.reserve));
            }
        }
    }));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) Auction(org.apache.beam.sdk.nexmark.model.Auction) KV(org.apache.beam.sdk.values.KV) IdNameReserve(org.apache.beam.sdk.nexmark.model.IdNameReserve) Person(org.apache.beam.sdk.nexmark.model.Person) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CoGbkResult(org.apache.beam.sdk.transforms.join.CoGbkResult)

Aggregations

Auction (org.apache.beam.sdk.nexmark.model.Auction)1 IdNameReserve (org.apache.beam.sdk.nexmark.model.IdNameReserve)1 Person (org.apache.beam.sdk.nexmark.model.Person)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 CoGbkResult (org.apache.beam.sdk.transforms.join.CoGbkResult)1 KV (org.apache.beam.sdk.values.KV)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1