public void execute(TridentTuple tuple, TridentCollector collector) { Map<String, Object> event = new HashMap<String, Object>(); if (tuple.get(0) instanceof byte[]) { byte[] messageBytes = (byte[]) tuple.get(0); String messageString = new String(messageBytes); System.out.println("flight delay tweet: " + messageString); try { JSONObject json = new JSONObject(messageString); System.out.println("Created json object"); Iterator keyIt = json.keys(); while (keyIt.hasNext()) { String key = (String) keyIt.next(); if (!tweetKeyWhiteList.contains(key)) { continue; } Object value = json.get(key); event.put(key, value); } } catch (Exception e) { System.out.println("Error parsing json from tweet object"); log.error(e); } try { String id = event.get("user_id").toString(); double lat = Double.parseDouble(event.get("lat").toString()); double lon = Double.parseDouble(event.get("lon").toString()); if (lat > 0 || lat < 0) { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(id.getBytes()); byte byteData[] = md.digest(); // convert the byte to hex format method 1 StringBuilder sb = new StringBuilder(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } id = sb.toString(); GroupByField idField = new GroupByField("TWEET_USER_ID", id); GroupByField objectType = new GroupByField("OBJECT_TYPE", "TWITTER_USER"); event.put("TWEET_USER_ID", id); event.put("user_id", id); collector.emit(new Values(idField, objectType, event)); } } catch (Exception e) { log.error("Error parsing tweet info", e); } } else { System.out.println("object at tuple index 0: " + tuple.get(0).getClass().getName()); } }
/* * Takes the first tuple of the batch and checks the timestamp t0. * It stores tuples into a state while the timestamp is within [t0 AND t0+timeWindowInSeconds] * When the timestamp tn of a tuple does not match this condition, THEN t0 = tn, and the previous state is overwritten with new tuples. * (non-Javadoc) * @see storm.trident.state.StateUpdater#updateState(storm.trident.state.State, java.util.List, storm.trident.operation.TridentCollector) */ @Override public void updateState( MemoryMapState<Object> state, List<TridentTuple> tuples, TridentCollector collector) { List<Object> ids = new ArrayList<Object>(); List<Object> earthquakeGraphs = new ArrayList<Object>(); for (TridentTuple tuple : tuples) { ids.add((String) tuple.get(0) + (String) tuple.get(1)); earthquakeGraphs.add(tuple.get(2)); } state.multiPut(Arrays.asList(ids), earthquakeGraphs); // Tuples emitted to the collector go through the newValuesStream call // collector.emit(state.multiGet(Arrays.asList(sensorIds))); }
private TridentTuple createMockedInstanceTuple(double[] features) { Instance<Boolean> instance = new Instance<Boolean>(features); TridentTuple tuple = mock(TridentTuple.class); given(tuple.get(0)).willReturn(instance); return tuple; }
@Override public boolean isKeep(TridentTuple tuple) { GameState gameState = (GameState) tuple.get(0); boolean keep = (gameState.getBoard().isEndState()); if (keep) { LOG.info("END GAME [" + gameState + "]"); } return keep; }
@Override public void execute(TridentTuple tuple, TridentCollector collector) { GameState gameState = (GameState) tuple.get(0); Board currentBoard = gameState.getBoard(); List<Board> history = new ArrayList<Board>(); history.addAll(gameState.getHistory()); history.add(currentBoard); if (!currentBoard.isEndState()) { String nextPlayer = Player.next(gameState.getPlayer()); List<Board> boards = gameState.getBoard().nextBoards(nextPlayer); Log.debug( "Generated [" + boards.size() + "] children boards for [" + gameState.toString() + "]"); for (Board b : boards) { GameState newGameState = new GameState(b, history, nextPlayer); List<Object> values = new ArrayList<Object>(); values.add(newGameState); collector.emit(values); } } else { Log.debug("End game found! [" + currentBoard + "]"); } }