Search in sources :

Example 16 with MISTQueryBuilder

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

the class RuleBasedTranslator method statelessTranslator.

/**
 * Translate stateless query into MIST query.
 * @param query StatelessQuery
 * @return translated Mist query
 */
public static MISTQueryBuilder statelessTranslator(final MISTStatelessQuery query) {
    final RuleBasedInput input = query.getInput();
    final List<StatelessRule> statelessRules = query.getStatelessRules();
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    final ContinuousStream<Map<String, Object>> inputMapStream = inputTranslator(queryBuilder, input);
    statelessRulesTranslator(inputMapStream, statelessRules);
    return queryBuilder;
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder)

Example 17 with MISTQueryBuilder

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

the class GroupRecoveryTest method buildQuery.

/**
 * Builds the query for this test.
 * @return the built MISTQuery
 */
private MISTQuery buildQuery() {
    /**
     * Build the query which consists of:
     * SRC1 -> toTuple -> reduceByKey -> sort -> toString -> sink1
     */
    final SourceConfiguration localTextSocketSource1Conf = TextSocketSourceConfiguration.newBuilder().setHostAddress("localhost").setHostPort(16118).build();
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.setApplicationId("test-group");
    final int defaultWatermarkPeriod = 100;
    final WatermarkConfiguration testConf = PeriodicWatermarkConfiguration.newBuilder().setWatermarkPeriod(defaultWatermarkPeriod).build();
    queryBuilder.socketTextStream(localTextSocketSource1Conf, testConf).map(toTupleMapFunc).reduceByKey(0, String.class, countFunc).map(m -> new TreeMap<>(m)).map(toStringMapFunc).textSocketOutput("localhost", SINK_PORT);
    return queryBuilder.build();
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) TextSocketSourceConfiguration(edu.snu.mist.client.datastreams.configurations.TextSocketSourceConfiguration) PeriodicWatermarkConfiguration(edu.snu.mist.client.datastreams.configurations.PeriodicWatermarkConfiguration) WatermarkConfiguration(edu.snu.mist.client.datastreams.configurations.WatermarkConfiguration)

Example 18 with MISTQueryBuilder

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

the class MQTTNoiseSensing 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 SourceConfiguration localMQTTSourceConf = MISTExampleUtils.getMQTTSourceConf("MISTExampleSub", brokerURI);
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    final ContinuousStream<Integer> sensedData = queryBuilder.mqttStream(localMQTTSourceConf).map((mqttMessage) -> Integer.parseInt(new String(mqttMessage.getPayload())));
    final ContinuousStream<Integer> noisy = sensedData.filter((value) -> value < 200);
    final ContinuousStream<Integer> calm = sensedData.filter((value) -> value >= 200);
    // Text socket output
    noisy.map((loudValue) -> new StringBuilder().append("It's noisy! The value was ").append(loudValue).toString()).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
    // MQTT output
    noisy.map((value) -> new MqttMessage("ON".getBytes())).mqttOutput(brokerURI, "MISTExamplePub");
    calm.map((value) -> new MqttMessage("OFF".getBytes())).mqttOutput(brokerURI, "MISTExamplePub");
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Tang(org.apache.reef.tang.Tang) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CommandLine(org.apache.reef.tang.formats.CommandLine) ContinuousStream(edu.snu.mist.client.datastreams.ContinuousStream) APIQueryControlResult(edu.snu.mist.client.APIQueryControlResult) TestMQTTBrokerURI(edu.snu.mist.examples.parameters.TestMQTTBrokerURI) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Configuration(org.apache.reef.tang.Configuration) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) InjectionException(org.apache.reef.tang.exceptions.InjectionException) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration)

Example 19 with MISTQueryBuilder

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

the class QueryDeletion method submitQuery.

/**
 * Submit a query containing reduce-by-key operator.
 * The query reads strings from a source server, filters alphabetical words,
 * counts words using reduce-by-key operator, and sends them 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 SourceConfiguration localTextSocketSourceConf = MISTExampleUtils.getLocalTextSocketSourceConf(sourceSocket);
    // Simple reduce function.
    final MISTBiFunction<Integer, Integer, Integer> reduceFunction = (v1, v2) -> {
        return v1 + v2;
    };
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.socketTextStream(localTextSocketSourceConf).filter(s -> isAlpha(s)).map(s -> new Tuple2(s, 1)).reduceByKey(0, String.class, reduceFunction).map(s -> s.toString()).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : MISTQueryControl(edu.snu.mist.client.MISTQueryControl) Tang(org.apache.reef.tang.Tang) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) NettySourceAddress(edu.snu.mist.examples.parameters.NettySourceAddress) Tuple2(edu.snu.mist.common.types.Tuple2) 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) StringUtils.isAlpha(org.apache.commons.lang3.StringUtils.isAlpha) Configuration(org.apache.reef.tang.Configuration) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) MISTBiFunction(edu.snu.mist.common.functions.MISTBiFunction) InjectionException(org.apache.reef.tang.exceptions.InjectionException) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Tuple2(edu.snu.mist.common.types.Tuple2) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration)

Example 20 with MISTQueryBuilder

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

the class RuleBasedHelloMist method submitQuery.

/**
 * Submit a stateless query.
 * The query reads strings from a source server, filter "ID" which is "HelloMIST",
 * and send "Message" field's data to sink.
 * @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 = "ID";
    final String secondField = "Message";
    final RuleBasedValueType firstFieldType = RuleBasedValueType.STRING;
    final RuleBasedValueType secondFieldType = RuleBasedValueType.STRING;
    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 stateless query.
     */
    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)

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