Search in sources :

Example 6 with MISTQueryBuilder

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

the class MqttFilterApplication method submitQuery.

public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    final Injector injector = Tang.Factory.getTang().newInjector(configuration);
    final String brokerURI = injector.getNamedInstance(TestMQTTBrokerURI.class);
    final String sourceTopic = injector.getNamedInstance(MqttSourceTopic.class);
    final String sinkTopic = injector.getNamedInstance(MqttSinkTopic.class);
    final String filteredString = injector.getNamedInstance(FilteredString.class);
    final String appId = injector.getNamedInstance(ApplicationIdentifier.class);
    final SourceConfiguration mqttSourceConf = new MQTTSourceConfiguration.MQTTSourceConfigurationBuilder().setBrokerURI(brokerURI).setTopic(sourceTopic).build();
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.setApplicationId(appId).mqttStream(mqttSourceConf).map(new MqttMessageToStringFunc()).filter(new FilterFunc(filteredString)).map(new MapFunc(filteredString)).mqttOutput(brokerURI, sinkTopic);
    final String[] driverSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(MasterAddress.class).split(":");
    final String driverHostname = driverSocket[0];
    final int driverPort = Integer.parseInt(driverSocket[1]);
    try (final MISTExecutionEnvironment executionEnvironment = new MISTDefaultExecutionEnvironmentImpl(driverHostname, driverPort)) {
        return executionEnvironment.submitQuery(queryBuilder.build());
    } catch (final Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) InjectionException(org.apache.reef.tang.exceptions.InjectionException) Injector(org.apache.reef.tang.Injector) MQTTSourceConfiguration(edu.snu.mist.client.datastreams.configurations.MQTTSourceConfiguration) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) MISTDefaultExecutionEnvironmentImpl(edu.snu.mist.client.MISTDefaultExecutionEnvironmentImpl) MISTExecutionEnvironment(edu.snu.mist.client.MISTExecutionEnvironment) MQTTSourceConfiguration(edu.snu.mist.client.datastreams.configurations.MQTTSourceConfiguration)

Example 7 with MISTQueryBuilder

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

the class RuleBasedMQTTHelloMist method submitQuery.

/**
 * Submit a rule-based stateless query.
 * The query reads strings from a mqtt source server, filter "ID" which is "HelloMIST",
 * and send "Message" field's data to mqtt sink.
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    final String brokerURI = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(TestMQTTBrokerURI.class);
    final String sourceTopic = "MISTExampleSub";
    final String sinkTopic = "MISTExamplePub";
    final String firstField = "ID";
    final String secondField = "Message";
    final RuleBasedValueType firstFieldType = RuleBasedValueType.STRING;
    final RuleBasedValueType secondFieldType = RuleBasedValueType.STRING;
    final String sourceSeparator = ":";
    /**
     * Make rule-based Input with sourceConfiguration.
     */
    final RuleBasedInput input = new RuleBasedInput.MqttBuilder().setMqttBrokerURI(brokerURI).setMqttTopic(sourceTopic).addField(firstField, firstFieldType).addField(secondField, secondFieldType).setSeparator(sourceSeparator).build();
    /**
     * Make RuleBasedSink with sinkConfiguration.
     */
    final RuleBasedSink sink = new RuleBasedSink.MqttBuilder().setMqttBrokerURI(brokerURI).setMqttTopic(sinkTopic).build();
    /**
     * Make a RuleBasedQuery.
     */
    final MISTStatelessQuery ruleBasedQuery = new MISTStatelessQuery.Builder("example-group", "user1").input(input).addStatelessRule(new StatelessRule.Builder().setCondition(ComparisonCondition.eq("ID", "HelloMIST")).setAction(new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(sink).setParams("$Message").build()).build()).build();
    /**
     * Translate statelessQuery 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 8 with MISTQueryBuilder

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

the class RuleBasedMQTTNoiseSensing method submitQuery.

/**
 * Submit a query fetching data from a MQTT source.
 * The query reads strings from a MQTT topic and send them to a sink.
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    final String brokerURI = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(TestMQTTBrokerURI.class);
    final String sourceTopic = "MISTExampleSub";
    final String sinkTopic = "MISTExamplePub";
    final String firstField = "Noisy_sensor";
    final RuleBasedValueType firstFieldType = RuleBasedValueType.INTEGER;
    final int noiseLimit = 200;
    /**
     * Make RuleBasedInput with sourceConfiguartion
     */
    final RuleBasedInput input = new RuleBasedInput.MqttBuilder().setMqttBrokerURI(brokerURI).setMqttTopic(sourceTopic).addField(firstField, firstFieldType).build();
    /**
     * Make RuleBasedSink with sinkConfiguration.
     */
    final RuleBasedSink mqttSink = new RuleBasedSink.MqttBuilder().setMqttBrokerURI(brokerURI).setMqttTopic(sinkTopic).build();
    final RuleBasedSink socketSink = new RuleBasedSink.TextSocketBuilder().setSocketAddress(MISTExampleUtils.SINK_HOSTNAME).setSocketPort(MISTExampleUtils.SINK_PORT).build();
    /**
     * Make a stateless rule Query.
     */
    final MISTStatelessQuery ruleBasedQuery = new MISTStatelessQuery.Builder("example-group", "user1").input(input).addStatelessRule(new StatelessRule.Builder().setCondition(ComparisonCondition.lt(firstField, noiseLimit)).setAction(new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(mqttSink).setParams("ON").build()).build()).addStatelessRule(new StatelessRule.Builder().setCondition(ComparisonCondition.ge(firstField, noiseLimit)).setAction(new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(mqttSink).setParams("OFF").build()).build()).addStatelessRule(new StatelessRule.Builder().setCondition(ComparisonCondition.lt(firstField, noiseLimit)).setAction(new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(socketSink).setParams("It's noisy! The value was $Noisy_sensor").build()).build()).build();
    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 9 with MISTQueryBuilder

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

