我已经根据@abhi 和@Nishu Tayal 的回答解决了这个问题,我想在这里发布我的代码:
public static void submitLocalTopologyWay1(String topologyName, Config topologyConf,
StormTopology topology, String localJar) {
try {
//get default storm config
Map defaultStormConf = Utils.readStormConfig();
defaultStormConf.putAll(topologyConf);
//set JAR
System.setProperty("storm.jar",localJar);
//submit topology
StormSubmitter.submitTopology(topologyName, defaultStormConf, topology);
} catch (Exception e) {
String errorMsg = "can't deploy topology " + topologyName + ", " + e.getMessage();
System.out.println(errorMsg);
e.printStackTrace();
}
}
public static void submitLocalTopologyWay2(String topologyName, Config topologyConf,
StormTopology topology, String localJar) {
try {
//get nimbus client
Map defaultStormConf = Utils.readStormConfig();
defaultStormConf.putAll(topologyConf);
Client client = NimbusClient.getConfiguredClient(defaultStormConf).getClient();
//upload JAR
String remoteJar = StormSubmitter.submitJar(defaultStormConf, localJar);
//submit topology
client.submitTopology(topologyName, remoteJar, JSONValue.toJSONString(topologyConf), topology);
} catch (Exception e) {
String errorMsg = "can't deploy topology " + topologyName + ", " + e.getMessage();
System.out.println(errorMsg);
e.printStackTrace();
}
}
那么这是一个测试,您必须先将代码构建到 JAR 文件中。
public void testSubmitTopologySubmitLocalTopologyWay1() {
Config config = new Config();
config.put(Config.NIMBUS_HOST,"9.119.84.179");
config.put(Config.NIMBUS_THRIFT_PORT, 6627);
config.put(Config.STORM_ZOOKEEPER_SERVERS, Arrays.asList("9.119.84.177","9.119.84.178","9.119.84.176"));
config.put(Config.STORM_ZOOKEEPER_PORT,2181);
config.put(Config.TOPOLOGY_WORKERS, 3);
RemoteSubmitter.submitLocalTopologyWay1("word-count-test-1", config,
WordCountTopology.buildTopology(), // your topology
"C:\\MyWorkspace\\project\\storm-sample-0.0.1-SNAPSHOT-jar-with-dependencies.jar");//the JAR file
}