Java Code Examples for org.apache.beam.sdk.values.TimestampedValue#getValue()

The following examples show how to use org.apache.beam.sdk.values.TimestampedValue#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Query4Model.java    From beam with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source File: Query5Model.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  TimestampedValue<Event> timestampedEvent = nextInput();
  if (timestampedEvent == null) {
    // Drain the remaining windows.
    retireWindows(NexmarkUtils.END_OF_TIME);
    allDone();
    return;
  }

  Event event = timestampedEvent.getValue();
  if (event.bid == null) {
    // Ignore non-bid events.
    return;
  }
  Instant timestamp = timestampedEvent.getTimestamp();
  Instant newWindowStart =
      windowStart(
          Duration.standardSeconds(configuration.windowSizeSec),
          Duration.standardSeconds(configuration.windowPeriodSec),
          timestamp);
  // Capture results from any windows we can now retire.
  retireWindows(newWindowStart);
  // Capture current bid.
  captureBid(event.bid, timestamp);
}
 
Example 3
Source File: Query6Model.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected Iterable<TimestampedValue<SellerPrice>> relevantResults(
    Iterable<TimestampedValue<SellerPrice>> results) {
  // Find the last (in processing time) reported average price for each seller.
  Map<Long, TimestampedValue<SellerPrice>> finalAverages = new TreeMap<>();
  for (TimestampedValue<SellerPrice> obj : results) {
    Assert.assertTrue("have SellerPrice", obj.getValue() instanceof SellerPrice);
    SellerPrice sellerPrice = (SellerPrice) obj.getValue();
    finalAverages.put(sellerPrice.seller, TimestampedValue.of(sellerPrice, obj.getTimestamp()));
  }
  return finalAverages.values();
}
 
Example 4
Source File: Query7Model.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() {
  TimestampedValue<Event> timestampedEvent = nextInput();
  if (timestampedEvent == null) {
    // Capture all remaining bids in results.
    retireWindow(lastTimestamp);
    allDone();
    return;
  }

  Event event = timestampedEvent.getValue();
  if (event.bid == null) {
    // Ignore non-bid events.
    return;
  }
  lastTimestamp = timestampedEvent.getTimestamp();
  Instant newWindowStart =
      windowStart(
          Duration.standardSeconds(configuration.windowSizeSec),
          Duration.standardSeconds(configuration.windowSizeSec),
          lastTimestamp);
  if (!newWindowStart.equals(windowStart)) {
    // Capture highest priced bids in current window and retire it.
    retireWindow(lastTimestamp);
    windowStart = newWindowStart;
  }
  // Keep only the highest bids.
  captureBid(event.bid);
}
 
Example 5
Source File: SessionSideInputJoinModel.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() {
  TimestampedValue<Event> timestampedEvent = nextInput();
  if (timestampedEvent == null) {
    for (Long bidder : ImmutableSet.copyOf(activeSessions.keySet())) {
      flushSession(bidder);
    }
    allDone();
    return;
  }
  Event event = timestampedEvent.getValue();
  if (event.bid == null) {
    // Ignore non-bid events.
    return;
  }

  List<TimestampedValue<Event>> activeSession = activeSessions.get(event.bid.bidder);
  if (activeSession == null) {
    beginSession(timestampedEvent);
  } else if (timestampedEvent
      .getTimestamp()
      .isAfter(
          activeSession
              .get(activeSession.size() - 1)
              .getTimestamp()
              .plus(configuration.sessionGap))) {
    flushSession(event.bid.bidder);
    beginSession(timestampedEvent);
  } else {
    activeSession.add(timestampedEvent);
  }
}
 
