Search in sources :

Example 11 with MISTQueryBuilder

use of edu.snu.mist.client.MISTQueryBuilder in project mist by snuspl.

the class RuleBasedWeatherInfo method submitQuery.

/**
 * Submit a stateless query.
 * The query reads strings that represents location name and temperature from a source server,
 * filter the only location "Seoul" and "Paris". And send a message if the temperature is too hot or cold
 * to a sink server.
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
    final String[] source = sourceSocket.split(":");
    final String sourceHostname = source[0];
    final int sourcePort = Integer.parseInt(source[1]);
    final String firstField = "Location";
    final String secondField = "Temperature";
    final RuleBasedValueType firstFieldType = RuleBasedValueType.STRING;
    final RuleBasedValueType secondFieldType = RuleBasedValueType.DOUBLE;
    final String sourceSeparator = ",";
    /**
     * Make RuleBasedInput with sourceConfiguration.
     */
    final RuleBasedInput input = new RuleBasedInput.TextSocketBuilder().setSocketAddress(sourceHostname).setSocketPort(sourcePort).addField(firstField, firstFieldType).addField(secondField, secondFieldType).setSeparator(sourceSeparator).build();
    /**
     * Make RuleBasedSink with sinkConfiguration.
     */
    final RuleBasedSink sink = new RuleBasedSink.TextSocketBuilder().setSocketAddress(MISTExampleUtils.SINK_HOSTNAME).setSocketPort(MISTExampleUtils.SINK_PORT).build();
    /**
     * Make a RuleBasedQuery.
     */
    final MISTStatelessQuery ruleBasedQuery = new MISTStatelessQuery.Builder("example-group", "user1").input(input).addStatelessRule(new StatelessRule.Builder().setCondition(UnionCondition.and(UnionCondition.or(ComparisonCondition.eq("Location", "Seoul"), ComparisonCondition.eq("Location", "Paris")), ComparisonCondition.gt("Temperature", 30.0))).setAction(new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(sink).setParams("$Location is too hot!(Temperature : $Temperature)").build()).build()).addStatelessRule(new StatelessRule.Builder().setCondition(UnionCondition.and(UnionCondition.or(ComparisonCondition.eq("Location", "Seoul"), ComparisonCondition.eq("Location", "Paris")), ComparisonCondition.lt("Temperature", 0.0))).setAction(new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(sink).setParams("$Location is too cold!(Temperature : $Temperature)").build()).build()).build();
    /**
     * Translate cepStatelessQuery into MISTQuery
     */
    final MISTQueryBuilder queryBuilder = RuleBasedTranslator.statelessTranslator(ruleBasedQuery);
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder)

Example 12 with MISTQueryBuilder

use of edu.snu.mist.client.MISTQueryBuilder 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 13 with MISTQueryBuilder

use of edu.snu.mist.client.MISTQueryBuilder in project mist by snuspl.

the class CepHRMonitoring method submitQuery.

public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
    final String[] source = sourceSocket.split(":");
    final String sourceHostname = source[0];
    final int sourcePort = Integer.parseInt(source[1]);
    final CepInput<CepHRClass> input = new CepInput.TextSocketBuilder<CepHRClass>().setSocketAddress(sourceHostname).setSocketPort(sourcePort).setClassGenFunc(new CepHRClassGenFunc()).build();
    final CepSink sink = new CepSink.TextSocketBuilder().setSocketAddress(MISTExampleUtils.SINK_HOSTNAME).setSocketPort(MISTExampleUtils.SINK_PORT).build();
    final MISTPredicate<CepHRClass> conditionD = s -> s.getName().equals("D");
    final MISTPredicate<CepHRClass> conditionP = s -> s.getName().equals("P");
    final CepEventPattern<CepHRClass> eventD = new CepEventPattern.Builder<CepHRClass>().setName("doctor").setCondition(conditionD).setContiguity(CepEventContiguity.NON_DETERMINISTIC_RELAXED).setClass(CepHRClass.class).build();
    final CepEventPattern<CepHRClass> eventP = new CepEventPattern.Builder<CepHRClass>().setName("patient").setCondition(conditionP).setContiguity(CepEventContiguity.NON_DETERMINISTIC_RELAXED).setNOrMore(2).setInnerContiguity(CepEventContiguity.STRICT).setClass(CepHRClass.class).build();
    final MISTCepQuery<CepHRClass> cepQuery = new MISTCepQuery.Builder<CepHRClass>("demo-group", "user1").input(input).setEventPatternSequence(eventD, eventP).setQualifier(new CepHRQualifier()).within(3600000).setAction(new CepAction.Builder().setActionType(CepActionType.TEXT_WRITE).setCepSink(sink).setParams("Alert!").build()).build();
    final MISTQueryBuilder queryBuilder = CepUtils.translate(cepQuery);
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : Tang(org.apache.reef.tang.Tang) MISTFunction(edu.snu.mist.common.functions.MISTFunction) MISTPredicate(edu.snu.mist.common.functions.MISTPredicate) NettySourceAddress(edu.snu.mist.examples.parameters.NettySourceAddress) edu.snu.mist.client.cep(edu.snu.mist.client.cep) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CommandLine(org.apache.reef.tang.formats.CommandLine) APIQueryControlResult(edu.snu.mist.client.APIQueryControlResult) CepEventPattern(edu.snu.mist.common.cep.CepEventPattern) CepEventContiguity(edu.snu.mist.common.cep.CepEventContiguity) List(java.util.List) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Configuration(org.apache.reef.tang.Configuration) Map(java.util.Map) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) InjectionException(org.apache.reef.tang.exceptions.InjectionException) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) CepEventPattern(edu.snu.mist.common.cep.CepEventPattern) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder)