the class DefaultDagGeneratorImplTest method testPlanGenerator.

/**
 * Round-trip test of de-serializing AvroOperatorChainDag.
 * @throws org.apache.reef.tang.exceptions.InjectionException
 */
@Test
public void testPlanGenerator() throws InjectionException, IOException, URISyntaxException, ClassNotFoundException {
    // Generate a query
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.setApplicationId(TestParameters.SUPER_GROUP_ID);
    queryBuilder.socketTextStream(TestParameters.LOCAL_TEXT_SOCKET_SOURCE_CONF).flatMap(s -> Arrays.asList(s.split(" "))).filter(s -> s.startsWith("A")).map(s -> new Tuple2<>(s, 1)).reduceByKey(0, String.class, (Integer x, Integer y) -> x + y).textSocketOutput(TestParameters.HOST, TestParameters.SINK_PORT);
    final MISTQuery query = queryBuilder.build();
    // Generate avro operator chain dag
    final Tuple<List<AvroVertex>, List<Edge>> serializedDag = query.getAvroOperatorDag();
    final AvroDag.Builder avroDagBuilder = AvroDag.newBuilder();
    final AvroDag avroChainedDag = avroDagBuilder.setAppId(TestParameters.SUPER_GROUP_ID).setQueryId(TestParameters.QUERY_ID).setJarPaths(new ArrayList<>()).setAvroVertices(serializedDag.getKey()).setEdges(serializedDag.getValue()).build();
    final JavaConfigurationBuilder jcb = Tang.Factory.getTang().newConfigurationBuilder();
    jcb.bindNamedParameter(TaskHostname.class, "127.0.0.1");
    final Injector injector = Tang.Factory.getTang().newInjector(jcb.build());
    final ConfigDagGenerator configDagGenerator = injector.getInstance(ConfigDagGenerator.class);
    final DagGenerator dagGenerator = injector.getInstance(DagGenerator.class);
    final Tuple<String, AvroDag> tuple = new Tuple<>("query-test", avroChainedDag);
    final DAG<ConfigVertex, MISTEdge> configDag = configDagGenerator.generate(tuple.getValue());
    final ExecutionDag executionDag = dagGenerator.generate(configDag, new LinkedList<>());
    // Test execution dag
    final DAG<ExecutionVertex, MISTEdge> dag = executionDag.getDag();
    final Set<ExecutionVertex> sources = dag.getRootVertices();
    Assert.assertEquals(1, sources.size());
    Assert.assertTrue(sources.iterator().next() instanceof PhysicalSource);
    final PhysicalSource source = (PhysicalSource) sources.iterator().next();
    final Map<ExecutionVertex, MISTEdge> nextOps = dag.getEdges(source);
    Assert.assertEquals(1, nextOps.size());
    final PhysicalOperator flatMapOp = (PhysicalOperator) nextOps.entrySet().iterator().next().getKey();
    final PhysicalOperator filterOp = (PhysicalOperator) dag.getEdges(flatMapOp).entrySet().iterator().next().getKey();
    final PhysicalOperator mapOp = (PhysicalOperator) dag.getEdges(filterOp).entrySet().iterator().next().getKey();
    final PhysicalOperator reduceByKeyOp = (PhysicalOperator) dag.getEdges(mapOp).entrySet().iterator().next().getKey();
    final PhysicalSink sink = (PhysicalSink) dag.getEdges(reduceByKeyOp).entrySet().iterator().next().getKey();
    Assert.assertTrue(flatMapOp.getOperator() instanceof FlatMapOperator);
    Assert.assertTrue(filterOp.getOperator() instanceof FilterOperator);
    Assert.assertTrue(mapOp.getOperator() instanceof MapOperator);
    Assert.assertTrue(reduceByKeyOp.getOperator() instanceof ReduceByKeyOperator);
    Assert.assertTrue(sink.getSink() instanceof NettyTextSink);
}
Also used : Injector(org.apache.reef.tang.Injector) java.util(java.util) NettyTextSink(edu.snu.mist.core.sinks.NettyTextSink) Tuple2(edu.snu.mist.common.types.Tuple2) URISyntaxException(java.net.URISyntaxException) MISTEdge(edu.snu.mist.common.graph.MISTEdge) AvroDag(edu.snu.mist.formats.avro.AvroDag) MISTQuery(edu.snu.mist.client.MISTQuery) ReduceByKeyOperator(edu.snu.mist.core.operators.ReduceByKeyOperator) ServerSocket(java.net.ServerSocket) DAG(edu.snu.mist.common.graph.DAG) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) After(org.junit.After) MapOperator(edu.snu.mist.core.operators.MapOperator) Before(org.junit.Before) Tang(org.apache.reef.tang.Tang) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test) IOException(java.io.IOException) TestParameters(edu.snu.mist.core.utils.TestParameters) TaskHostname(edu.snu.mist.core.parameters.TaskHostname) FilterOperator(edu.snu.mist.core.operators.FilterOperator) Edge(edu.snu.mist.formats.avro.Edge) FlatMapOperator(edu.snu.mist.core.operators.FlatMapOperator) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) AvroVertex(edu.snu.mist.formats.avro.AvroVertex) InjectionException(org.apache.reef.tang.exceptions.InjectionException) Assert(org.junit.Assert) FlatMapOperator(edu.snu.mist.core.operators.FlatMapOperator) NettyTextSink(edu.snu.mist.core.sinks.NettyTextSink) AvroDag(edu.snu.mist.formats.avro.AvroDag) MapOperator(edu.snu.mist.core.operators.MapOperator) FlatMapOperator(edu.snu.mist.core.operators.FlatMapOperator) Injector(org.apache.reef.tang.Injector) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) MISTEdge(edu.snu.mist.common.graph.MISTEdge) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) FilterOperator(edu.snu.mist.core.operators.FilterOperator) ReduceByKeyOperator(edu.snu.mist.core.operators.ReduceByKeyOperator) MISTQuery(edu.snu.mist.client.MISTQuery) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test)

