Java Code Examples for org.joda.time.Instant#ofEpochMilli()

The following examples show how to use org.joda.time.Instant#ofEpochMilli() . 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: HL7v2IO.java    From beam with Apache License 2.0 6 votes vote down vote up
@SplitRestriction
public void split(@Restriction OffsetRange timeRange, OutputReceiver<OffsetRange> out) {
  List<OffsetRange> splits =
      timeRange.split(initialSplitDuration.getMillis(), DEFAULT_MIN_SPLIT_DURATION.getMillis());
  Instant from = Instant.ofEpochMilli(timeRange.getFrom());
  Instant to = Instant.ofEpochMilli(timeRange.getTo());
  Duration totalDuration = new Duration(from, to);
  LOG.info(
      String.format(
          "splitting initial sendTime restriction of [minSendTime, now): [%s,%s), "
              + "or [%s, %s). \n"
              + "total days: %s \n"
              + "into %s splits. \n"
              + "Last split: %s",
          from,
          to,
          timeRange.getFrom(),
          timeRange.getTo(),
          totalDuration.getStandardDays(),
          splits.size(),
          splits.get(splits.size() - 1).toString()));

  for (OffsetRange s : splits) {
    out.output(s);
  }
}
 
Example 2
Source File: StatefulParDoEvaluatorFactoryTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequiresTimeSortedInputWithLateDataAndAllowedLateness() {
  Instant now = Instant.ofEpochMilli(0);
  PCollection<KV<String, Integer>> input =
      pipeline
          .apply(
              TestStream.create(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()))
                  .addElements(TimestampedValue.of(KV.of("", 1), now.plus(2)))
                  .addElements(TimestampedValue.of(KV.of("", 2), now.plus(1)))
                  .advanceWatermarkTo(now.plus(1))
                  .addElements(TimestampedValue.of(KV.of("", 3), now))
                  .advanceWatermarkToInfinity())
          .apply(
              Window.<KV<String, Integer>>into(new GlobalWindows())
                  .withAllowedLateness(Duration.millis(2)));
  PCollection<String> result = input.apply(ParDo.of(statefulConcat()));
  PAssert.that(result).containsInAnyOrder("3", "3:2", "3:2:1");
  pipeline.run();
}
 
Example 3
Source File: PeriodicSequence.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public ProcessContinuation processElement(
    @Element SequenceDefinition srcElement,
    OutputReceiver<Instant> out,
    RestrictionTracker<OffsetRange, Long> restrictionTracker) {

  OffsetRange restriction = restrictionTracker.currentRestriction();
  Long interval = srcElement.durationMilliSec;
  Long nextOutput = restriction.getFrom() + interval;

  boolean claimSuccess = true;

  while (claimSuccess && Instant.ofEpochMilli(nextOutput).isBeforeNow()) {
    claimSuccess = restrictionTracker.tryClaim(nextOutput);
    if (claimSuccess) {
      Instant output = Instant.ofEpochMilli(nextOutput);
      out.outputWithTimestamp(output, output);
      nextOutput = nextOutput + interval;
    }
  }

  ProcessContinuation continuation = ProcessContinuation.stop();
  if (claimSuccess) {
    Duration offset = new Duration(Instant.now(), Instant.ofEpochMilli(nextOutput));
    continuation = ProcessContinuation.resume().withResumeDelay(offset);
  }
  return continuation;
}
 
Example 4
Source File: HL7v2IO.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * List messages.
 *
 * @param hl7v2Store the HL7v2 store to list messages from
 * @throws IOException the io exception
 */
