Example usage for org.joda.time Duration millis

List of usage examples for org.joda.time Duration millis

Introduction

In this page you can find the example usage for org.joda.time Duration millis.

Prototype

public static Duration millis(long millis) 

Source Link

Document

Create a duration with the specified number of milliseconds.

Usage

From source file:org.apache.apex.malhar.stream.sample.WordCountWithStreamAPI.java

License:Apache License

@Override
public void populateDAG(DAG dag, Configuration configuration) {
    WCInput wcInput = new WCInput();
    ApexStream<String> stream = StreamFactory.fromInput(wcInput, wcInput.output)
            .flatMap(new Function.FlatMapFunction<String, String>() {
                @Override//w  ww  . j av a 2 s .com
                public Iterable<String> f(String input) {
                    return Arrays.asList(input.split("[\\p{Punct}\\s]+"));
                }
            });
    stream.print();
    stream.window(new WindowOption.GlobalWindow(),
            new TriggerOption().withEarlyFiringsAtEvery(Duration.millis(1000)).accumulatingFiredPanes())
            .countByKey(new Function.ToKeyValue<String, String, Long>() {
                @Override
                public Tuple<KeyValPair<String, Long>> f(String input) {
                    return new Tuple.PlainTuple(new KeyValPair<>(input, 1L));
                }
            }).print();
    stream.populateDag(dag);
}

From source file:org.apache.beam.runners.core.construction.Triggers.java

License:Apache License

public static Trigger fromProto(RunnerApi.Trigger triggerProto) {
    switch (triggerProto.getTriggerCase()) {
    case AFTER_ALL:
        return AfterAll.of(protosToTriggers(triggerProto.getAfterAll().getSubtriggersList()));
    case AFTER_ANY:
        return AfterFirst.of(protosToTriggers(triggerProto.getAfterAny().getSubtriggersList()));
    case AFTER_EACH:
        return AfterEach.inOrder(protosToTriggers(triggerProto.getAfterEach().getSubtriggersList()));
    case AFTER_END_OF_WINDOW:
        RunnerApi.Trigger.AfterEndOfWindow eowProto = triggerProto.getAfterEndOfWindow();

        if (!eowProto.hasEarlyFirings() && !eowProto.hasLateFirings()) {
            return AfterWatermark.pastEndOfWindow();
        }//from www .  j av a2 s . c o  m

        // It either has early or late firings or both; our typing in Java makes this a smidge
        // annoying
        if (triggerProto.getAfterEndOfWindow().hasEarlyFirings()) {
            AfterWatermarkEarlyAndLate trigger = AfterWatermark.pastEndOfWindow().withEarlyFirings(
                    (OnceTrigger) fromProto(triggerProto.getAfterEndOfWindow().getEarlyFirings()));

            if (triggerProto.getAfterEndOfWindow().hasLateFirings()) {
                trigger = trigger.withLateFirings(
                        (OnceTrigger) fromProto(triggerProto.getAfterEndOfWindow().getLateFirings()));
            }
            return trigger;
        } else {
            // only late firings, so return directly
            return AfterWatermark.pastEndOfWindow()
                    .withLateFirings((OnceTrigger) fromProto(eowProto.getLateFirings()));
        }
    case AFTER_PROCESSING_TIME:
        AfterProcessingTime trigger = AfterProcessingTime.pastFirstElementInPane();
        for (RunnerApi.TimestampTransform transform : triggerProto.getAfterProcessingTime()
                .getTimestampTransformsList()) {
            switch (transform.getTimestampTransformCase()) {
            case ALIGN_TO:
                trigger = trigger.alignedTo(Duration.millis(transform.getAlignTo().getPeriod()),
                        new Instant(transform.getAlignTo().getOffset()));
                break;
            case DELAY:
                trigger = trigger.plusDelayOf(Duration.millis(transform.getDelay().getDelayMillis()));
                break;
            case TIMESTAMPTRANSFORM_NOT_SET:
                throw new IllegalArgumentException(
                        String.format("Required field 'timestamp_transform' not set in %s", transform));
            default:
                throw new IllegalArgumentException(String.format("Unknown timestamp transform case: %s",
                        transform.getTimestampTransformCase()));
            }
        }
        return trigger;
    case AFTER_SYNCHRONIZED_PROCESSING_TIME:
        return AfterSynchronizedProcessingTime.ofFirstElement();
    case ALWAYS:
        return new ReshuffleTrigger();
    case ELEMENT_COUNT:
        return AfterPane.elementCountAtLeast(triggerProto.getElementCount().getElementCount());
    case NEVER:
        return Never.ever();
    case OR_FINALLY:
        return fromProto(triggerProto.getOrFinally().getMain())
                .orFinally((OnceTrigger) fromProto(triggerProto.getOrFinally().getFinally()));
    case REPEAT:
        return Repeatedly.forever(fromProto(triggerProto.getRepeat().getSubtrigger()));
    case DEFAULT:
        return DefaultTrigger.of();
    case TRIGGER_NOT_SET:
        throw new IllegalArgumentException(
                String.format("Required field 'trigger' not set in %s", triggerProto));
    default:
        throw new IllegalArgumentException(
                String.format("Unknown trigger case: %s", triggerProto.getTriggerCase()));
    }
}