Example 14 with MISTQueryBuilder

use of edu.snu.mist.client.MISTQueryBuilder in project mist by snuspl.

the class ContinuousStreamTest method setUp.

@Before
public void setUp() {
    queryBuilder = new MISTQueryBuilder();
    queryBuilder.setApplicationId(TestParameters.SUPER_GROUP_ID);
    sourceStream = queryBuilder.socketTextStream(TestParameters.LOCAL_TEXT_SOCKET_SOURCE_CONF);
    filteredStream = sourceStream.filter(defaultFilter);
    filteredMappedStream = filteredStream.map(defaultMap);
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Before(org.junit.Before)

Example 15 with MISTQueryBuilder

use of edu.snu.mist.client.MISTQueryBuilder in project mist by snuspl.

the class RuleBasedTranslator method statefulTranslator.

/**
 * Translate stateful query into MIST query.
 * @param query RuleBasedStatefulQuery
 * @return translated MIST query
 */
public static MISTQueryBuilder statefulTranslator(final MISTStatefulQuery query) {
    final RuleBasedInput input = query.getInput();
    final String initialState = query.getInitialState();
    final List<StatefulRule> statefulRules = query.getStatefulRules();
    final Map<String, RuleBasedAction> finalStates = query.getFinalState();
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    final ContinuousStream<Map<String, Object>> inputMapStream = inputTranslator(queryBuilder, input);
    statefulRulesTranslator(inputMapStream, initialState, statefulRules, finalStates);
    return queryBuilder;
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder)

Aggregations

MISTQueryBuilder (edu.snu.mist.client.MISTQueryBuilder)25 JavaConfigurationBuilder (org.apache.reef.tang.JavaConfigurationBuilder)17 IOException (java.io.IOException)14 InjectionException (org.apache.reef.tang.exceptions.InjectionException)14 URISyntaxException (java.net.URISyntaxException)13 Tang (org.apache.reef.tang.Tang)13 SourceConfiguration (edu.snu.mist.client.datastreams.configurations.SourceConfiguration)12 APIQueryControlResult (edu.snu.mist.client.APIQueryControlResult)11 Configuration (org.apache.reef.tang.Configuration)11 CommandLine (org.apache.reef.tang.formats.CommandLine)11 NettySourceAddress (edu.snu.mist.examples.parameters.NettySourceAddress)8 Tuple2 (edu.snu.mist.common.types.Tuple2)6 Injector (org.apache.reef.tang.Injector)5 MISTFunction (edu.snu.mist.common.functions.MISTFunction)4 ContinuousStream (edu.snu.mist.client.datastreams.ContinuousStream)3 MISTBiFunction (edu.snu.mist.common.functions.MISTBiFunction)3 TimeWindowInformation (edu.snu.mist.common.windows.TimeWindowInformation)3 MISTQuery (edu.snu.mist.client.MISTQuery)2 CepEventPattern (edu.snu.mist.common.cep.CepEventPattern)2 ApplyStatefulFunction (edu.snu.mist.common.functions.ApplyStatefulFunction)2