@ProcessElement
public void listMessages(
    @Element String hl7v2Store,
    RestrictionTracker<OffsetRange, Long> tracker,
    OutputReceiver<HL7v2Message> outputReceiver)
    throws IOException {
  OffsetRange currentRestriction = (OffsetRange) tracker.currentRestriction();
  Instant startRestriction = Instant.ofEpochMilli(currentRestriction.getFrom());
  Instant endRestriction = Instant.ofEpochMilli(currentRestriction.getTo());
  HttpHealthcareApiClient.HL7v2MessagePages pages =
      new HttpHealthcareApiClient.HL7v2MessagePages(
          client, hl7v2Store, startRestriction, endRestriction, filter.get(), "sendTime");
  Instant cursor;
  long lastClaimedMilliSecond = startRestriction.getMillis() - 1;
  for (HL7v2Message msg : FluentIterable.concat(pages)) {
    cursor = Instant.parse(msg.getSendTime());
    if (cursor.getMillis() > lastClaimedMilliSecond) {
      // Return early after the first claim failure preventing us from iterating
      // through the remaining messages.
      if (!tracker.tryClaim(cursor.getMillis())) {
        return;
      }
      lastClaimedMilliSecond = cursor.getMillis();
    }

    outputReceiver.output(msg);
  }

  // We've paginated through all messages for this restriction but the last message may be
  // before the end of the restriction
  tracker.tryClaim(currentRestriction.getTo());
}
 
Example 5
Source File: HttpHealthcareApiClient.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Instant getEarliestHL7v2SendTime(String hl7v2Store, @Nullable String filter)
    throws IOException {
  ListMessagesResponse response =
      client
          .projects()
          .locations()
          .datasets()
          .hl7V2Stores()
          .messages()
          .list(hl7v2Store)
          .setFilter(filter)
          .set("view", "full") // needed to retrieve the value for sendtime
          .setOrderBy("sendTime") // default order is ascending
          // https://cloud.google.com/apis/design/design_patterns#sorting_order
          .setPageSize(1) // Only interested in the earliest sendTime
          .execute();
  if (response.isEmpty()) {
    throw new IllegalArgumentException(
        String.format(
            "Could not find earliest send time. The filter %s  matched no results on "
                + "HL7v2 Store: %s",
            filter, hl7v2Store));
  }
  String sendTime = response.getHl7V2Messages().get(0).getSendTime();
  if (Strings.isNullOrEmpty(sendTime)) {
    LOG.warn(
        String.format(
            "Earliest message in %s has null or empty sendTime defaulting to Epoch.",
            hl7v2Store));
    return Instant.ofEpochMilli(0);
  }
  // sendTime is conveniently RFC3339 UTC "Zulu"
  // https://cloud.google.com/healthcare/docs/reference/rest/v1beta1/projects.locations.datasets.hl7V2Stores.messages#Message
  return Instant.parse(sendTime);
}
 
Example 6
Source File: SparkCombineFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionCombineFn() throws Exception {
  WindowingStrategy<Object, IntervalWindow> strategy =
      WindowingStrategy.of(Sessions.withGapDuration(Duration.millis(1000)));

  SparkCombineFn<KV<String, Integer>, Integer, Long, Long> sparkCombineFn =
      SparkCombineFn.keyed(combineFn, opts, Collections.emptyMap(), strategy);

  Instant now = Instant.ofEpochMilli(0);
  WindowedValue<KV<String, Integer>> first =
      input("key", 1, now.plus(5000), strategy.getWindowFn());
  WindowedValue<KV<String, Integer>> second =
      input("key", 2, now.plus(1000), strategy.getWindowFn());
  WindowedValue<KV<String, Integer>> third =
      input("key", 3, now.plus(500), strategy.getWindowFn());
  SparkCombineFn.WindowedAccumulator<KV<String, Integer>, Integer, Long, ?> c1 =
      sparkCombineFn.createCombiner(first);
  SparkCombineFn.WindowedAccumulator<KV<String, Integer>, Integer, Long, ?> c2 =
      sparkCombineFn.createCombiner(third);
  sparkCombineFn.mergeValue(c1, second);
  SparkCombineFn.WindowedAccumulator<KV<String, Integer>, Integer, Long, ?> c3 =
      sparkCombineFn.mergeCombiners(c1, c2);
  Iterable<WindowedValue<Long>> output = sparkCombineFn.extractOutput(c3);
  assertEquals(2, Iterables.size(output));
  List<String> format =
      StreamSupport.stream(output.spliterator(), false)
          .map(val -> val.getValue() + ":" + val.getTimestamp().getMillis())
          .collect(Collectors.toList());
  assertEquals(Lists.newArrayList("5:1999", "1:5999"), format);
}
 
