Search in sources :

Example 1 with WindowData

use of edu.snu.mist.common.windows.WindowData in project mist by snuspl.

the class JoinOperatorTest method testAggregateWindowOperator.

/**
 * Tests JoinOperator.
 * It joins a pair of inputs in two streams that has same key.
 */
@Test
public void testAggregateWindowOperator() {
    // input stream events
    final WindowImpl<Integer> window = new WindowImpl<>(0L, 100L);
    window.putData(new MistDataEvent(new Tuple2<>(new Tuple2<>("Hello", 1), null), 10));
    window.putData(new MistDataEvent(new Tuple2<>(new Tuple2<>("MIST", 2), null), 20));
    window.putData(new MistDataEvent(new Tuple2<>(null, new Tuple2<>(1, 3000L)), 30));
    window.putData(new MistDataEvent(new Tuple2<>(new Tuple2<>("SNUCMS", 3), null), 40));
    window.putData(new MistDataEvent(new Tuple2<>(null, new Tuple2<>(1, 4000L)), 50));
    window.putData(new MistDataEvent(new Tuple2<>(null, new Tuple2<>(2, 5000L)), 60));
    final MistDataEvent dataEvent = new MistDataEvent(window, 60L);
    final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(101L);
    // predicate that tests whether two input data have same key or not
    final MISTBiPredicate<Tuple2<String, Integer>, Tuple2<Integer, Long>> joinPredicate = (tuple1, tuple2) -> tuple1.get(1).equals(tuple2.get(0));
    final JoinOperator<Tuple2<String, Integer>, Tuple2<Integer, Long>> joinOperator = new JoinOperator<>(joinPredicate);
    // expected pairs
    // {Hello, 1} and {1, 3000L}
    // {Hello, 1} and {1, 4000L}
    // {MIST, 2} and {2, 5000L}
    final List<MistEvent> result = new LinkedList<>();
    joinOperator.setOutputEmitter(new OutputBufferEmitter(result));
    joinOperator.processLeftData(dataEvent);
    Assert.assertEquals(1, result.size());
    Assert.assertTrue(result.get(0).isData());
    Assert.assertTrue(((MistDataEvent) result.get(0)).getValue() instanceof WindowData);
    final WindowData windowData = (WindowData) ((MistDataEvent) result.get(0)).getValue();
    Assert.assertEquals(0L, windowData.getStart());
    Assert.assertEquals(99L, windowData.getEnd());
    final Collection<Tuple2<Tuple2<String, Integer>, Tuple2<Integer, Long>>> dataCollection = windowData.getDataCollection();
    final Iterator iterator = dataCollection.iterator();
    Assert.assertEquals(3, dataCollection.size());
    Assert.assertEquals(new Tuple2<>(new Tuple2<>("Hello", 1), new Tuple2<>(1, 3000L)), iterator.next());
    Assert.assertEquals(new Tuple2<>(new Tuple2<>("Hello", 1), new Tuple2<>(1, 4000L)), iterator.next());
    Assert.assertEquals(new Tuple2<>(new Tuple2<>("MIST", 2), new Tuple2<>(2, 5000L)), iterator.next());
    Assert.assertEquals(60L, result.get(0).getTimestamp());
    joinOperator.processLeftWatermark(watermarkEvent);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(watermarkEvent, result.get(1));
}
Also used : Iterator(java.util.Iterator) Tuple2(edu.snu.mist.common.types.Tuple2) Collection(java.util.Collection) MistEvent(edu.snu.mist.core.MistEvent) Test(org.junit.Test) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistDataEvent(edu.snu.mist.core.MistDataEvent) List(java.util.List) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) MISTBiPredicate(edu.snu.mist.common.functions.MISTBiPredicate) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) WindowData(edu.snu.mist.common.windows.WindowData) Assert(org.junit.Assert) LinkedList(java.util.LinkedList) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) WindowData(edu.snu.mist.common.windows.WindowData) MistDataEvent(edu.snu.mist.core.MistDataEvent) Tuple2(edu.snu.mist.common.types.Tuple2) Iterator(java.util.Iterator) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) Test(org.junit.Test)

Example 2 with WindowData

use of edu.snu.mist.common.windows.WindowData in project mist by snuspl.

