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;
}
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();
}
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);
}
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);
}
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);
}
Aggregations