Example 7
Source File: SparkCombineFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlidingCombineFnNonMerging() throws Exception {
  WindowingStrategy<Object, IntervalWindow> strategy =
      WindowingStrategy.of(SlidingWindows.of(Duration.millis(3000)).every(Duration.millis(1000)));

  SparkCombineFn<KV<String, Integer>, Integer, Long, Long> sparkCombineFn =
      SparkCombineFn.keyed(
          combineFn,
          opts,
          Collections.emptyMap(),
          strategy,
          SparkCombineFn.WindowedAccumulator.Type.NON_MERGING);

  Instant now = Instant.ofEpochMilli(0);
  WindowedValue<KV<String, Integer>> first =
      input("key", 1, now.plus(5000), strategy.getWindowFn());
  WindowedValue<KV<String, Integer>> second =
      input("key", 2, now.plus(1500), strategy.getWindowFn());
  WindowedValue<KV<String, Integer>> third =
      input("key", 3, now.plus(500), strategy.getWindowFn());
  SparkCombineFn.WindowedAccumulator<KV<String, Integer>, Integer, Long, ?> c1 =
      sparkCombineFn.createCombiner(first);
  SparkCombineFn.WindowedAccumulator<KV<String, Integer>, Integer, Long, ?> c2 =
      sparkCombineFn.createCombiner(third);
  sparkCombineFn.mergeValue(c1, second);
  SparkCombineFn.WindowedAccumulator<KV<String, Integer>, Integer, Long, ?> c3 =
      sparkCombineFn.mergeCombiners(c1, c2);
  Iterable<WindowedValue<Long>> output = sparkCombineFn.extractOutput(c3);
  assertEquals(7, Iterables.size(output));
  List<String> format =
      StreamSupport.stream(output.spliterator(), false)
          .map(val -> val.getValue() + ":" + val.getTimestamp().getMillis())
          .collect(Collectors.toList());
  assertUnorderedEquals(
      Lists.newArrayList("3:999", "5:1999", "5:2999", "2:3999", "1:5999", "1:6999", "1:7999"),
      format);
}
 
Example 8
Source File: StatefulParDoEvaluatorFactoryTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequiresTimeSortedInput() {
  Instant now = Instant.ofEpochMilli(0);
  PCollection<KV<String, Integer>> input =
      pipeline.apply(
          Create.timestamped(
              TimestampedValue.of(KV.of("", 1), now.plus(2)),
              TimestampedValue.of(KV.of("", 2), now.plus(1)),
              TimestampedValue.of(KV.of("", 3), now)));
  PCollection<String> result = input.apply(ParDo.of(statefulConcat()));
  PAssert.that(result).containsInAnyOrder("3", "3:2", "3:2:1");
  pipeline.run();
}
 
Example 9
Source File: StatefulParDoEvaluatorFactoryTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequiresTimeSortedInputWithLateData() {
  Instant now = Instant.ofEpochMilli(0);
  PCollection<KV<String, Integer>> input =
      pipeline.apply(
          TestStream.create(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()))
              .addElements(TimestampedValue.of(KV.of("", 1), now.plus(2)))
              .addElements(TimestampedValue.of(KV.of("", 2), now.plus(1)))
              .advanceWatermarkTo(now.plus(1))
              .addElements(TimestampedValue.of(KV.of("", 3), now))
              .advanceWatermarkToInfinity());
  PCollection<String> result = input.apply(ParDo.of(statefulConcat()));
  PAssert.that(result).containsInAnyOrder("2", "2:1");
  pipeline.run();
}
 
Example 10
Source File: Utility.java    From googlecalendar with Apache License 2.0 4 votes vote down vote up
public static LocalDate getDate(long milliSeconds) {
    Instant instantFromEpochMilli
            = Instant.ofEpochMilli(milliSeconds);
    return instantFromEpochMilli.toDateTime(DateTimeZone.getDefault()).toLocalDate();

}
 
Example 11
Source File: ZetaSqlBeamTranslationUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
private static Instant zetaSqlTimestampValueToJodaInstant(Value timestampValue) {
  long millis = timestampValue.getTimestampUnixMicros() / MICROS_PER_MILLI;
  return Instant.ofEpochMilli(millis);
}
 
Example 12
Source File: ConvertDateTimeUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenJodaInstant_WhenGetMillis_ThenMillis() {
    Instant jodaInstant = Instant.ofEpochMilli(millis);
    Assert.assertEquals(millis, jodaInstant.getMillis());
}