Example 10 with MISTQueryBuilder

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

the class RuleBasedStatefulExample method submitQuery.

/**
 * Submit a stateless query.
 * The query reads location and temperature from a source server.
 * First, the query only accept INSIDE event to go INSIDE.
 * When the location is changed, the state would be changed and send the mode to a sink server.
 * After the state is changed into INSIDE,
 * the state is changed only with the temperature value, ignoring the location value.
 * If the temperature is not in the appropriate range, alarmed message would be sent to a sink server.
 * The appropriate temperature of INSIDE is 5~25.
 * @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.INTEGER;
    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 StatefulQuery.
     */
    final MISTStatefulQuery ruleBasedQuery = new MISTStatefulQuery.Builder("example-group", "user1").input(input).initialState("OUTSIDE").addStatefulRule(new StatefulRule.Builder().setCurrentState("OUTSIDE").addTransition(ComparisonCondition.eq("Location", "INSIDE"), "INSIDE").build()).addStatefulRule(new StatefulRule.Builder().setCurrentState("INSIDE").addTransition(UnionCondition.or(ComparisonCondition.lt("Temperature", 5), ComparisonCondition.gt("Temperature", 25)), "INSIDE(ALARM)").addTransition(UnionCondition.and(ComparisonCondition.ge("Temperature", 5), ComparisonCondition.le("Temperature", 25)), "INSIDE(0)").build()).addStatefulRule(new StatefulRule.Builder().setCurrentState("INSIDE(ALARM)").addTransition(UnionCondition.and(ComparisonCondition.ge("Temperature", 5), ComparisonCondition.le("Temperature", 25)), "INSIDE(0)").build()).addStatefulRule(new StatefulRule.Builder().setCurrentState("INSIDE(0)").addTransition(UnionCondition.or(ComparisonCondition.lt("Temperature", 5), ComparisonCondition.gt("Temperature", 25)), "INSIDE(ALARM)").build()).addFinalState("INSIDE", new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(sink).setParams("GO INSIDE").build()).addFinalState("INSIDE(ALARM)", new RuleBasedAction.Builder().setActionType(RuleBasedActionType.TEXT_WRITE).setSink(sink).setParams("INSIDE ALARM!(Temperature: $Temperature)").build()).build();
    /**
     * Translate statelessQuery into MISTQuery
     */
    final MISTQueryBuilder queryBuilder = RuleBasedTranslator.statefulTranslator(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)

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