use of storm.trident.operation.builtin.FilterNull in project jstorm by alibaba.
the class TridentWordCountTest method testTridentWordCount.
// to make sure the validator is right
@Test
public void testTridentWordCount() {
LocalDRPC localDRPC = new LocalDRPC();
FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("sentence"), SPOUT_BATCH_SIZE, 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"));
TridentTopology tridentTopology = new TridentTopology();
TridentState wordCount = tridentTopology.newStream("spout", spout).parallelismHint(1).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
tridentTopology.newDRPCStream("words", localDRPC).each(new Fields("args"), new Split(), new Fields("keyword")).groupBy(new Fields("keyword")).stateQuery(wordCount, new Fields("keyword"), new MapGet(), new Fields("result")).each(new Fields("result"), new FilterNull()).aggregate(new Fields("result"), new Sum(), new Fields("sum"));
Map config = new HashMap();
config.put(Config.TOPOLOGY_NAME, "TridentWordCountTest");
JStormUnitTestValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {
Logger LOG = LoggerFactory.getLogger(JStormUnitTestValidator.class);
@Override
public boolean validate(Map config) {
String queryResult = executeLocalDRPC("words", "the");
//the result is like [[8080]], so remove the [[]]
queryResult = queryResult.substring(2, queryResult.length() - 2);
//how many times of emit can finish a loop
int oneLoopNeedEmits = (int) Math.ceil(SPOUT_CONTENT_TYPES / (float) SPOUT_BATCH_SIZE);
// of all the spout content
//the loop time of the LimitFixBatchSpout content
int loopTime = SPOUT_LIMIT / oneLoopNeedEmits;
int receiveCountOfThe = Integer.valueOf(queryResult);
LOG.info("Final receive total " + receiveCountOfThe + " \"the\" when expected " + (loopTime * 5));
//5 "the" are in one loop
boolean isCountOfTheRight = (receiveCountOfThe == loopTime * 5);
// query the word count of these 3 words total
queryResult = executeLocalDRPC("words", "be store kujou");
queryResult = queryResult.substring(2, queryResult.length() - 2);
int receiveCountOfBeAndStore = Integer.valueOf(queryResult);
LOG.info("Final receive total " + receiveCountOfBeAndStore + " \"be\" and \"store\" and \"kujou\" " + "when expected " + (loopTime * 3));
//2 "be" 1 "store" 0 "kujou" are in one loop
boolean isCountOfBeAndStoreRight = (receiveCountOfBeAndStore == loopTime * 3);
return isCountOfTheRight && isCountOfBeAndStoreRight;
}
};
try {
boolean result = JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 90, validator);
assertTrue("Topology should pass the validator", result);
} finally {
localDRPC.shutdown();
}
}
use of storm.trident.operation.builtin.FilterNull in project jstorm by alibaba.
the class TridentMapExample method buildTopology.
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();
}
use of storm.trident.operation.builtin.FilterNull in project jstorm by alibaba.
the class TridentWordCount method buildTopology.
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();
}
use of storm.trident.operation.builtin.FilterNull in project jstorm by alibaba.
the class TridentMapTest method testTridentMap.
// to make sure the validator is right
@Test
public void testTridentMap() {
LocalDRPC localDRPC = new LocalDRPC();
FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("word"), SPOUT_BATCH_SIZE, 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"));
TridentTopology tridentTopology = new TridentTopology();
TridentState wordCount = tridentTopology.newStream("spout", spout).parallelismHint(1).flatMap(new SplitFlatMapFunction()).map(new UpperMapFunction()).filter(new Fields("word"), new WordFilter("THE")).peek(new LogConsumer()).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
tridentTopology.newDRPCStream("words", localDRPC).flatMap(new SplitFlatMapFunction()).groupBy(new Fields("args")).stateQuery(wordCount, new Fields("args"), new MapGet(), new Fields("result")).filter(new Fields("result"), new FilterNull()).aggregate(new Fields("result"), new Sum(), new Fields("sum"));
Map config = new HashMap();
config.put(Config.TOPOLOGY_NAME, "TridentMapTest");
JStormUnitTestValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {
Logger LOG = LoggerFactory.getLogger(JStormUnitTestValidator.class);
@Override
public boolean validate(Map config) {
String queryResult = executeLocalDRPC("words", "THE");
//the result is like [[8080]], so remove the [[]]
queryResult = queryResult.substring(2, queryResult.length() - 2);
//how many times of emit can finish a loop
int oneLoopNeedEmits = (int) Math.ceil(SPOUT_CONTENT_TYPES / (float) SPOUT_BATCH_SIZE);
// of all the spout content
//the loop time of the LimitFixBatchSpout content
int loopTime = SPOUT_LIMIT / oneLoopNeedEmits;
int receiveCountOfUpperCase = Integer.valueOf(queryResult);
LOG.info("Final receive total " + receiveCountOfUpperCase + " \"THE\" when expected " + (loopTime * 5));
//5 "the" are in one loop
boolean isCountOfUpperCaseRight = (receiveCountOfUpperCase == loopTime * 5);
//query the word count of these 3 words total
queryResult = executeLocalDRPC("words", "the tHe thE");
queryResult = queryResult.substring(2, queryResult.length() - 2);
int receiveCountOfLowerCase = Integer.valueOf(queryResult);
LOG.info("Final receive total " + receiveCountOfLowerCase + " \"the\" and \"tHe\" and \"thE\" " + "when expected " + 0);
//no the tHe thE should pass the UpperFilter
boolean isCountOfLowerCaseRight = (receiveCountOfLowerCase == 0);
return isCountOfUpperCaseRight && isCountOfLowerCaseRight;
}
};
try {
boolean result = JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 90, validator);
assertTrue("Topology should pass the validator", result);
} finally {
localDRPC.shutdown();
}
}
Aggregations