From source file:org.apache.beam.runners.core.construction.WindowingStrategies.java

License:Apache License

/**
 * Converts from {@link RunnerApi.WindowingStrategy} to the SDK's {@link WindowingStrategy} using
 * the provided components to dereferences identifiers found in the proto.
 *//*from w w w  .j  a va 2s  .  c om*/
public static WindowingStrategy<?, ?> fromProto(RunnerApi.WindowingStrategy proto, Components components)
        throws InvalidProtocolBufferException {

    SdkFunctionSpec windowFnSpec = proto.getWindowFn();

    checkArgument(windowFnSpec.getSpec().getUrn().equals(CUSTOM_WINDOWFN_URN),
            "Only Java-serialized %s instances are supported, with URN %s. But found URN %s",
            WindowFn.class.getSimpleName(), CUSTOM_WINDOWFN_URN, windowFnSpec.getSpec().getUrn());

    Object deserializedWindowFn = SerializableUtils.deserializeFromByteArray(
            windowFnSpec.getSpec().getParameter().unpack(BytesValue.class).getValue().toByteArray(),
            "WindowFn");

    WindowFn<?, ?> windowFn = (WindowFn<?, ?>) deserializedWindowFn;
    OutputTimeFn<?> outputTimeFn = OutputTimeFns.fromProto(proto.getOutputTime());
    AccumulationMode accumulationMode = fromProto(proto.getAccumulationMode());
    Trigger trigger = Triggers.fromProto(proto.getTrigger());
    ClosingBehavior closingBehavior = fromProto(proto.getClosingBehavior());
    Duration allowedLateness = Duration.millis(proto.getAllowedLateness());

    return WindowingStrategy.of(windowFn).withAllowedLateness(allowedLateness).withMode(accumulationMode)
            .withTrigger(trigger).withOutputTimeFn(outputTimeFn).withClosingBehavior(closingBehavior);
}

From source file:org.apache.beam.runners.core.construction.WindowingStrategyTranslation.java

License:Apache License

/**
 * Converts from {@link RunnerApi.WindowingStrategy} to the SDK's {@link WindowingStrategy} using
 * the provided components to dereferences identifiers found in the proto.
 *//*from  w ww  .j  ava  2  s . c  o m*/
public static WindowingStrategy<?, ?> fromProto(RunnerApi.WindowingStrategy proto,
        RehydratedComponents components) throws InvalidProtocolBufferException {

    SdkFunctionSpec windowFnSpec = proto.getWindowFn();
    WindowFn<?, ?> windowFn = windowFnFromProto(windowFnSpec);
    TimestampCombiner timestampCombiner = timestampCombinerFromProto(proto.getOutputTime());
    AccumulationMode accumulationMode = fromProto(proto.getAccumulationMode());
    Trigger trigger = TriggerTranslation.fromProto(proto.getTrigger());
    ClosingBehavior closingBehavior = fromProto(proto.getClosingBehavior());
    Duration allowedLateness = Duration.millis(proto.getAllowedLateness());
    OnTimeBehavior onTimeBehavior = fromProto(proto.getOnTimeBehavior());

    return WindowingStrategy.of(windowFn).withAllowedLateness(allowedLateness).withMode(accumulationMode)
            .withTrigger(trigger).withTimestampCombiner(timestampCombiner).withClosingBehavior(closingBehavior)
            .withOnTimeBehavior(onTimeBehavior);
}

