Search in sources :

Example 51 with StreamlineEvent

use of com.hortonworks.streamline.streams.StreamlineEvent in project streamline by hortonworks.

the class EventCorrelationInjectorTest method eventArgsTestWithTwoParentsWhichOneIsRootEventAndAnotherOneIsNonRootEvent.

@Test
public void eventArgsTestWithTwoParentsWhichOneIsRootEventAndAnotherOneIsNonRootEvent() throws Exception {
    StreamlineEvent parentEvent1 = copyEventWithNewID();
    // this line is covered via `testNoParent()`
    parentEvent1 = sut.injectCorrelationInformation(parentEvent1, Collections.emptyList(), TEST_COMPONENT_NAME);
    StreamlineEvent parentOfParentEvent2 = copyEventWithNewID();
    // this line is covered via `testNoParent()`
    parentOfParentEvent2 = sut.injectCorrelationInformation(parentOfParentEvent2, Collections.emptyList(), TEST_COMPONENT_NAME);
    StreamlineEvent parentEvent2 = copyEventWithNewID();
    // this line is covered via `testWithAParentWhichIsRootEvent()`
    parentEvent2 = sut.injectCorrelationInformation(parentEvent2, Collections.singletonList(parentOfParentEvent2), TEST_COMPONENT_NAME);
    StreamlineEvent injectedEvent = sut.injectCorrelationInformation(INPUT_STREAMLINE_EVENT, Lists.newArrayList(parentEvent1, parentEvent2), TEST_COMPONENT_NAME);
    StreamlineEventTestUtil.assertEventIsProperlyCopied(injectedEvent, INPUT_STREAMLINE_EVENT);
    // added headers
    assertEquals(Sets.newHashSet(parentEvent1.getId(), parentOfParentEvent2.getId()), EventCorrelationInjector.getRootIds(injectedEvent));
    assertEquals(Sets.newHashSet(parentEvent1.getId(), parentEvent2.getId()), EventCorrelationInjector.getParentIds(injectedEvent));
    assertEquals(TEST_COMPONENT_NAME, EventCorrelationInjector.getSourceComponentName(injectedEvent));
}
Also used : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) Test(org.junit.Test)

Example 52 with StreamlineEvent

use of com.hortonworks.streamline.streams.StreamlineEvent in project streamline by hortonworks.

the class EventCorrelationInjectorTest method eventArgsTestNoParent.

@Test
public void eventArgsTestNoParent() throws Exception {
    StreamlineEvent injectedEvent = sut.injectCorrelationInformation(INPUT_STREAMLINE_EVENT, Collections.emptyList(), TEST_COMPONENT_NAME);
    StreamlineEventTestUtil.assertEventIsProperlyCopied(injectedEvent, INPUT_STREAMLINE_EVENT);
    // added headers
    // if rootIds is empty itself is the root event
    assertEquals(Collections.emptySet(), EventCorrelationInjector.getRootIds(injectedEvent));
    assertEquals(Collections.emptySet(), EventCorrelationInjector.getParentIds(injectedEvent));
    assertEquals(TEST_COMPONENT_NAME, EventCorrelationInjector.getSourceComponentName(injectedEvent));
}
Also used : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) Test(org.junit.Test)

Example 53 with StreamlineEvent

use of com.hortonworks.streamline.streams.StreamlineEvent in project streamline by hortonworks.

the class NotificationsTestBolt method process.

