@Override public void execute(Tuple tuple) { if(tuple.contains(EVENT)) { synchronized (eventsReceived) { eventsReceived.add((Event) tuple.getValueByField(EVENT)); } } }
@Override public void execute(Tuple tuple) { JsonObject obj = new JsonObject(); obj.addProperty(Constants.FIELD_TIMESTAMP, tuple.getLongByField(Constants.FIELD_TIMESTAMP)); obj.addProperty(Constants.FIELD_AGGREGATION_WINDOW, tuple.getIntegerByField(Constants.FIELD_AGGREGATION_WINDOW)); obj.addProperty(Constants.FIELD_RULE_ACTION_ID, tuple.getStringByField(Constants.FIELD_RULE_ACTION_ID)); obj.addProperty(Constants.FIELD_AGGREGATION_KEY, tuple.getStringByField(Constants.FIELD_AGGREGATION_KEY)); if (tuple.contains(Constants.FIELD_STATE_TRACK)) { obj.addProperty(Constants.FIELD_STATE_TRACK, tuple.getBooleanByField(Constants.FIELD_STATE_TRACK)); } else if (tuple.contains(Constants.FIELD_AGGREGATION_VALUE)) { obj.addProperty(Constants.FIELD_AGGREGATION_VALUE, tuple.getValueByField(Constants.FIELD_AGGREGATION_VALUE).toString()); } else { // invalid event collector.fail(tuple); return; } collector.emit(tuple, new Values(tuple.getStringByField(Constants.FIELD_RULE_ACTION_ID) + "_" + tuple.getStringByField(Constants.FIELD_AGGREGATION_KEY), gson.toJson(obj))); collector.ack(tuple); }
@Override public void execute(Tuple input) { K key = null; if (input.contains(BOLT_KEY)) { key = (K) input.getValueByField(BOLT_KEY); } V message = (V) input.getValueByField(BOLT_MESSAGE); try { producer.send(new KeyedMessage<K, V>(topic, key, message)); } catch (Exception ex) { LOG.error("Could not send message with key '" + key + "' and value '" + message + "'", ex); } finally { collector.ack(input); } }
@Test public void testBasicSerialization() { AggregationSerializerBolt bolt = new AggregationSerializerBolt(); Map<String, String> conf = new HashMap<>(); bolt.prepare(conf, null, collector); when(tuple.getLongByField(Constants.FIELD_TIMESTAMP)).thenReturn(143322L); when(tuple.getStringByField(Constants.FIELD_RULE_ACTION_ID)).thenReturn("34_22"); when(tuple.getStringByField(Constants.FIELD_AGGREGATION_KEY)).thenReturn("host1"); when(tuple.getIntegerByField(Constants.FIELD_AGGREGATION_WINDOW)).thenReturn(100); when(tuple.getBooleanByField(Constants.FIELD_STATE_TRACK)).thenReturn(true); when(tuple.contains(Constants.FIELD_STATE_TRACK)).thenReturn(true); bolt.execute(tuple); verify(collector, times(1)).ack(tuple); }
public FlowInfo(Tuple tuple) { flowId = tuple.getStringByField(FLOW_ID); event = (Event) tuple.getValueByField(EVENT); idx = tuple.getIntegerByField(FLOW_OP_IDX); idx++; streamName = tuple.getStringByField(STREAM_NAME); previousStream = tuple.getStringByField(LAST_STREAM); if(tuple.contains(PARTITION)) partition = tuple.getStringByField(PARTITION); }
@Override public final void execute(Tuple tuple) { if (tuple.contains(Constants.FIELD_ALERT)) { Alert alert = (Alert) tuple.getValueByField(Constants.FIELD_ALERT); AlertTemplate template = templateMap.get(alert.getId());
@Test public void testJSONSerialization() { AggregationSerializerBolt bolt = new AggregationSerializerBolt(); Map<String, String> conf = new HashMap<>(); final AtomicReference<Values> outputTuple = new AtomicReference<Values>(null); OutputCollector mockCollector = MockTupleHelpers.mockCollector(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object newEvent = invocation.getArguments()[1]; outputTuple.set((Values) newEvent); return new ArrayList<>(); } }); bolt.prepare(conf, null, mockCollector); when(tuple.getLongByField(Constants.FIELD_TIMESTAMP)).thenReturn(143322L); when(tuple.getStringByField(Constants.FIELD_RULE_ACTION_ID)).thenReturn("34_22"); when(tuple.getStringByField(Constants.FIELD_AGGREGATION_KEY)).thenReturn("host1"); when(tuple.getIntegerByField(Constants.FIELD_AGGREGATION_WINDOW)).thenReturn(100); when(tuple.getBooleanByField(Constants.FIELD_STATE_TRACK)).thenReturn(true); when(tuple.contains(Constants.FIELD_STATE_TRACK)).thenReturn(true); bolt.execute(tuple); verify(mockCollector, times(1)).ack(tuple); assertEquals("34_22_host1", outputTuple.get().get(0)); JsonObject obj = new Gson().fromJson(outputTuple.get().get(1).toString(), JsonObject.class); assertEquals(143322L, obj.get(Constants.FIELD_TIMESTAMP).getAsLong()); assertEquals("34_22", obj.get(Constants.FIELD_RULE_ACTION_ID).getAsString()); assertEquals("host1", obj.get(Constants.FIELD_AGGREGATION_KEY).getAsString()); assertEquals(100, obj.get(Constants.FIELD_AGGREGATION_WINDOW).getAsInt()); assertEquals(true, obj.get(Constants.FIELD_STATE_TRACK).getAsBoolean()); }
int j = 1; for (i = 1; i < 5; i++) { when(input.contains(Constants.FIELD_ALERT)).thenReturn(true); bolt.execute(input); assertEquals(j % 3, bolt.getCounter().get(alert.getId()).getVal()); verify(collector, times(j++)).ack(input); if (i % 2 == 0) { when(input.contains(Constants.FIELD_ALERT)).thenReturn(false); when(input.getSourceComponent()).thenReturn(backtype.storm.Constants.SYSTEM_COMPONENT_ID); when(input.getSourceStreamId()).thenReturn(backtype.storm.Constants.SYSTEM_TICK_STREAM_ID);
alert.setMedia("mail"); alert.setTimestamp(3253454235L); when(input.contains(Constants.FIELD_ALERT)).thenReturn(true); when(input.getValueByField(Constants.FIELD_ALERT)).thenReturn(alert); bolt.execute(input);
alert.setBody("test"); alert.setTimestamp(3253454235L); when(input.contains(Constants.FIELD_ALERT)).thenReturn(true); when(input.getValueByField(Constants.FIELD_ALERT)).thenReturn(alert); int i = 0; assertEquals(i + 1, bolt.getCounter().get(alert.getId()).getVal()); verify(collector, times(i + 1)).ack(input); when(input.contains(Constants.FIELD_ALERT)).thenReturn(false); when(input.getSourceComponent()).thenReturn(backtype.storm.Constants.SYSTEM_COMPONENT_ID); when(input.getSourceStreamId()).thenReturn(backtype.storm.Constants.SYSTEM_TICK_STREAM_ID);
when(tuple.getStringByField(Constants.FIELD_AGGREGATION_KEY)).thenReturn((String) values.get(4)); when(tuple.contains(Constants.FIELD_STATE_TRACK)).thenReturn(true);