From source file:org.apache.beam.runners.core.construction.WindowingStrategyTranslation.java

License:Apache License

public static WindowFn<?, ?> windowFnFromProto(SdkFunctionSpec windowFnSpec) {
    try {//from www .  j av  a2s .c  o m
        String s = windowFnSpec.getSpec().getUrn();
        if (s.equals(getUrn(GlobalWindowsPayload.Enum.PROPERTIES))) {
            return new GlobalWindows();
        } else if (s.equals(getUrn(FixedWindowsPayload.Enum.PROPERTIES))) {
            FixedWindowsPayload fixedParams = FixedWindowsPayload
                    .parseFrom(windowFnSpec.getSpec().getPayload());
            return FixedWindows.of(Duration.millis(Durations.toMillis(fixedParams.getSize())))
                    .withOffset(Duration.millis(Timestamps.toMillis(fixedParams.getOffset())));
        } else if (s.equals(getUrn(SlidingWindowsPayload.Enum.PROPERTIES))) {
            SlidingWindowsPayload slidingParams = SlidingWindowsPayload
                    .parseFrom(windowFnSpec.getSpec().getPayload());
            return SlidingWindows.of(Duration.millis(Durations.toMillis(slidingParams.getSize())))
                    .every(Duration.millis(Durations.toMillis(slidingParams.getPeriod())))
                    .withOffset(Duration.millis(Timestamps.toMillis(slidingParams.getOffset())));
        } else if (s.equals(getUrn(SessionsPayload.Enum.PROPERTIES))) {
            SessionsPayload sessionParams = SessionsPayload.parseFrom(windowFnSpec.getSpec().getPayload());
            return Sessions.withGapDuration(Duration.millis(Durations.toMillis(sessionParams.getGapSize())));
        } else if (s.equals(SERIALIZED_JAVA_WINDOWFN_URN)) {
            return (WindowFn<?, ?>) SerializableUtils
                    .deserializeFromByteArray(windowFnSpec.getSpec().getPayload().toByteArray(), "WindowFn");
        } else {
            throw new IllegalArgumentException(
                    "Unknown or unsupported WindowFn: " + windowFnSpec.getSpec().getUrn());
        }
    } catch (InvalidProtocolBufferException e) {
        throw new IllegalArgumentException(
                String.format("%s for %s with URN %s did not contain expected proto message for payload",
                        FunctionSpec.class.getSimpleName(), WindowFn.class.getSimpleName(),
                        windowFnSpec.getSpec().getUrn()),
                e);
    }
}

From source file:org.apache.beam.runners.core.GroupAlsoByWindowsProperties.java

License:Apache License

/**
 * Tests that for empty input and the given {@link WindowingStrategy}, the provided GABW
 * implementation produces no output./*from   www  .  j av a2 s.  c  om*/
 *
 * <p>The input type is deliberately left as a wildcard, since it is not relevant.
 */
public static <K, InputT, OutputT> void emptyInputEmptyOutput(
        GroupAlsoByWindowsDoFnFactory<K, InputT, OutputT> gabwFactory) throws Exception {

    WindowingStrategy<?, IntervalWindow> windowingStrategy = WindowingStrategy
            .of(FixedWindows.of(Duration.millis(10)));

    // This key should never actually be used, though it is eagerly passed to the
    // StateInternalsFactory so must be non-null
    @SuppressWarnings("unchecked")
    K fakeKey = (K) "this key should never be used";

    List<WindowedValue<KV<K, OutputT>>> result = runGABW(gabwFactory, windowingStrategy, fakeKey,
            Collections.<WindowedValue<InputT>>emptyList());

    assertThat(result, hasSize(0));
}

From source file:org.apache.beam.runners.core.GroupAlsoByWindowsProperties.java

License:Apache License

/**
 * Tests that for a simple sequence of elements on the same key, the given GABW implementation
 * correctly groups them according to fixed windows.
 *///from w  w  w .j a v a 2s .co  m