@Override
protected void process(Tuple input) {
    StreamlineEvent event = (StreamlineEvent) input.getValueByField(StreamlineEvent.STREAMLINE_EVENT);
    List<String> eventIds = Arrays.asList(event.getId());
    List<String> dataSourceIds = Arrays.asList(event.getDataSourceId());
    if (!consoleNotificationStream.isEmpty()) {
        // Create a dummy Notification object
        Map<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("temperature", "100");
        fieldsMap.put("humidity", "100");
        Map<String, Object> header = new HashMap<>();
        header.put(AddHeaderTransformRuntime.HEADER_FIELD_EVENT_IDS, eventIds);
        header.put(AddHeaderTransformRuntime.HEADER_FIELD_DATASOURCE_IDS, dataSourceIds);
        header.put(AddHeaderTransformRuntime.HEADER_FIELD_NOTIFIER_NAME, "console_notifier");
        header.put(AddHeaderTransformRuntime.HEADER_FIELD_RULE_ID, 1L);
        header.put(AddHeaderTransformRuntime.HEADER_FIELD_TIMESTAMP, System.currentTimeMillis());
        StreamlineEvent streamlineEvent = StreamlineEventImpl.builder().fieldsAndValues(fieldsMap).dataSourceId("notificationsTestBolt").header(header).build();
        collector.emit(consoleNotificationStream, new Values(streamlineEvent));
    }
    // Send an email every EMAIL_NOTIFICATION_INTERVAL
    if (++count % EMAIL_NOTIFICATION_INTERVAL == 0) {
        if (!emailNotificationStream.isEmpty()) {
            Map<String, Object> fieldsMap = new HashMap<>();
            fieldsMap.put("body", "Too many notifications, count so far is " + count);
            Map<String, Object> header = new HashMap<>();
            header.put(AddHeaderTransformRuntime.HEADER_FIELD_EVENT_IDS, eventIds);
            header.put(AddHeaderTransformRuntime.HEADER_FIELD_DATASOURCE_IDS, dataSourceIds);
            header.put(AddHeaderTransformRuntime.HEADER_FIELD_NOTIFIER_NAME, "email_notifier");
            header.put(AddHeaderTransformRuntime.HEADER_FIELD_RULE_ID, 1L);
            header.put(AddHeaderTransformRuntime.HEADER_FIELD_TIMESTAMP, System.currentTimeMillis());
            StreamlineEvent streamlineEvent = StreamlineEventImpl.builder().fieldsAndValues(fieldsMap).dataSourceId("notificationsTestBolt").header(header).build();
            collector.emit(emailNotificationStream, new Values(streamlineEvent));
        }
    }
    collector.ack(input);
}
Also used : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values)

Example 54 with StreamlineEvent

use of com.hortonworks.streamline.streams.StreamlineEvent in project streamline by hortonworks.

the class NotificationBoltTest method testAck.

@Test
public void testAck() throws Exception {
    Map<String, Object> fieldsAndValues = new HashMap<>();
    fieldsAndValues.put("temperature", "100");
    final StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValues).dataSourceId("srcid").build();
    final Notification notification = new StreamlineEventAdapter(event);
    new MockUp<NotificationQueueHandler>() {

        @Mock
        public void enqueue(Notifier notifier, Notification notification1) {
            notifier.notify(notification);
        }
    };
    new Expectations() {

        {
            mockProxyUtil.loadClassFromJar(anyString, "TestClass");
            result = notifier;
            tuple.getValueByField(anyString);
            result = event;
        }
    };
    Map<String, String> stormConf = new HashMap<>();
    stormConf.put("catalog.root.url", "http://localhost:8080/api/v1/catalog");
    stormConf.put("local.notifier.jar.path", "/tmp");
    bolt.prepare(stormConf, null, collector);
    bolt.execute(tuple);
    new Verifications() {

        {
            hBaseNotificationStore.store(notification);
            times = 1;
            hBaseNotificationStore.updateNotificationStatus(notification.getId(), Notification.Status.DELIVERED);
            times = 1;
            collector.ack(tuple);
            times = 1;
            collector.fail(tuple);
            times = 0;
        }
    };
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StreamlineEventAdapter(com.hortonworks.streamline.streams.runtime.notification.StreamlineEventAdapter) MockUp(mockit.MockUp) Verifications(mockit.Verifications) Notification(com.hortonworks.streamline.streams.notification.Notification) ConsoleNotifier(com.hortonworks.streamline.streams.notifiers.ConsoleNotifier) Notifier(com.hortonworks.streamline.streams.notification.Notifier) Test(org.junit.Test)

Example 55 with StreamlineEvent

use of com.hortonworks.streamline.streams.StreamlineEvent in project streamline by hortonworks.

the class SqlBasicExprScriptTest method testBasicAggregation.