Example 6
Source File: SessionSideInputJoinModel.java    From beam with Apache License 2.0 5 votes vote down vote up
private void flushSession(long bidder) {
  List<TimestampedValue<Event>> session = activeSessions.get(bidder);
  checkState(session != null);

  Instant sessionStart =
      Ordering.<Instant>natural()
          .min(
              session.stream()
                  .<Instant>map(tsv -> tsv.getTimestamp())
                  .collect(Collectors.toList()));

  Instant sessionEnd =
      Ordering.<Instant>natural()
          .max(
              session.stream()
                  .<Instant>map(tsv -> tsv.getTimestamp())
                  .collect(Collectors.toList()))
          .plus(configuration.sessionGap);

  for (TimestampedValue<Event> timestampedEvent : session) {
    // Join to the side input is always a string representation of the id being looked up
    Bid bid = timestampedEvent.getValue().bid;
    Bid resultBid =
        new Bid(
            bid.auction,
            bid.bidder,
            bid.price,
            bid.dateTime,
            String.format(
                "%d:%s:%s",
                bid.bidder % configuration.sideInputRowCount, sessionStart, sessionEnd));
    TimestampedValue<Bid> result =
        TimestampedValue.of(resultBid, timestampedEvent.getTimestamp());
    addResult(result);
  }
  activeSessions.remove(bidder);
}
 
Example 7
Source File: Query3Model.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected void run() {
  TimestampedValue<Event> timestampedEvent = nextInput();
  if (timestampedEvent == null) {
    allDone();
    return;
  }
  Event event = timestampedEvent.getValue();
  if (event.bid != null) {
    // Ignore bid events.
    return;
  }

  Instant timestamp = timestampedEvent.getTimestamp();

  if (event.newAuction != null) {
    // Only want auctions in category 10.
    if (event.newAuction.category == 10) {
      // Join new auction with existing person, if any.
      Person person = newPersons.get(event.newAuction.seller);
      if (person != null) {
        addResult(event.newAuction, person, timestamp);
      } else {
        // Remember auction for future new person event.
        newAuctions.put(event.newAuction.seller, event.newAuction);
      }
    }
  } else {
    // Only want people in OR, ID or CA.
    if ("OR".equals(event.newPerson.state)
        || "ID".equals(event.newPerson.state)
        || "CA".equals(event.newPerson.state)) {
      // Join new person with existing auctions.
      for (Auction auction : newAuctions.get(event.newPerson.id)) {
        addResult(auction, event.newPerson, timestamp);
      }
      // We'll never need these auctions again.
      newAuctions.removeAll(event.newPerson.id);
      // Remember person for future auctions.
      newPersons.put(event.newPerson.id, event.newPerson);
    }
  }
}
 
Example 8
Source File: Query8Model.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
  TimestampedValue<Event> timestampedEvent = nextInput();
  if (timestampedEvent == null) {
    allDone();
    return;
  }

  Event event = timestampedEvent.getValue();
  if (event.bid != null) {
    // Ignore bid events.
    // Keep looking for next events.
    return;
  }
  Instant timestamp = timestampedEvent.getTimestamp();
  Instant newWindowStart =
      windowStart(
          Duration.standardSeconds(configuration.windowSizeSec),
          Duration.standardSeconds(configuration.windowSizeSec),
          timestamp);
  if (!newWindowStart.equals(windowStart)) {
    // Retire this window.
    retirePersons();
    retireAuctions();
    windowStart = newWindowStart;
  }

  if (event.newAuction != null) {
    // Join new auction with existing person, if any.
    Person person = newPersons.get(event.newAuction.seller);
    if (person != null) {
      addResult(event.newAuction, person, timestamp);
    } else {
      // Remember auction for future new people.
      newAuctions.put(event.newAuction.seller, event.newAuction);
    }
  } else { // event is not an auction, nor a bid, so it is a person
    // Join new person with existing auctions.
    for (Auction auction : newAuctions.get(event.newPerson.id)) {
      addResult(auction, event.newPerson, timestamp);
    }
    // We'll never need these auctions again.
    newAuctions.removeAll(event.newPerson.id);
    // Remember person for future auctions.
    newPersons.put(event.newPerson.id, event.newPerson);
  }
}
 
Example 9
Source File: Latest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public T extractOutput(TimestampedValue<T> accumulator) {
  return accumulator.getValue();
}