public static StormTopology buildTopology() {
   TridentTopology topology = new TridentTopology();
   DiagnosisEventSpout spout = new DiagnosisEventSpout();
   Stream inputStream = topology.newStream("event", spout);
   inputStream
       // Filter for critical events.
       .each(new Fields("event"), new DiseaseFilter())
       // Locate the closest city
       .each(new Fields("event"), new CityAssignment(), new Fields("city"))
       // Derive the hour segment
       .each(
           new Fields("event", "city"),
           new HourAssignment(),
           new Fields("hour", "cityDiseaseHour"))
       // Group occurrences in same city and hour
       .groupBy(new Fields("cityDiseaseHour"))
       // Count occurrences and persist the results.
       .persistentAggregate(new OutbreakTrendFactory(), new Count(), new Fields("count"))
       .newValuesStream()
       // Detect an outbreak
       .each(
           new Fields("cityDiseaseHour", "count"),
           new OutbreakDetector(),
           new Fields("alert")) // Dispatch the alert
       .each(new Fields("alert"), new DispatchAlert(), new Fields());
   return topology.build();
 }
  @Test
  public void testTridentTopology() throws Exception {

    Session session = cassandraCQLUnit.session;
    String[] stationIds = {"station-1", "station-2", "station-3"};
    for (int i = 1; i < 4; i++) {
      ResultSet resultSet =
          session.execute(
              "INSERT INTO weather.station(id, name) VALUES(?, ?)",
              stationIds[i - 1],
              "Foo-Station-" + new Random().nextInt());
    }

    ResultSet rows = cassandraCQLUnit.session.execute("SELECT * FROM weather.station");
    for (Row row : rows) {
      System.out.println("####### row = " + row);
    }

    WeatherBatchSpout weatherBatchSpout =
        new WeatherBatchSpout(
            new Fields("weather_station_id", "temperature", "event_time"), 3, stationIds);

    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("cassandra-trident-stream", weatherBatchSpout);

    CassandraStateFactory insertValuesStateFactory = getInsertTemperatureStateFactory();

    CassandraStateFactory selectWeatherStationStateFactory = getSelectWeatherStationStateFactory();

    TridentState selectState = topology.newStaticState(selectWeatherStationStateFactory);
    stream =
        stream.stateQuery(
            selectState,
            new Fields("weather_station_id"),
            new CassandraQuery(),
            new Fields("name"));
    stream = stream.each(new Fields("name"), new PrintFunction(), new Fields("name_x"));

    stream.partitionPersist(
        insertValuesStateFactory,
        new Fields("weather_station_id", "name", "event_time", "temperature"),
        new CassandraStateUpdater(),
        new Fields());

    StormTopology stormTopology = topology.build();
    LocalCluster cluster = new LocalCluster();
    cluster.submitTopology("wordCounter", getConfig(), stormTopology);
    Thread.sleep(30 * 1000);

    rows = cassandraCQLUnit.session.execute("SELECT * FROM weather.temperature");
    Assert.assertTrue(rows.iterator().hasNext()); // basic sanity check

    cluster.killTopology("wordCounter");
    cluster.shutdown();
  }
示例#3
0
文件: Stream.java 项目: wshgit/jstorm
 @Override
 public IAggregatableStream aggPartition(Stream s) {
   return s.global();
 }
 @Override
 protected Stream createTracesStream(Stream stream) {
   return stream.each(
       new Fields("str"), new JsonToTrace(sessionId), new Fields("versionId", "trace"));
 }