use of org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator in project apex-malhar by apache.
the class Application method populateDAG.
@SuppressWarnings("unchecked")
@Override
public void populateDAG(DAG dag, Configuration conf) {
try {
URI duri = PubSubHelper.getURIWithDefault(dag, "localhost:9090");
PubSubWebSocketInputOperator userTxWsInput = getPubSubWebSocketInputOperator("userTxInput", dag, duri, "examples.app.frauddetect.submitTransaction");
PubSubWebSocketOutputOperator ccUserAlertWsOutput = getPubSubWebSocketOutputOperator("ccUserAlertQueryOutput", dag, duri, "examples.app.frauddetect.fraudAlert");
PubSubWebSocketOutputOperator avgUserAlertwsOutput = getPubSubWebSocketOutputOperator("avgUserAlertQueryOutput", dag, duri, "examples.app.frauddetect.fraudAlert");
PubSubWebSocketOutputOperator binUserAlertwsOutput = getPubSubWebSocketOutputOperator("binUserAlertOutput", dag, duri, "examples.app.frauddetect.fraudAlert");
PubSubWebSocketOutputOperator txSummaryWsOutput = getPubSubWebSocketOutputOperator("txSummaryWsOutput", dag, duri, "examples.app.frauddetect.txSummary");
SlidingWindowSumKeyVal<KeyValPair<MerchantKey, String>, Integer> smsOperator = dag.addOperator("movingSum", SlidingWindowSumKeyVal.class);
MerchantTransactionGenerator txReceiver = dag.addOperator("txReceiver", MerchantTransactionGenerator.class);
MerchantTransactionInputHandler txInputHandler = dag.addOperator("txInputHandler", new MerchantTransactionInputHandler());
BankIdNumberSamplerOperator binSampler = dag.addOperator("bankInfoFraudDetector", BankIdNumberSamplerOperator.class);
MerchantTransactionBucketOperator txBucketOperator = dag.addOperator("txFilter", MerchantTransactionBucketOperator.class);
RangeKeyVal rangeOperator = dag.addOperator("rangePerMerchant", new RangeKeyVal<MerchantKey, Long>());
SimpleMovingAverage<MerchantKey, Long> smaOperator = dag.addOperator("smaPerMerchant", SimpleMovingAverage.class);
TransactionStatsAggregator txStatsAggregator = dag.addOperator("txStatsAggregator", TransactionStatsAggregator.class);
AverageAlertingOperator avgAlertingOperator = dag.addOperator("avgAlerter", AverageAlertingOperator.class);
CreditCardAmountSamplerOperator ccSamplerOperator = dag.addOperator("amountFraudDetector", CreditCardAmountSamplerOperator.class);
HdfsStringOutputOperator hdfsOutputOperator = getHdfsOutputOperator("hdfsOutput", dag, "fraud");
MongoDBOutputOperator mongoTxStatsOperator = dag.addOperator("mongoTxStatsOutput", MongoDBOutputOperator.class);
MongoDBOutputOperator mongoBinAlertsOperator = dag.addOperator("mongoBinAlertsOutput", MongoDBOutputOperator.class);
MongoDBOutputOperator mongoCcAlertsOperator = dag.addOperator("mongoCcAlertsOutput", MongoDBOutputOperator.class);
MongoDBOutputOperator mongoAvgAlertsOperator = dag.addOperator("mongoAvgAlertsOutput", MongoDBOutputOperator.class);
dag.addStream("userTxStream", userTxWsInput.outputPort, txInputHandler.userTxInputPort);
dag.addStream("transactions", txReceiver.txOutputPort, txBucketOperator.inputPort).setLocality(DAG.Locality.CONTAINER_LOCAL);
// dump all tx into Hdfs
dag.addStream("txData", txReceiver.txDataOutputPort, hdfsOutputOperator.input);
dag.addStream("userTransactions", txInputHandler.txOutputPort, txBucketOperator.txUserInputPort);
dag.addStream("bankInfoData", txBucketOperator.binCountOutputPort, smsOperator.data);
dag.addStream("bankInfoCount", smsOperator.integerSum, binSampler.txCountInputPort);
dag.addStream("filteredTransactions", txBucketOperator.txOutputPort, rangeOperator.data, smaOperator.data, avgAlertingOperator.txInputPort);
KeyPartitionCodec<MerchantKey, Long> txCodec = new KeyPartitionCodec<MerchantKey, Long>();
dag.setInputPortAttribute(rangeOperator.data, Context.PortContext.STREAM_CODEC, txCodec);
dag.setInputPortAttribute(smaOperator.data, Context.PortContext.STREAM_CODEC, txCodec);
dag.setInputPortAttribute(avgAlertingOperator.txInputPort, Context.PortContext.STREAM_CODEC, txCodec);
dag.addStream("creditCardData", txBucketOperator.ccAlertOutputPort, ccSamplerOperator.inputPort);
dag.addStream("txnSummaryData", txBucketOperator.summaryTxnOutputPort, txSummaryWsOutput.input);
dag.addStream("smaAlerts", smaOperator.doubleSMA, avgAlertingOperator.smaInputPort);
dag.addStream("binAlerts", binSampler.countAlertOutputPort, mongoBinAlertsOperator.inputPort);
dag.addStream("binAlertsNotification", binSampler.countAlertNotificationPort, binUserAlertwsOutput.input);
dag.addStream("rangeData", rangeOperator.range, txStatsAggregator.rangeInputPort);
dag.addStream("smaData", smaOperator.longSMA, txStatsAggregator.smaInputPort);
dag.addStream("txStatsOutput", txStatsAggregator.txDataOutputPort, mongoTxStatsOperator.inputPort);
dag.addStream("avgAlerts", avgAlertingOperator.avgAlertOutputPort, mongoAvgAlertsOperator.inputPort);
dag.addStream("avgAlertsNotification", avgAlertingOperator.avgAlertNotificationPort, avgUserAlertwsOutput.input);
dag.addStream("ccAlerts", ccSamplerOperator.ccAlertOutputPort, mongoCcAlertsOperator.inputPort);
dag.addStream("ccAlertsNotification", ccSamplerOperator.ccAlertNotificationPort, ccUserAlertWsOutput.input);
} catch (Exception exc) {
DTThrowable.rethrow(exc);
}
}
use of org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator in project apex-malhar by apache.
the class ApplicationTest method testGetApplication.
/**
* Test of getApplication method, of class Application.
*/
@Test
public void testGetApplication() throws Exception {
Configuration conf = new Configuration(false);
conf.addResource("dt-site-mobile.xml");
Server server = new Server(0);
Servlet servlet = new SamplePubSubWebSocketServlet();
ServletHolder sh = new ServletHolder(servlet);
ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
contextHandler.addServlet(sh, "/pubsub");
contextHandler.addServlet(sh, "/*");
server.start();
Connector[] connector = server.getConnectors();
conf.set("dt.attr.GATEWAY_CONNECT_ADDRESS", "localhost:" + connector[0].getLocalPort());
URI uri = PubSubHelper.getURI("localhost:" + connector[0].getLocalPort());
PubSubWebSocketOutputOperator<Object> outputOperator = new PubSubWebSocketOutputOperator<Object>();
outputOperator.setUri(uri);
outputOperator.setTopic(conf.get("dt.application.MobileExample.operator.QueryLocation.topic"));
PubSubWebSocketInputOperator<Map<String, String>> inputOperator = new PubSubWebSocketInputOperator<Map<String, String>>();
inputOperator.setUri(uri);
inputOperator.setTopic(conf.get("dt.application.MobileExample.operator.LocationResults.topic"));
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
inputOperator.outputPort.setSink(sink);
Map<String, String> data = new HashMap<String, String>();
data.put("command", "add");
data.put("phone", "5559990");
Application app = new Application();
LocalMode lma = LocalMode.newInstance();
lma.prepareDAG(app, conf);
LocalMode.Controller lc = lma.getController();
lc.setHeartbeatMonitoringEnabled(false);
lc.runAsync();
Thread.sleep(5000);
inputOperator.setup(null);
outputOperator.setup(null);
inputOperator.activate(null);
outputOperator.beginWindow(0);
outputOperator.input.process(data);
outputOperator.endWindow();
inputOperator.beginWindow(0);
int timeoutMillis = 5000;
while (sink.collectedTuples.size() < 5 && timeoutMillis > 0) {
inputOperator.emitTuples();
timeoutMillis -= 20;
Thread.sleep(20);
}
inputOperator.endWindow();
lc.shutdown();
inputOperator.teardown();
outputOperator.teardown();
server.stop();
Assert.assertTrue("size of output is 5 ", sink.collectedTuples.size() == 5);
for (Object obj : sink.collectedTuples) {
Assert.assertEquals("Expected phone number", "5559990", ((Map<String, String>) obj).get("phone"));
}
}
use of org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator in project apex-malhar by apache.
the class Application method getPubSubWebSocketOutputOperator.
public PubSubWebSocketOutputOperator getPubSubWebSocketOutputOperator(String name, DAG dag, URI duri, String topic) throws Exception {
PubSubWebSocketOutputOperator out = dag.addOperator(name, new PubSubWebSocketOutputOperator());
out.setUri(duri);
return out;
}
Aggregations