Search in sources :

Example 1 with PubSubWebSocketInputOperator

use of org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator 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);
    }
}
Also used : PubSubWebSocketOutputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator) URI(java.net.URI) PubSubWebSocketInputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator) RangeKeyVal(org.apache.apex.malhar.lib.math.RangeKeyVal) MongoDBOutputOperator(org.apache.apex.examples.frauddetect.operator.MongoDBOutputOperator) HdfsStringOutputOperator(org.apache.apex.examples.frauddetect.operator.HdfsStringOutputOperator) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair)

Example 2 with PubSubWebSocketInputOperator

use of org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator 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"));
    }
}
Also used : PubSubWebSocketOutputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator) Connector(org.eclipse.jetty.server.Connector) Configuration(org.apache.hadoop.conf.Configuration) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) URI(java.net.URI) PubSubWebSocketInputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator) LocalMode(com.datatorrent.api.LocalMode) Servlet(javax.servlet.Servlet) SamplePubSubWebSocketServlet(org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) HashMap(java.util.HashMap) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 3 with PubSubWebSocketInputOperator

use of org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator in project apex-malhar by apache.

the class MRMonitoringApplication method populateDAG.

@Override
public void populateDAG(DAG dag, Configuration conf) {
    MRJobStatusOperator mrJobOperator = dag.addOperator("JobMonitor", new MRJobStatusOperator());
    URI uri = PubSubHelper.getURI(dag);
    PubSubWebSocketInputOperator wsIn = dag.addOperator("Query", new PubSubWebSocketInputOperator());
    wsIn.setUri(uri);
    MapToMRObjectOperator queryConverter = dag.addOperator("QueryConverter", new MapToMRObjectOperator());
    /**
     * This is used to emit the meta data about the job
     */
    PubSubWebSocketOutputOperator<Object> wsOut = dag.addOperator("JobOutput", new PubSubWebSocketOutputOperator<Object>());
    wsOut.setUri(uri);
    /**
     * This is used to emit the information of map tasks of the job
     */
    PubSubWebSocketOutputOperator<Object> wsMapOut = dag.addOperator("MapJob", new PubSubWebSocketOutputOperator<Object>());
    wsMapOut.setUri(uri);
    /**
     * This is used to emit the information of reduce tasks of the job
     */
    PubSubWebSocketOutputOperator<Object> wsReduceOut = dag.addOperator("ReduceJob", new PubSubWebSocketOutputOperator<Object>());
    wsReduceOut.setUri(uri);
    /**
     * This is used to emit the metric information of the job
     */
    PubSubWebSocketOutputOperator<Object> wsCounterOut = dag.addOperator("JobCounter", new PubSubWebSocketOutputOperator<Object>());
    wsCounterOut.setUri(uri);
    dag.addStream("QueryConversion", wsIn.outputPort, queryConverter.input);
    dag.addStream("QueryProcessing", queryConverter.output, mrJobOperator.input);
    dag.addStream("JobData", mrJobOperator.output, wsOut.input);
    dag.addStream("MapData", mrJobOperator.mapOutput, wsMapOut.input);
    dag.addStream("ReduceData", mrJobOperator.reduceOutput, wsReduceOut.input);
    dag.addStream("CounterData", mrJobOperator.counterOutput, wsCounterOut.input);
}
Also used : PubSubWebSocketInputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator) URI(java.net.URI)

Example 4 with PubSubWebSocketInputOperator

use of org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator in project apex-malhar by apache.

the class Application method getPubSubWebSocketInputOperator.

public PubSubWebSocketInputOperator getPubSubWebSocketInputOperator(String name, DAG dag, URI duri, String topic) throws Exception {
    PubSubWebSocketInputOperator reqin = dag.addOperator(name, new PubSubWebSocketInputOperator());
    reqin.setUri(duri);
    reqin.setTopic(topic);
    return reqin;
}
Also used : PubSubWebSocketInputOperator(org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator)

Aggregations

PubSubWebSocketInputOperator (org.apache.apex.malhar.lib.io.PubSubWebSocketInputOperator)4 URI (java.net.URI)3 PubSubWebSocketOutputOperator (org.apache.apex.malhar.lib.io.PubSubWebSocketOutputOperator)2 LocalMode (com.datatorrent.api.LocalMode)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Servlet (javax.servlet.Servlet)1 HdfsStringOutputOperator (org.apache.apex.examples.frauddetect.operator.HdfsStringOutputOperator)1 MongoDBOutputOperator (org.apache.apex.examples.frauddetect.operator.MongoDBOutputOperator)1 SamplePubSubWebSocketServlet (org.apache.apex.malhar.lib.helper.SamplePubSubWebSocketServlet)1 RangeKeyVal (org.apache.apex.malhar.lib.math.RangeKeyVal)1 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)1 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)1 Configuration (org.apache.hadoop.conf.Configuration)1 Connector (org.eclipse.jetty.server.Connector)1 Server (org.eclipse.jetty.server.Server)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Test (org.junit.Test)1