public static void groupsElementsIntoFixedWindows(
        GroupAlsoByWindowsDoFnFactory<String, String, Iterable<String>> gabwFactory) throws Exception {

    WindowingStrategy<?, IntervalWindow> windowingStrategy = WindowingStrategy
            .of(FixedWindows.of(Duration.millis(10)));

    List<WindowedValue<KV<String, Iterable<String>>>> result = runGABW(gabwFactory, windowingStrategy, "key",
            WindowedValue.of("v1", new Instant(1), Arrays.asList(window(0, 10)), PaneInfo.NO_FIRING),
            WindowedValue.of("v2", new Instant(2), Arrays.asList(window(0, 10)), PaneInfo.NO_FIRING),
            WindowedValue.of("v3", new Instant(13), Arrays.asList(window(10, 20)), PaneInfo.NO_FIRING));

    assertThat(result, hasSize(2));

    TimestampedValue<KV<String, Iterable<String>>> item0 = getOnlyElementInWindow(result, window(0, 10));
    assertThat(item0.getValue().getValue(), containsInAnyOrder("v1", "v2"));
    assertThat(item0.getTimestamp(), equalTo(window(0, 10).maxTimestamp()));

    TimestampedValue<KV<String, Iterable<String>>> item1 = getOnlyElementInWindow(result, window(10, 20));
    assertThat(item1.getValue().getValue(), contains("v3"));
    assertThat(item1.getTimestamp(), equalTo(window(10, 20).maxTimestamp()));
}

From source file:org.apache.beam.runners.core.GroupAlsoByWindowsProperties.java

License:Apache License

/**
 * Tests that for a simple sequence of elements on the same key, the given GABW implementation
 * correctly groups them into sliding windows.
 *
 * <p>In the input here, each element occurs in multiple windows.
 *///from   ww  w  .ja va  2  s.  c om
public static void groupsElementsIntoSlidingWindowsWithMinTimestamp(
        GroupAlsoByWindowsDoFnFactory<String, String, Iterable<String>> gabwFactory) throws Exception {

    WindowingStrategy<?, IntervalWindow> windowingStrategy = WindowingStrategy
            .of(SlidingWindows.of(Duration.millis(20)).every(Duration.millis(10)))
            .withOutputTimeFn(OutputTimeFns.outputAtEarliestInputTimestamp());

    List<WindowedValue<KV<String, Iterable<String>>>> result = runGABW(gabwFactory, windowingStrategy, "key",
            WindowedValue.of("v1", new Instant(5), Arrays.asList(window(-10, 10), window(0, 20)),
                    PaneInfo.NO_FIRING),
            WindowedValue.of("v2", new Instant(15), Arrays.asList(window(0, 20), window(10, 30)),
                    PaneInfo.NO_FIRING));

    assertThat(result, hasSize(3));

    TimestampedValue<KV<String, Iterable<String>>> item0 = getOnlyElementInWindow(result, window(-10, 10));
    assertThat(item0.getValue().getValue(), contains("v1"));
    assertThat(item0.getTimestamp(), equalTo(new Instant(5)));

    TimestampedValue<KV<String, Iterable<String>>> item1 = getOnlyElementInWindow(result, window(0, 20));
    assertThat(item1.getValue().getValue(), containsInAnyOrder("v1", "v2"));
    // Timestamp adjusted by WindowFn to exceed the end of the prior sliding window
    assertThat(item1.getTimestamp(), equalTo(new Instant(10)));

    TimestampedValue<KV<String, Iterable<String>>> item2 = getOnlyElementInWindow(result, window(10, 30));
    assertThat(item2.getValue().getValue(), contains("v2"));
    // Timestamp adjusted by WindowFn to exceed the end of the prior sliding window
    assertThat(item2.getTimestamp(), equalTo(new Instant(20)));
}

From source file:org.apache.beam.runners.core.GroupAlsoByWindowsProperties.java

License:Apache License

/**
 * Tests that for a simple sequence of elements on the same key, the given GABW implementation
 * correctly groups and combines them according to sliding windows.
 *
 * <p>In the input here, each element occurs in multiple windows.
 *///w ww .  jav  a 2  s . c o  m
