use of backtype.storm.LocalDRPC 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 backtype.storm.LocalDRPC in project jstorm by alibaba.
the class TridentReachTest method testTridentReach.
@Test
public void testTridentReach() {
TridentTopology tridentTopology = new TridentTopology();
TridentState urlToTweeters = tridentTopology.newStaticState(new TridentReach.StaticSingleKeyMapState.Factory(TWEETERS));
TridentState tweetersToFollowers = tridentTopology.newStaticState(new TridentReach.StaticSingleKeyMapState.Factory(FOLLOWERS));
LocalDRPC localDRPC = new LocalDRPC();
tridentTopology.newDRPCStream("reach", localDRPC).stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields("tweeters")).each(new Fields("tweeters"), new TridentReach.ExpandList(), new Fields("tweeter")).shuffle().stateQuery(tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers")).each(new Fields("followers"), new TridentReach.ExpandList(), new Fields("follower")).groupBy(new Fields("follower")).aggregate(new TridentReach.One(), new Fields("one")).aggregate(new Fields("one"), new Sum(), new Fields("reach"));
Map config = new HashMap();
config.put(Config.TOPOLOGY_NAME, "TridentReachTest");
JStormUnitTestDRPCValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {
@Override
public boolean validate(Map config) {
String query = executeLocalDRPC("reach", "aaa");
assertEquals("[[0]]", query);
query = executeLocalDRPC("reach", "foo.com/blog/1");
assertEquals("[[16]]", query);
query = executeLocalDRPC("reach", "engineering.twitter.com/blog/5");
assertEquals("[[14]]", query);
return true;
}
};
try {
JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 120, validator);
} finally {
localDRPC.shutdown();
}
}
use of backtype.storm.LocalDRPC in project jstorm by alibaba.
the class TridentReach method test.
public static void test() {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("spout", new InOrderSpout(), 8);
builder.setBolt("count", new Check(), 8).fieldsGrouping("spout", new Fields("c1"));
conf.setMaxSpoutPending(20);
String[] className = Thread.currentThread().getStackTrace()[1].getClassName().split("\\.");
String topologyName = className[className.length - 1];
if (isLocal) {
drpc = new LocalDRPC();
}
try {
JStormHelper.runTopology(buildTopology(drpc), topologyName, conf, 60, new DrpcValidator(), isLocal);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Assert.fail("Failed");
}
}
use of backtype.storm.LocalDRPC in project jstorm by alibaba.
the class BasicDRPCTopology method testDrpc.
public static void testDrpc() {
LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("exclamation");
builder.addBolt(new ExclaimBolt(), 3);
Config conf = new Config();
LocalDRPC drpc = new LocalDRPC();
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("drpc-demo", conf, builder.createLocalTopology(drpc));
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
try {
for (String word : new String[] { "hello", "goodbye" }) {
System.out.println("Result for \"" + word + "\": " + drpc.execute("exclamation", word));
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Failed to run DRPC Test");
}
drpc.shutdown();
cluster.shutdown();
}
use of backtype.storm.LocalDRPC in project jstorm by alibaba.
the class ManualDRPC method testDrpc.
public static void testDrpc() {
TopologyBuilder builder = new TopologyBuilder();
LocalDRPC drpc = new LocalDRPC();
DRPCSpout spout = new DRPCSpout("exclamation", drpc);
builder.setSpout("drpc", spout);
builder.setBolt("exclaim", new ExclamationBolt(), 3).shuffleGrouping("drpc");
builder.setBolt("return", new ReturnResults(), 3).shuffleGrouping("exclaim");
LocalCluster cluster = new LocalCluster();
Config conf = new Config();
cluster.submitTopology("exclaim", conf, builder.createTopology());
JStormUtils.sleepMs(30 * 1000);
try {
System.out.println(drpc.execute("exclamation", "aaa"));
System.out.println(drpc.execute("exclamation", "bbb"));
} catch (Exception e) {
Assert.fail("Failed to test drpc");
}
drpc.shutdown();
cluster.shutdown();
}
Aggregations