Search in sources :

Example 1 with Tuple

use of org.apache.apex.malhar.lib.window.Tuple in project apex-malhar by apache.

the class ApexWindowedStreamImpl method countByKey.

@Override
public <K, STREAM extends WindowedStream<Tuple.WindowedTuple<KeyValPair<K, Long>>>> STREAM countByKey(Function.ToKeyValue<T, K, Long> convertToKeyValue, Option... opts) {
    WindowedStream<Tuple<KeyValPair<K, Long>>> kvstream = map(convertToKeyValue);
    KeyedWindowedOperatorImpl<K, Long, MutableLong, Long> keyedWindowedOperator = createKeyedWindowedOperator(new SumLong());
    return kvstream.addOperator(keyedWindowedOperator, keyedWindowedOperator.input, keyedWindowedOperator.output, opts);
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) SumLong(org.apache.apex.malhar.lib.window.accumulation.SumLong) SumLong(org.apache.apex.malhar.lib.window.accumulation.SumLong) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Tuple(org.apache.apex.malhar.lib.window.Tuple)

Example 2 with Tuple

use of org.apache.apex.malhar.lib.window.Tuple in project apex-malhar by apache.

the class ApexWindowedStreamImpl method count.

@Override
public <STREAM extends WindowedStream<Tuple.WindowedTuple<Long>>> STREAM count(Option... opts) {
    Function.MapFunction<T, Tuple<Long>> kVMap = new Function.MapFunction<T, Tuple<Long>>() {

        @Override
        public Tuple<Long> f(T input) {
            if (input instanceof Tuple.TimestampedTuple) {
                return new Tuple.TimestampedTuple<>(((Tuple.TimestampedTuple) input).getTimestamp(), 1L);
            } else {
                return new Tuple.TimestampedTuple<>(System.currentTimeMillis(), 1L);
            }
        }
    };
    WindowedStream<Tuple<Long>> innerstream = map(kVMap);
    WindowedOperatorImpl<Long, MutableLong, Long> windowedOperator = createWindowedOperator(new SumLong());
    return innerstream.addOperator(windowedOperator, windowedOperator.input, windowedOperator.output, opts);
}
Also used : Function(org.apache.apex.malhar.lib.function.Function) MutableLong(org.apache.commons.lang3.mutable.MutableLong) SumLong(org.apache.apex.malhar.lib.window.accumulation.SumLong) SumLong(org.apache.apex.malhar.lib.window.accumulation.SumLong) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Tuple(org.apache.apex.malhar.lib.window.Tuple)

Example 3 with Tuple

use of org.apache.apex.malhar.lib.window.Tuple in project apex-malhar by apache.

the class WindowedWordCount method populateDAG.

/**
 * Populate dag with High-Level API.
 * @param dag
 * @param conf
 */
@Override
public void populateDAG(DAG dag, Configuration conf) {
    TextInput input = new TextInput();
    Collector collector = new Collector();
    // Create stream from the TextInput operator.
    ApexStream<Tuple.TimestampedTuple<String>> stream = StreamFactory.fromInput(input, input.output, name("input")).flatMap(new Function.FlatMapFunction<String, String>() {

        @Override
        public Iterable<String> f(String input) {
            return Arrays.asList(input.split("[\\p{Punct}\\s]+"));
        }
    }, name("ExtractWords")).map(new AddTimestampFn(), name("AddTimestampFn"));
    // apply window and trigger option.
    // TODO: change trigger option to atWaterMark when available.
    WindowedStream<Tuple.TimestampedTuple<String>> windowedWords = stream.window(new WindowOption.TimeWindows(Duration.standardMinutes(WINDOW_SIZE)), new TriggerOption().accumulatingFiredPanes().withEarlyFiringsAtEvery(1));
    WindowedStream<PojoEvent> wordCounts = // Perform a countByKey transformation to count the appearance of each word in every time window.
    windowedWords.countByKey(new Function.ToKeyValue<Tuple.TimestampedTuple<String>, String, Long>() {

        @Override
        public Tuple<KeyValPair<String, Long>> f(Tuple.TimestampedTuple<String> input) {
            return new Tuple.TimestampedTuple<KeyValPair<String, Long>>(input.getTimestamp(), new KeyValPair<String, Long>(input.getValue(), 1L));
        }
    }, name("count words")).map(new FormatAsTableRowFn(), name("FormatAsTableRowFn")).print(name("console"));
    wordCounts.endWith(collector, collector.input, name("Collector")).populateDag(dag);
}
Also used : TriggerOption(org.apache.apex.malhar.lib.window.TriggerOption) WindowOption(org.apache.apex.malhar.lib.window.WindowOption) Function(org.apache.apex.malhar.lib.function.Function) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Tuple(org.apache.apex.malhar.lib.window.Tuple)

Aggregations

Tuple (org.apache.apex.malhar.lib.window.Tuple)3 Function (org.apache.apex.malhar.lib.function.Function)2 SumLong (org.apache.apex.malhar.lib.window.accumulation.SumLong)2 MutableLong (org.apache.commons.lang3.mutable.MutableLong)2 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)1 TriggerOption (org.apache.apex.malhar.lib.window.TriggerOption)1 WindowOption (org.apache.apex.malhar.lib.window.WindowOption)1