public static void combinesElementsInSlidingWindows(
        GroupAlsoByWindowsDoFnFactory<String, Long, Long> gabwFactory, CombineFn<Long, ?, Long> combineFn)
        throws Exception {

    WindowingStrategy<?, IntervalWindow> windowingStrategy = WindowingStrategy
            .of(SlidingWindows.of(Duration.millis(20)).every(Duration.millis(10)))
            .withOutputTimeFn(OutputTimeFns.outputAtEarliestInputTimestamp());

    List<WindowedValue<KV<String, Long>>> result = runGABW(gabwFactory, windowingStrategy, "k",
            WindowedValue.of(1L, new Instant(5), Arrays.asList(window(-10, 10), window(0, 20)),
                    PaneInfo.NO_FIRING),
            WindowedValue.of(2L, new Instant(15), Arrays.asList(window(0, 20), window(10, 30)),
                    PaneInfo.NO_FIRING),
            WindowedValue.of(4L, new Instant(18), Arrays.asList(window(0, 20), window(10, 30)),
                    PaneInfo.NO_FIRING));

    assertThat(result, hasSize(3));

    TimestampedValue<KV<String, Long>> item0 = getOnlyElementInWindow(result, window(-10, 10));
    assertThat(item0.getValue().getKey(), equalTo("k"));
    assertThat(item0.getValue().getValue(), equalTo(combineFn.apply(ImmutableList.of(1L))));
    assertThat(item0.getTimestamp(), equalTo(new Instant(5L)));

    TimestampedValue<KV<String, Long>> item1 = getOnlyElementInWindow(result, window(0, 20));
    assertThat(item1.getValue().getKey(), equalTo("k"));
    assertThat(item1.getValue().getValue(), equalTo(combineFn.apply(ImmutableList.of(1L, 2L, 4L))));
    // Timestamp adjusted by WindowFn to exceed the end of the prior sliding window
    assertThat(item1.getTimestamp(), equalTo(new Instant(10L)));

    TimestampedValue<KV<String, Long>> item2 = getOnlyElementInWindow(result, window(10, 30));
    assertThat(item2.getValue().getKey(), equalTo("k"));
    assertThat(item2.getValue().getValue(), equalTo(combineFn.apply(ImmutableList.of(2L, 4L))));
    // Timestamp adjusted by WindowFn to exceed the end of the prior sliding window
    assertThat(item2.getTimestamp(), equalTo(new Instant(20L)));
}

From source file:org.apache.beam.runners.core.GroupAlsoByWindowsProperties.java

License:Apache License

/**
 * Tests that the given GABW implementation correctly groups elements that fall into overlapping
 * windows that are not merged.//  w  w w  .  j  a v  a2 s.  com
 */
public static void groupsIntoOverlappingNonmergingWindows(
        GroupAlsoByWindowsDoFnFactory<String, String, Iterable<String>> gabwFactory) throws Exception {

    WindowingStrategy<?, IntervalWindow> windowingStrategy = WindowingStrategy
            .of(FixedWindows.of(Duration.millis(10)));

    List<WindowedValue<KV<String, Iterable<String>>>> result = runGABW(gabwFactory, windowingStrategy, "key",
            WindowedValue.of("v1", new Instant(1), Arrays.asList(window(0, 5)), PaneInfo.NO_FIRING),
            WindowedValue.of("v2", new Instant(4), Arrays.asList(window(1, 5)), PaneInfo.NO_FIRING),
            WindowedValue.of("v3", new Instant(4), Arrays.asList(window(0, 5)), PaneInfo.NO_FIRING));

    assertThat(result, hasSize(2));

    TimestampedValue<KV<String, Iterable<String>>> item0 = getOnlyElementInWindow(result, window(0, 5));
    assertThat(item0.getValue().getValue(), containsInAnyOrder("v1", "v3"));
    assertThat(item0.getTimestamp(), equalTo(window(1, 5).maxTimestamp()));

    TimestampedValue<KV<String, Iterable<String>>> item1 = getOnlyElementInWindow(result, window(1, 5));
    assertThat(item1.getValue().getValue(), contains("v2"));
    assertThat(item1.getTimestamp(), equalTo(window(0, 5).maxTimestamp()));
}