package tutorial.storm.trident.example;

import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.LocalDRPC;
import backtype.storm.generated.StormTopology;
import backtype.storm.spout.SchemeAsMultiScheme;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import storm.kafka.KafkaConfig;
import storm.kafka.StringScheme;
import storm.kafka.trident.TransactionalTridentKafkaSpout;
import storm.kafka.trident.TridentKafkaConfig;
import storm.trident.TridentState;
import storm.trident.TridentTopology;
import storm.trident.operation.builtin.Count;
import storm.trident.operation.builtin.FirstN;
import storm.trident.operation.builtin.MapGet;
import storm.trident.operation.builtin.TupleCollectionGet;
import storm.trident.testing.FeederBatchSpout;
import storm.trident.testing.MemoryMapState;
import tutorial.storm.trident.operations.*;
import tutorial.storm.trident.testutil.SampleTweet;

import java.io.IOException;

/**
 * @author Enno Shioji (enno.shioji@peerindex.com)
 */
public class Skeleton {

    public static StormTopology buildTopology(LocalDRPC drpc, FeederBatchSpout spout) throws IOException {

        TridentTopology topology = new TridentTopology();
        TridentState count =
        topology
                .newStream("tweets", spout)
                .each(new Fields("str"), new ParseTweet(), new Fields("text", "content", "user"))
                .project(new Fields("content", "user"))
                .each(new Fields("content"), new OnlyHashtags())
                .each(new Fields("user"), new OnlyEnglish())
                .each(new Fields("content", "user"), new ExtractFollowerClassAndContentName(), new Fields("followerClass", "contentName"))
                .groupBy(new Fields("followerClass", "contentName"))
                .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count"))
        ;


        topology
                .newDRPCStream("hashtag_count", drpc)
                .stateQuery(count, new TupleCollectionGet(), new Fields("followerClass", "contentName"))
                .stateQuery(count, new Fields("followerClass", "contentName"), new MapGet(), new Fields("count"))
                .groupBy(new Fields("followerClass"))
                .aggregate(new Fields("contentName", "count"), new FirstN.FirstNSortedAgg(1,"count", true), new Fields("contentName", "count"))
        ;

        return topology.build();
    }

    public static void main(String[] args) throws Exception {
        Preconditions.checkArgument(args.length == 1, "Please specify the test kafka broker host:port");
        String testKafkaBrokerHost = args[0];

//        TransactionalTridentKafkaSpout tweetSpout = tweetSpout(testKafkaBrokerHost);

        Config conf = new Config();

        LocalDRPC drpc = new LocalDRPC();
        LocalCluster cluster = new LocalCluster();
        FeederBatchSpout feederSpout = new FeederBatchSpout(ImmutableList.of("str"));

        SampleTweet sampleTweet = new SampleTweet();

        cluster.submitTopology("hackaton", conf, buildTopology(drpc,feederSpout));

//        spout.feed(new Values(ImmutableList.of("rose")));
//        spout.feed(new Values(ImmutableList.of("rose")));
//        spout.feed(new Values(ImmutableList.of("rose")));
//
//        spout.feed(new Values(ImmutableList.of("fred")));
//        spout.feed(new Values(ImmutableList.of("fred")));
//        spout.feed(new Values(ImmutableList.of("fred")));
//        spout.feed(new Values(ImmutableList.of("fred")));
//
//        spout.feed(new Values(ImmutableList.of("steve")));
//        spout.feed(new Values(ImmutableList.of("steve")));
//
//
        while(!Thread.currentThread().isInterrupted()){
            Thread.sleep(500);
            System.out.println(drpc.execute("hashtag_count",""));
            feederSpout.feed(ImmutableList.of(new Values(sampleTweet.sampleTweet())));
        }
    }

    private static TransactionalTridentKafkaSpout tweetSpout(String testKafkaBrokerHost) {
//        TweetIngestor ingestor = new TweetIngestor("/tmp/kafka", "test", 12000);
//        ingestor.startAndWait();
        KafkaConfig.BrokerHosts hosts = TridentKafkaConfig.StaticHosts.fromHostString(ImmutableList.of(testKafkaBrokerHost), 1);
        TridentKafkaConfig config = new TridentKafkaConfig(hosts, "test");
        config.scheme = new SchemeAsMultiScheme(new StringScheme());
        return new TransactionalTridentKafkaSpout(config);
    }

}

@@ -1024,16 +1024,70 @@
 leTweet;
+%0Aimport tutorial.storm.trident.testutil.TweetIngestor;
 %0A%0Aimport
@@ -1255,27 +1255,41 @@
 C drpc, 
-FeederBatch
+TransactionalTridentKafka
 Spout sp
@@ -2817,18 +2817,16 @@
 gs%5B0%5D;%0A%0A
-//
         
@@ -3040,16 +3040,47 @@
 %0A       
+ // For unit testing%0A        //
  FeederB
@@ -3150,24 +3150,26 @@
 str%22));%0A
-%0A
+ 
        
+//
  SampleT
@@ -3206,25 +3206,24 @@
 weet();%0A
-%0A
         
 cluster.
@@ -3218,637 +3218,169 @@
     
-cluster.submitTopology(%22hackaton%22, conf, buildTopology(drpc,feederSpout));%0A%0A//        spout.feed(new Values(ImmutableList.of(%22rose%22)));%0A//        spout.feed(new Values(ImmutableList.of(%22rose%22)));%0A//        spout.feed(new Values(ImmutableList.of(%22rose%22)));%0A//%0A//        spout.feed(new Values(ImmutableList.of(%22fred%22)));%0A//        spout.feed(new Values(ImmutableList.of(%22fred%22)));%0A//        spout.feed(new Values(ImmutableList.of(%22fred%22)));%0A//        spout.feed(new Values(ImmutableList.of(%22fred%22)));%0A//%0A//        spout.feed(new Values(ImmutableList.of(%22steve%22)));%0A//        spout.feed(new Values(ImmutableList.of(%22steve%22)));%0A//%0A//
+// feederSpout.feed(ImmutableList.of(new Values(sampleTweet.sampleTweet())));%0A%0A        cluster.submitTopology(%22hackaton%22, conf, buildTopology(drpc,tweetSpout));%0A
 %0A   
@@ -3533,95 +3533,8 @@
 ));%0A
-            feederSpout.feed(ImmutableList.of(new Values(sampleTweet.sampleTweet())));%0A
     
@@ -3637,25 +3637,75 @@
 Host) %7B%0A
-//
        
+ // For locally starting the kafka broker%0A        //
  TweetIn
@@ -3774,17 +3774,18 @@
 0);%0A
-//
+ 
        
+//
  ing

Clean up