the class SessionWindow method submitQuery.

/**
 * Submit a query.
 * The query reads strings from a source server, puts them into a window,
 * and if there is no incoming data during the interval of the session window,
 * the session will be closed.
 * The query will print out the data in the session, and a new session is created
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    // configurations for source and sink
    final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
    final SourceConfiguration localTextSocketSourceConf = MISTExampleUtils.getLocalTextSocketSourceConf(sourceSocket);
    // configurations for windowing and aggregation by session dependent on time
    final int sessionInterval = 5000;
    final MISTFunction<WindowData<String>, String> aggregateFunc = (windowData) -> {
        return windowData.getDataCollection().toString() + ", window is started at " + windowData.getStart() + ", window is ended at " + windowData.getEnd() + ".";
    };
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.socketTextStream(localTextSocketSourceConf).window(new SessionWindowInformation(sessionInterval)).aggregateWindow(aggregateFunc).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
    System.out.println("End of submitQuery");
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : Tang(org.apache.reef.tang.Tang) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) MISTFunction(edu.snu.mist.common.functions.MISTFunction) NettySourceAddress(edu.snu.mist.examples.parameters.NettySourceAddress) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CommandLine(org.apache.reef.tang.formats.CommandLine) APIQueryControlResult(edu.snu.mist.client.APIQueryControlResult) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Configuration(org.apache.reef.tang.Configuration) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) WindowData(edu.snu.mist.common.windows.WindowData) InjectionException(org.apache.reef.tang.exceptions.InjectionException) SessionWindowInformation(edu.snu.mist.common.windows.SessionWindowInformation) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) WindowData(edu.snu.mist.common.windows.WindowData) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) SessionWindowInformation(edu.snu.mist.common.windows.SessionWindowInformation)

Example 3 with WindowData

use of edu.snu.mist.common.windows.WindowData in project mist by snuspl.

the class WindowedStreamTest method testAggregateWindowStream.

/**
 * Test for aggregateWindow operation.
 */
@Test
public void testAggregateWindowStream() throws InjectionException, IOException, ClassNotFoundException {
    final MISTFunction<WindowData<Tuple2<String, Integer>>, String> func = new WindowAggregateFunction();
    final ContinuousStream<String> aggregateWindowStream = timeWindowedStream.aggregateWindow(func);
    final Map<String, String> conf = aggregateWindowStream.getConfiguration();
    Assert.assertEquals(SerializeUtils.serializeToString(func), conf.get(ConfKeys.OperatorConf.UDF_STRING.name()));
    // Check windowed -> aggregated
    checkEdges(queryBuilder.build().getDAG(), 1, timeWindowedStream, aggregateWindowStream, new MISTEdge(Direction.LEFT));
}
Also used : WindowData(edu.snu.mist.common.windows.WindowData) MISTEdge(edu.snu.mist.common.graph.MISTEdge) Test(org.junit.Test)

Example 4 with WindowData

use of edu.snu.mist.common.windows.WindowData in project mist by snuspl.

the class AggregateWindowOperatorTest method testAggregateWindowOperator.

/**
 * Test AggregateWindowOperator.
 * It calculates the maximum value from the collection.
 */