@Test
public void testBasicAggregation() throws Exception {
    // SELECT STREAM DEPTID, EMPID, MIN(SALARY) FROM FOO where ID > 0 GROUP BY DEPTID, EMPID
    Expression min_salary = new AggregateFunctionExpression("MIN", ImmutableList.of(new FieldExpression(Schema.Field.of("salary", Schema.Type.INTEGER))));
    Expression deptid = new FieldExpression(Schema.Field.of("deptid", Schema.Type.INTEGER));
    Expression empid = new FieldExpression(Schema.Field.of("empid", Schema.Type.INTEGER));
    GroupBy groupBy = new GroupBy(ImmutableList.of(deptid, empid));
    Expression id = new FieldExpression(Schema.Field.of("id", Schema.Type.INTEGER));
    Expression id_gt_0 = new BinaryExpression(Operator.GREATER_THAN, id, new Literal("0"));
    Condition condition = new Condition();
    condition.setExpression(id_gt_0);
    Projection projection = new Projection();
    projection.setExpressions(ImmutableList.<Expression>of(deptid, empid, min_salary));
    sqlScript = new SqlScript(new StormSqlExpression(condition, projection, groupBy, null), new SqlEngine(), new SqlScript.CorrelatedValuesToStreamlineEventConverter(Lists.newArrayList("deptid", "empid", "MIN")));
    // (100, 100), 10
    Map<String, Object> kv1 = new HashMap<>();
    kv1.put("id", 1);
    kv1.put("deptid", 100);
    kv1.put("empid", 100);
    kv1.put("salary", 10);
    // (100, 100), 5
    Map<String, Object> kv2 = new HashMap<>();
    kv2.put("id", 2);
    kv2.put("deptid", 100);
    kv2.put("empid", 100);
    kv2.put("salary", 5);
    // (101, 101), 10
    Map<String, Object> kv3 = new HashMap<>();
    kv3.put("id", 3);
    kv3.put("deptid", 101);
    kv3.put("empid", 101);
    kv3.put("salary", 10);
    // (102, 102), 5
    Map<String, Object> kv4 = new HashMap<>();
    kv4.put("id", 4);
    kv4.put("deptid", 102);
    kv4.put("empid", 102);
    kv4.put("salary", 5);
    StreamlineEvent group1Event1 = StreamlineEventImpl.builder().fieldsAndValues(kv1).dataSourceId("1").build();
    StreamlineEvent group1Event2 = StreamlineEventImpl.builder().fieldsAndValues(kv2).dataSourceId("1").build();
    StreamlineEvent group2Event1 = StreamlineEventImpl.builder().fieldsAndValues(kv3).dataSourceId("1").build();
    StreamlineEvent group3Event1 = StreamlineEventImpl.builder().fieldsAndValues(kv4).dataSourceId("1").build();
    sqlScript.evaluate(group1Event1);
    sqlScript.evaluate(group1Event2);
    sqlScript.evaluate(group2Event1);
    sqlScript.evaluate(group3Event1);
    Collection<StreamlineEvent> result = sqlScript.evaluate(StreamlineEventImpl.GROUP_BY_TRIGGER_EVENT);
    Assert.assertEquals(3, result.size());
    Map<List<Integer>, Pair<Integer, Set<String>>> expectedGroupValueToMinAndParentIds;
    expectedGroupValueToMinAndParentIds = new HashMap<>();
    expectedGroupValueToMinAndParentIds.put(Lists.newArrayList(100, 100), new ImmutablePair<>(5, Sets.newHashSet(group1Event1.getId(), group1Event2.getId())));
    expectedGroupValueToMinAndParentIds.put(Lists.newArrayList(101, 101), new ImmutablePair<>(10, Sets.newHashSet(group2Event1.getId())));
    expectedGroupValueToMinAndParentIds.put(Lists.newArrayList(102, 102), new ImmutablePair<>(5, Sets.newHashSet(group3Event1.getId())));
    result.iterator().forEachRemaining(res -> {
        List<Integer> groupValue = Lists.newArrayList((Integer) res.get("deptid"), (Integer) res.get("empid"));
        Assert.assertTrue(expectedGroupValueToMinAndParentIds.containsKey(groupValue));
        Pair<Integer, Set<String>> minAndPairIds = expectedGroupValueToMinAndParentIds.get(groupValue);
        Integer minValue = (Integer) res.get("MIN");
        Assert.assertEquals(minValue, minAndPairIds.getLeft());
        Assert.assertTrue(EventCorrelationInjector.containsParentIds(res));
        Assert.assertEquals(minAndPairIds.getRight(), EventCorrelationInjector.getParentIds(res));
        // avoid matching multiple times
        expectedGroupValueToMinAndParentIds.remove(groupValue);
    });
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) Test(org.junit.Test)

Aggregations

StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)97 Test (org.junit.Test)47 HashMap (java.util.HashMap)41 Values (org.apache.storm.tuple.Values)25 ArrayList (java.util.ArrayList)19 Tuple (org.apache.storm.tuple.Tuple)14 Result (com.hortonworks.streamline.streams.Result)13 Expectations (mockit.Expectations)9 TupleImpl (org.apache.storm.tuple.TupleImpl)9 Map (java.util.Map)8 Verifications (mockit.Verifications)8 TransformRuntime (com.hortonworks.streamline.streams.runtime.TransformRuntime)7 ProcessingException (com.hortonworks.streamline.streams.exception.ProcessingException)6 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)6 List (java.util.List)5 IdPreservedStreamlineEvent (com.hortonworks.streamline.streams.common.IdPreservedStreamlineEvent)4 StreamlineEventImpl (com.hortonworks.streamline.streams.common.StreamlineEventImpl)4 Fields (org.apache.storm.tuple.Fields)4 EventCorrelationInjector (com.hortonworks.streamline.streams.common.event.correlation.EventCorrelationInjector)3 MergeTransform (com.hortonworks.streamline.streams.layout.component.rule.action.transform.MergeTransform)3