storm.trident.operation.builtin.Count Java Examples
The following examples show how to use
storm.trident.operation.builtin.Count.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TridentWordCount.java From flink-perf with Apache License 2.0 | 7 votes |
public static StormTopology buildTopology(LocalDRPC drpc) { FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person")); spout.setCycle(true); TridentTopology topology = new TridentTopology(); TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16); topology.newDRPCStream("words", drpc).each(new Fields("args"), new Split(), new Fields("word")).groupBy(new Fields( "word")).stateQuery(wordCounts, new Fields("word"), new MapGet(), new Fields("count")).each(new Fields("count"), new FilterNull()).aggregate(new Fields("count"), new Sum(), new Fields("sum")); return topology.build(); }
Example #2
Source File: ClickThruAnalyticsTopology.java From storm-example with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology() { LOG.info("Building topology."); TridentTopology topology = new TridentTopology(); StateFactory clickThruMemory = new MemoryMapState.Factory(); ClickThruSpout spout = new ClickThruSpout(); Stream inputStream = topology.newStream("clithru", spout); TridentState clickThruState = inputStream.each(new Fields("username", "campaign", "product", "click"), new Filter("click", "true")) .each(new Fields("username", "campaign", "product", "click"), new Distinct()) .groupBy(new Fields("campaign")) .persistentAggregate(clickThruMemory, new Count(), new Fields("click_thru_count")); inputStream.groupBy(new Fields("campaign")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("impression_count")) .newValuesStream() .stateQuery(clickThruState, new Fields("campaign"), new MapGet(), new Fields("click_thru_count")) .each(new Fields("campaign", "impression_count", "click_thru_count"), new CampaignEffectiveness(), new Fields("")); return topology.build(); }
Example #3
Source File: TridentTopologySource.java From jstorm with Apache License 2.0 | 6 votes |
public StormTopology getTopology(Config config) { this.spout = new FixedBatchSpout(new Fields("sentence"), 20, new Values("one two"), new Values("two three"), new Values("three four"), new Values("four five"), new Values("five six") ); TridentTopology trident = new TridentTopology(); trident.newStream("wordcount", spout).name("sentence").parallelismHint(1).shuffle() .each(new Fields("sentence"), new Split(), new Fields("word")) .parallelismHint(1) .groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .parallelismHint(1); return trident.build(); }
Example #4
Source File: TridentMapExample.java From jstorm with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology(LocalDRPC drpc) { FixedBatchSpout spout = new FixedBatchSpout(new Fields("word"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person")); spout.setCycle(true); TridentTopology topology = new TridentTopology(); TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(16).flatMap(split).map(toUpper) .filter(theFilter).peek(new Consumer() { @Override public void accept(TridentTuple input) { System.out.println(input.getString(0)); } }).groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .parallelismHint(16); topology.newDRPCStream("words", drpc).flatMap(split).groupBy(new Fields("args")) .stateQuery(wordCounts, new Fields("args"), new MapGet(), new Fields("count")).filter(new FilterNull()) .aggregate(new Fields("count"), new Sum(), new Fields("sum")); return topology.build(); }
Example #5
Source File: TridentWordCount.java From jstorm with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology(LocalDRPC drpc) { FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person")); spout.setCycle(true); TridentTopology topology = new TridentTopology(); TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(16) .each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .parallelismHint(16); topology.newDRPCStream("words", drpc).each(new Fields("args"), new Split(), new Fields("word")) .groupBy(new Fields("word")) .stateQuery(wordCounts, new Fields("word"), new MapGet(), new Fields("count")) .each(new Fields("count"), new FilterNull()) .aggregate(new Fields("count"), new Sum(), new Fields("sum")); return topology.build(); }
Example #6
Source File: ClusterTestTopology.java From trident-tutorial with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { Config conf = new Config(); // Submits the topology String topologyName = args[0]; conf.setNumWorkers(8); // Our Vagrant environment has 8 workers FakeTweetsBatchSpout fakeTweets = new FakeTweetsBatchSpout(10); TridentTopology topology = new TridentTopology(); TridentState countState = topology .newStream("spout", fakeTweets) .groupBy(new Fields("actor")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")); topology .newDRPCStream("count_per_actor") .stateQuery(countState, new Fields("args"), new MapGet(), new Fields("count")); StormSubmitter.submitTopology(topologyName, conf, topology.build()); }
Example #7
Source File: TopHashtagFollowerCountGrouping.java From trident-tutorial with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology(TransactionalTridentKafkaSpout 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")) .parallelismHint(3) .groupBy(new Fields("followerClass", "contentName")) .persistentAggregate(new HazelCastStateFactory(), new Count(), new Fields("count")) .parallelismHint(3) ; topology .newDRPCStream("hashtag_count") .each(new Constants<String>("< 100", "< 10K", "< 100K", ">= 100K"), new Fields("followerClass")) .stateQuery(count, new Fields("followerClass", "args"), new MapGet(), new Fields("count")) ; return topology.build(); }
Example #8
Source File: TopHashtagByCountry.java From trident-tutorial with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology(TransactionalTridentKafkaSpout spout) throws IOException { TridentTopology topology = new TridentTopology(); TridentState count = topology .newStream("tweets", spout) .each(new Fields("str"), new ParseTweet(), new Fields("status", "content", "user")) .project(new Fields("content", "user", "status")) .each(new Fields("content"), new OnlyHashtags()) .each(new Fields("status"), new OnlyGeo()) .each(new Fields("status", "content"), new ExtractLocation(), new Fields("country", "contentName")) .groupBy(new Fields("country", "contentName")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) ; topology .newDRPCStream("location_hashtag_count") .stateQuery(count, new TupleCollectionGet(), new Fields("country", "contentName")) .stateQuery(count, new Fields("country", "contentName"), new MapGet(), new Fields("count")) .groupBy(new Fields("country")) .aggregate(new Fields("contentName", "count"), new FirstN.FirstNSortedAgg(3,"count", true), new Fields("contentName", "count")) ; return topology.build(); }
Example #9
Source File: GlobalTop20Hashtags.java From trident-tutorial with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology(TransactionalTridentKafkaSpout 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("top_hashtags") .stateQuery(count, new TupleCollectionGet(), new Fields("followerClass", "contentName")) .stateQuery(count, new Fields("followerClass", "contentName"), new MapGet(), new Fields("count")) .aggregate(new Fields("contentName", "count"), new FirstN.FirstNSortedAgg(5,"count", true), new Fields("contentName", "count")) ; return topology.build(); }
Example #10
Source File: TopHashtagByFollowerClass.java From trident-tutorial with Apache License 2.0 | 6 votes |
public static StormTopology buildTopology(TransactionalTridentKafkaSpout 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") .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(); }
Example #11
Source File: TridentWordCount.java From storm-benchmark with Apache License 2.0 | 6 votes |
@Override public StormTopology getTopology(Config config) { final int spoutNum = BenchmarkUtils.getInt(config, SPOUT_NUM, DEFAULT_SPOUT_NUM); final int splitNum = BenchmarkUtils.getInt(config, SPLIT_NUM, DEFAULT_SPLIT_BOLT_NUM); final int countNum = BenchmarkUtils.getInt(config, COUNT_NUM, DEFAULT_COUNT_BOLT_NUM); spout = new TransactionalTridentKafkaSpout( KafkaUtils.getTridentKafkaConfig(config, new SchemeAsMultiScheme(new StringScheme()))); TridentTopology trident = new TridentTopology(); trident.newStream("wordcount", spout).name("sentence").parallelismHint(spoutNum).shuffle() .each(new Fields(StringScheme.STRING_SCHEME_KEY), new WordSplit(), new Fields("word")) .parallelismHint(splitNum) .groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .parallelismHint(countNum); /* trident.newStream("wordcount", spout) .each(new Fields(StringScheme.STRING_SCHEME_KEY), new WordSplit(), new Fields("word")) .groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count"));*/ return trident.build(); }
Example #12
Source File: TridentTopologySource.java From flux with Apache License 2.0 | 6 votes |
public StormTopology getTopology(Config config) { this.spout = new FixedBatchSpout(new Fields("sentence"), 20, new Values("one two"), new Values("two three"), new Values("three four"), new Values("four five"), new Values("five six") ); TridentTopology trident = new TridentTopology(); trident.newStream("wordcount", spout).name("sentence").parallelismHint(1).shuffle() .each(new Fields("sentence"), new Split(), new Fields("word")) .parallelismHint(1) .groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .parallelismHint(1); return trident.build(); }
Example #13
Source File: Part03_AdvancedPrimitives2.java From trident-tutorial with Apache License 2.0 | 5 votes |
private static StormTopology advancedPrimitives(FeederBatchSpout spout) throws IOException { TridentTopology topology = new TridentTopology(); // What if we want more than one aggregation? For that, we can use "chained" aggregations. // Note how we calculate count and sum. // The aggregated values can then be processed further, in this case into mean topology .newStream("aggregation", spout) .groupBy(new Fields("city")) .chainedAgg() .aggregate(new Count(), new Fields("count")) .aggregate(new Fields("age"), new Sum(), new Fields("age_sum")) .chainEnd() .each(new Fields("age_sum", "count"), new DivideAsDouble(), new Fields("mean_age")) .each(new Fields("city", "mean_age"), new Print()) ; // What if we want to persist results of an aggregation, but want to further process these // results? You can use "newValuesStream" for that topology .newStream("further",spout) .groupBy(new Fields("city")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .newValuesStream() .each(new Fields("city", "count"), new Print()); return topology.build(); }
Example #14
Source File: OutbreakDetectionTopology.java From storm-example with Apache License 2.0 | 5 votes |
public static StormTopology buildTopology() { TridentTopology topology = new TridentTopology(); DiagnosisEventSpout spout = new DiagnosisEventSpout(); Stream inputStream = topology.newStream("event", spout); inputStream.each(new Fields("event"), new DiseaseFilter()) .each(new Fields("event"), new CityAssignment(), new Fields("city")) .each(new Fields("event", "city"), new HourAssignment(), new Fields("hour", "cityDiseaseHour")) .groupBy(new Fields("cityDiseaseHour")) .persistentAggregate(new OutbreakTrendFactory(), new Count(), new Fields("count")).newValuesStream() .each(new Fields("cityDiseaseHour", "count"), new OutbreakDetector(), new Fields("alert")) .each(new Fields("alert"), new DispatchAlert(), new Fields()); return topology.build(); }
Example #15
Source File: Part04_BasicStateAndDRPC.java From trident-tutorial with Apache License 2.0 | 4 votes |
private static StormTopology basicStateAndDRPC(LocalDRPC drpc, FeederBatchSpout spout) throws IOException { TridentTopology topology = new TridentTopology(); // persistentAggregate persists the result of aggregation into data stores, // which you can use from other applications. // You can also use it in other topologies by using the TridentState object returned. // // The state is commonly backed by a data store like memcache, cassandra etc. // Here we are simply using a hash map TridentState countState = topology .newStream("spout", spout) .groupBy(new Fields("actor")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")); // There are a few ready-made state libraries that you can use // Below is an example to use memcached // List<InetSocketAddress> memcachedServerLocations = ImmutableList.of(new InetSocketAddress("some.memcached.server",12000)); // TridentState countStateMemcached = // topology // .newStream("spout", spout) // .groupBy(new Fields("actor")) // .persistentAggregate(MemcachedState.transactional(memcachedServerLocations), new Count(), new Fields("count")); // DRPC stands for Distributed Remote Procedure Call // You can issue calls using the DRPC client library // A DRPC call takes two Strings, function name and function arguments // // In order to call the DRPC defined below, you'd use "count_per_actor" as the function name // The function arguments will be available as "args" /* topology .newDRPCStream("ping", drpc) .each(new Fields("args"), new Split(" "), new Fields("reply")) .each(new Fields("reply"), new RegexFilter("ping")) .project(new Fields("reply")); // You can apply usual processing primitives to DRPC streams as well topology .newDRPCStream("count", drpc) .each(new Fields("args"), new Split(" "), new Fields("split")) .each(new Fields("split"), new RegexFilter("a.*")) .groupBy(new Fields("split")) .aggregate(new Count(), new Fields("count")); */ // More usefully, you can query the state you created earlier topology .newDRPCStream("count_per_actor", drpc) .stateQuery(countState, new Fields("args"), new MapGet(), new Fields("count")); // Here is a more complex example topology .newDRPCStream("count_per_actors", drpc) .each(new Fields("args"), new Split(" "), new Fields("actor")) .groupBy(new Fields("actor")) .stateQuery(countState, new Fields("actor"), new MapGet(), new Fields("individual_count")) .each(new Fields("individual_count"), new FilterNull()) .aggregate(new Fields("individual_count"), new Sum(), new Fields("count")); // For how to call DRPC calls, go back to the main method return topology.build(); }
Example #16
Source File: Part01_BasicPrimitives.java From trident-tutorial with Apache License 2.0 | 4 votes |
public static StormTopology basicPrimitives(IBatchSpout spout) throws IOException { // A topology is a set of streams. // A stream is a DAG of Spouts and Bolts. // (In Storm there are Spouts (data producers) and Bolts (data processors). // Spouts create Tuples and Bolts manipulate then and possibly emit new ones.) // But in Trident we operate at a higher level. // Bolts are created and connected automatically out of higher-level constructs. // Also, Spouts are "batched". TridentTopology topology = new TridentTopology(); // The "each" primitive allows us to apply either filters or functions to the stream // We always have to select the input fields. topology .newStream("filter", spout) .each(new Fields("actor"), new RegexFilter("pere")) .each(new Fields("text", "actor"), new Print()); // Functions describe their output fields, which are always appended to the input fields. // As you see, Each operations can be chained. topology .newStream("function", spout) .each(new Fields("text"), new ToUpperCase(), new Fields("uppercased_text")) .each(new Fields("text", "uppercased_text"), new Print()); // You can prune unnecessary fields using "project" topology .newStream("projection", spout) .each(new Fields("text"), new ToUpperCase(), new Fields("uppercased_text")) .project(new Fields("uppercased_text")) .each(new Fields("uppercased_text"), new Print()); // Stream can be parallelized with "parallelismHint" // Parallelism hint is applied downwards until a partitioning operation (we will see this later). // This topology creates 5 spouts and 5 bolts: // Let's debug that with TridentOperationContext.partitionIndex ! topology .newStream("parallel", spout) .each(new Fields("actor"), new RegexFilter("pere")) .parallelismHint(5) .each(new Fields("text", "actor"), new Print()); // You can perform aggregations by grouping the stream and then applying an aggregation // Note how each actor appears more than once. We are aggregating inside small batches (aka micro batches) // This is useful for pre-processing before storing the result to databases topology .newStream("aggregation", spout) .groupBy(new Fields("actor")) .aggregate(new Count(),new Fields("count")) .each(new Fields("actor", "count"),new Print()) ; // In order ot aggregate across batches, we need persistentAggregate. // This example is incrementing a count in the DB, using the result of these micro batch aggregations // (here we are simply using a hash map for the "database") topology .newStream("aggregation", spout) .groupBy(new Fields("actor")) .persistentAggregate(new MemoryMapState.Factory(),new Count(),new Fields("count")) ; return topology.build(); }
Example #17
Source File: Part05_AdvancedStateAndDRPC.java From trident-tutorial with Apache License 2.0 | 4 votes |
private static StormTopology externalState(LocalDRPC drpc, FeederBatchSpout spout) { TridentTopology topology = new TridentTopology(); // You can reference existing data sources as well. // Here we are mocking up a "database" StateFactory stateFactory = new StateFactory() { @Override public State makeState(Map conf, IMetricsContext metrics, int partitionIndex, int numPartitions) { MemoryMapState<Integer> name_to_age = new MemoryMapState<Integer>("name_to_age"); // This is a bit hard to read but it's just pre-populating the state List<List<Object>> keys = getKeys("ted", "mary", "jason", "tom", "chuck"); name_to_age.multiPut(keys, ImmutableList.of(32, 21, 45, 52, 18)); return name_to_age; } }; TridentState nameToAge = topology.newStaticState(stateFactory); // Let's setup another state that keeps track of actor's appearance counts per location TridentState countState = topology .newStream("spout", spout) .groupBy(new Fields("actor","location")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")); // Now, let's calculate the average age of actors seen topology .newDRPCStream("age_stats", drpc) .stateQuery(countState, new TupleCollectionGet(), new Fields("actor", "location")) .stateQuery(nameToAge, new Fields("actor"), new MapGet(), new Fields("age")) .each(new Fields("actor","location","age"), new Print()) .groupBy(new Fields("location")) .chainedAgg() .aggregate(new Count(), new Fields("count")) .aggregate(new Fields("age"), new Sum(), new Fields("sum")) .chainEnd() .each(new Fields("sum", "count"), new DivideAsDouble(), new Fields("avg")) .project(new Fields("location", "count", "avg")) ; return topology.build(); }
Example #18
Source File: TridentFastWordCount.java From jstorm with Apache License 2.0 | 4 votes |
public static StormTopology buildTopology(LocalDRPC drpc) { FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"), new Values("marry had a little lamb whos fleese was white as snow"), new Values("and every where that marry went the lamb was sure to go"), new Values("one two three four five six seven eight nine ten"), new Values("this is a test of the emergency broadcast system this is only a test"), new Values("peter piper picked a peck of pickeled peppers"), new Values("JStorm is a distributed and fault-tolerant realtime computation system."), new Values( "Inspired by Apache Storm, JStorm has been completely rewritten in Java and provides many more enhanced features."), new Values("JStorm has been widely used in many enterprise environments and proved robust and stable."), new Values("JStorm provides a distributed programming framework very similar to Hadoop MapReduce."), new Values( "The developer only needs to compose his/her own pipe-lined computation logic by implementing the JStorm API"), new Values(" which is fully compatible with Apache Storm API"), new Values("and submit the composed Topology to a working JStorm instance."), new Values("Similar to Hadoop MapReduce, JStorm computes on a DAG (directed acyclic graph)."), new Values("Different from Hadoop MapReduce, a JStorm topology runs 24 * 7"), new Values("the very nature of its continuity abd 100% in-memory architecture "), new Values( "has been proved a particularly suitable solution for streaming data and real-time computation."), new Values("JStorm guarantees fault-tolerance."), new Values("Whenever a worker process crashes, "), new Values( "the scheduler embedded in the JStorm instance immediately spawns a new worker process to take the place of the failed one."), new Values(" The Acking framework provided by JStorm guarantees that every single piece of data will be processed at least once.") ); spout.setCycle(true); int spout_Parallelism_hint = JStormUtils.parseInt(conf.get(TOPOLOGY_SPOUT_PARALLELISM_HINT), 1); int split_Parallelism_hint = JStormUtils.parseInt(conf.get(TOPOLOGY_SPLIT_PARALLELISM_HINT), 2); int count_Parallelism_hint = JStormUtils.parseInt(conf.get(TOPOLOGY_COUNT_PARALLELISM_HINT), 2); TridentTopology topology = new TridentTopology(); TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(spout_Parallelism_hint) .each(new Fields("sentence"), new Split(), new Fields("word")).parallelismHint(split_Parallelism_hint).groupBy(new Fields("word")) .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")) .parallelismHint(count_Parallelism_hint); return topology.build(); }