use of edu.snu.mist.common.functions.MISTFunction in project mist by snuspl.
the class NettySourceTest method testPunctuatedNettyTextSource.
/**
* Test whether the created source using DataGenerator by NettyTextDataGeneratorFactory receive event-time data
* correctly from netty server, and generate proper punctuated watermark and outputs.
* It creates 4 sources each having data generator using Netty server.
* @throws Exception
*/
@Test(timeout = 4000L)
public void testPunctuatedNettyTextSource() throws Exception {
final int numSources = 4;
final int numData = 3;
final int numWatermark = 2;
final List<String> inputStreamWithTimestamp = Arrays.asList("Lorem ipsum dolor sit amet, consectetur adipiscing elit.:100", "In in leo nec erat fringilla mattis eu non massa.:800", "Watermark:1000", "Cras quis diam suscipit, commodo enim id, pulvinar nunc.:1200", "Watermark:1500");
final List<String> expectedDataWithoutTimestamp = Arrays.asList("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "In in leo nec erat fringilla mattis eu non massa.", "Cras quis diam suscipit, commodo enim id, pulvinar nunc.");
final List<Long> expectedPunctuatedWatermark = Arrays.asList(1000L, 1500L);
final CountDownLatch dataCountDownLatch = new CountDownLatch(numSources * numData);
final CountDownLatch watermarkCountDownLatch = new CountDownLatch(numSources * numWatermark);
final CountDownLatch channelCountDown = new CountDownLatch(numSources);
LOG.log(Level.FINE, "Count down data: {0}", dataCountDownLatch);
LOG.log(Level.FINE, "Count down watermark: {0}", watermarkCountDownLatch);
// create netty server
try (final NettyTextMessageStreamGenerator textMessageStreamGenerator = new NettyTextMessageStreamGenerator(SERVER_ADDR, SERVER_PORT, new TestChannelHandler(channelCountDown))) {
final Injector injector = Tang.Factory.getTang().newInjector();
// source list
final List<Tuple<DataGenerator, EventGenerator>> sources = new LinkedList<>();
// result data list
final List<List<String>> punctuatedDataResults = new LinkedList<>();
// result watermark list
final List<List<Long>> punctuatedWatermarkResults = new LinkedList<>();
// Create sources having punctuated watermark
for (int i = 0; i < numSources; i++) {
final DataGenerator<String> dataGenerator = new NettyTextDataGenerator(SERVER_ADDR, SERVER_PORT, nettySharedResource);
final MISTFunction<String, Tuple<String, Long>> extractFunc = (input) -> new Tuple<>(input.toString().split(":")[0], Long.parseLong(input.toString().split(":")[1]));
final MISTPredicate<String> isWatermark = (input) -> input.toString().split(":")[0].equals("Watermark");
final WatermarkTimestampFunction<String> parseTsFunc = (input) -> Long.parseLong(input.toString().split(":")[1]);
final EventGenerator<String> eventGenerator = new PunctuatedEventGenerator<>(extractFunc, isWatermark, parseTsFunc, 0, null, null);
sources.add(new Tuple<>(dataGenerator, eventGenerator));
dataGenerator.setEventGenerator(eventGenerator);
final List<String> receivedData = new LinkedList<>();
final List<Long> receivedWatermark = new LinkedList<>();
punctuatedDataResults.add(receivedData);
punctuatedWatermarkResults.add(receivedWatermark);
eventGenerator.setOutputEmitter(new SourceTestOutputEmitter<>(receivedData, receivedWatermark, dataCountDownLatch, watermarkCountDownLatch));
}
// Start to receive data stream from stream generator
for (final Tuple<DataGenerator, EventGenerator> source : sources) {
// start event generator
source.getValue().start();
// start data generator
source.getKey().start();
}
// Wait until all sources connect to stream generator
channelCountDown.await();
inputStreamWithTimestamp.forEach(textMessageStreamGenerator::write);
// Wait until all data are sent to source
dataCountDownLatch.await();
watermarkCountDownLatch.await();
for (final List<String> received : punctuatedDataResults) {
Assert.assertEquals(expectedDataWithoutTimestamp, received);
}
for (final List<Long> received : punctuatedWatermarkResults) {
Assert.assertEquals(expectedPunctuatedWatermark, received);
}
// Closes
for (final Tuple<DataGenerator, EventGenerator> source : sources) {
// stop data generator
source.getKey().close();
// stop event generator
source.getValue().close();
}
}
}
use of edu.snu.mist.common.functions.MISTFunction 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));
}
use of edu.snu.mist.common.functions.MISTFunction 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);
}
Aggregations