@Test
public void testAggregateWindowOperator() {
    // input stream events
    final WindowImpl<Integer> window = new WindowImpl<>(0L, 100L);
    window.putData(new MistDataEvent(10, 10));
    window.putData(new MistDataEvent(20, 20));
    window.putData(new MistDataEvent(15, 30));
    window.putData(new MistDataEvent(30, 90));
    final MistDataEvent dataEvent = new MistDataEvent(window, 90L);
    final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(101L);
    // functions that dealing with input WindowData
    final MISTFunction<WindowData<Integer>, String> aggregateFunc = (windowData) -> {
        return windowData.getDataCollection().toString() + ", " + windowData.getStart() + ", " + windowData.getEnd();
    };
    final AggregateWindowOperator<Integer, String> aggregateWindowOperator = new AggregateWindowOperator<>(aggregateFunc);
    final List<MistEvent> result = new LinkedList<>();
    aggregateWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    aggregateWindowOperator.processLeftData(dataEvent);
    Assert.assertEquals(1, result.size());
    Assert.assertTrue(result.get(0).isData());
    Assert.assertEquals("[10, 20, 15, 30], 0, 99", ((MistDataEvent) result.get(0)).getValue());
    Assert.assertEquals(90L, result.get(0).getTimestamp());
    aggregateWindowOperator.processLeftWatermark(watermarkEvent);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(watermarkEvent, result.get(1));
}
Also used : WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistDataEvent(edu.snu.mist.core.MistDataEvent) List(java.util.List) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) MISTFunction(edu.snu.mist.common.functions.MISTFunction) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistEvent(edu.snu.mist.core.MistEvent) AggregateWindowOperator(edu.snu.mist.core.operators.window.AggregateWindowOperator) Test(org.junit.Test) WindowData(edu.snu.mist.common.windows.WindowData) Assert(org.junit.Assert) LinkedList(java.util.LinkedList) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) WindowData(edu.snu.mist.common.windows.WindowData) AggregateWindowOperator(edu.snu.mist.core.operators.window.AggregateWindowOperator) MistDataEvent(edu.snu.mist.core.MistDataEvent) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) Test(org.junit.Test)

Example 5 with WindowData

use of edu.snu.mist.common.windows.WindowData in project mist by snuspl.

the class WindowAndAggregate method submitQuery.

/**
 * Submit a windowing and aggregating query.
 * The query reads strings from a source server, puts them into window,
 * concatenates all inputs in the window into a single string using toString function, and
 * print out the start and end time of the window.
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    // configurations for source and sink
    final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
    final SourceConfiguration localTextSocketSourceConf = MISTExampleUtils.getLocalTextSocketSourceConf(sourceSocket);
    // configurations for windowing and aggregation
    final int windowSize = 5000;
    final int windowEmissionInterval = 2500;
    final MISTFunction<WindowData<String>, String> aggregateFunc = (windowData) -> {
        return windowData.getDataCollection().toString() + ", window is started at " + windowData.getStart() + ", window is ended at " + windowData.getEnd() + ".";
    };
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.socketTextStream(localTextSocketSourceConf).window(new TimeWindowInformation(windowSize, windowEmissionInterval)).aggregateWindow(aggregateFunc).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : TimeWindowInformation(edu.snu.mist.common.windows.TimeWindowInformation) Tang(org.apache.reef.tang.Tang) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) MISTFunction(edu.snu.mist.common.functions.MISTFunction) NettySourceAddress(edu.snu.mist.examples.parameters.NettySourceAddress) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CommandLine(org.apache.reef.tang.formats.CommandLine) APIQueryControlResult(edu.snu.mist.client.APIQueryControlResult) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Configuration(org.apache.reef.tang.Configuration) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) WindowData(edu.snu.mist.common.windows.WindowData) InjectionException(org.apache.reef.tang.exceptions.InjectionException) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) WindowData(edu.snu.mist.common.windows.WindowData) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) TimeWindowInformation(edu.snu.mist.common.windows.TimeWindowInformation)

Aggregations

WindowData (edu.snu.mist.common.windows.WindowData)6 MISTFunction (edu.snu.mist.common.functions.MISTFunction)3 MistDataEvent (edu.snu.mist.core.MistDataEvent)3 Test (org.junit.Test)3 APIQueryControlResult (edu.snu.mist.client.APIQueryControlResult)2 MISTQueryBuilder (edu.snu.mist.client.MISTQueryBuilder)2 SourceConfiguration (edu.snu.mist.client.datastreams.configurations.SourceConfiguration)2 MistEvent (edu.snu.mist.core.MistEvent)2 MistWatermarkEvent (edu.snu.mist.core.MistWatermarkEvent)2 WindowImpl (edu.snu.mist.core.operators.window.WindowImpl)2 OutputBufferEmitter (edu.snu.mist.core.utils.OutputBufferEmitter)2 NettySourceAddress (edu.snu.mist.examples.parameters.NettySourceAddress)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Configuration (org.apache.reef.tang.Configuration)2 JavaConfigurationBuilder (org.apache.reef.tang.JavaConfigurationBuilder)2 Tang (org.apache.reef.tang.Tang)2 InjectionException (org.apache.reef.tang.exceptions.InjectionException)2