Search in sources :

Example 1 with Person

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

the class Query3 method expand.

@Override
public PCollection<NameCityStateId> expand(PCollection<Event> events) {
    PCollection<KV<Long, Event>> auctionsBySellerId = events.apply(NexmarkQueryUtil.JUST_NEW_AUCTIONS).apply(name + ".InCategory", Filter.by(auction -> auction.category == 10)).apply("EventByAuctionSeller", ParDo.of(new DoFn<Auction, KV<Long, Event>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Event e = new Event();
            e.newAuction = c.element();
            c.output(KV.of(c.element().seller, e));
        }
    }));
    PCollection<KV<Long, Event>> personsById = events.apply(NexmarkQueryUtil.JUST_NEW_PERSONS).apply(name + ".InState", Filter.by(person -> "OR".equals(person.state) || "ID".equals(person.state) || "CA".equals(person.state))).apply("EventByPersonId", ParDo.of(new DoFn<Person, KV<Long, Event>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Event e = new Event();
            e.newPerson = c.element();
            c.output(KV.of(c.element().id, e));
        }
    }));
    // Join auctions and people.
    return PCollectionList.of(auctionsBySellerId).and(personsById).apply(Flatten.pCollections()).apply(name + ".Join", ParDo.of(joinDoFn)).apply(name + ".Project", ParDo.of(new DoFn<KV<Auction, Person>, NameCityStateId>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Auction auction = c.element().getKey();
            Person person = c.element().getValue();
            c.output(new NameCityStateId(person.name, person.city, person.state, auction.id));
        }
    }));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) NameCityStateId(org.apache.beam.sdk.nexmark.model.NameCityStateId) Auction(org.apache.beam.sdk.nexmark.model.Auction) Event(org.apache.beam.sdk.nexmark.model.Event) KV(org.apache.beam.sdk.values.KV) Person(org.apache.beam.sdk.nexmark.model.Person)

Example 2 with Person

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

the class PersonGenerator method nextPerson.

/**
 * Generate and return a random person with next available id.
 */
public static Person nextPerson(long nextEventId, Random random, DateTime timestamp, GeneratorConfig config) {
    long id = lastBase0PersonId(nextEventId) + GeneratorConfig.FIRST_PERSON_ID;
    String name = nextPersonName(random);
    String email = nextEmail(random);
    String creditCard = nextCreditCard(random);
    String city = nextUSCity(random);
    String state = nextUSState(random);
    int currentSize = 8 + name.length() + email.length() + creditCard.length() + city.length() + state.length();
    String extra = nextExtra(random, currentSize, config.getAvgPersonByteSize());
    return new Person(id, name, email, creditCard, city, state, timestamp.toInstant(), extra);
}
Also used : StringsGenerator.nextString(org.apache.beam.sdk.nexmark.sources.generator.model.StringsGenerator.nextString) Person(org.apache.beam.sdk.nexmark.model.Person)

Example 3 with Person

use of org.apache.beam.sdk.nexmark.model.Person 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

Person (org.apache.beam.sdk.nexmark.model.Person)3 Auction (org.apache.beam.sdk.nexmark.model.Auction)2 DoFn (org.apache.beam.sdk.transforms.DoFn)2 KV (org.apache.beam.sdk.values.KV)2 Event (org.apache.beam.sdk.nexmark.model.Event)1 IdNameReserve (org.apache.beam.sdk.nexmark.model.IdNameReserve)1 NameCityStateId (org.apache.beam.sdk.nexmark.model.NameCityStateId)1 StringsGenerator.nextString (org.apache.beam.sdk.nexmark.sources.generator.model.StringsGenerator.nextString)1 CoGbkResult (org.apache.beam.sdk.transforms.join.CoGbkResult)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1