How to programmatically kill a Apache Storm topology?

7.4k views Asked by At

I am using a Java class to submit a topology to a Storm cluster and I also plan to use a Java class to kill the topology. But as per storm documentation, the following command is used to kill a topology and there is no Java method (and this has valid reasons).

storm kill {stormname}

So is it fine to call a shell script from Java class to kill the topology? What are the other ways to kill topology?

Also, how to get the status of running topologies in storm cluster?

2

There are 2 answers

3
Vishal John On BEST ANSWER

For killing topology you can try this

import backtype.storm.generated.KillOptions
import backtype.storm.generated.Nimbus.Client;
import backtype.storm.utils.NimbusClient
import backtype.storm.utils.Utils

Map conf = Utils.readStormConfig();
Client client = NimbusClient.getConfiguredClient(conf).getClient();
KillOptions killOpts = new KillOptions();
//killOpts.set_wait_secs(waitSeconds); // time to wait before killing
client.killTopologyWithOpts(topology_name, killOpts); //provide topology name

To get the status of topology running

Client client = NimbusClient.getConfiguredClient(conf).getClient();
List<TopologySummary> topologyList = client.getClusterInfo.get_topologies();
// loop through the list and check if the required topology name is present in the list
// if not it's not running
0
Nav On

As of Storm 1.0.0, killing a topology from within a spout or bolt needs you to specify the nimbus host location via nimbus.seeds (or if you aren't doing it through code, you need to specify the nimbus.seeds in the storm.yaml file):

import org.apache.storm.utils.NimbusClient;
import org.apache.storm.utils.Utils;

void somewhereInASpoutOrBolt() {
Map conf = Utils.readStormConfig();
conf.put("nimbus.seeds", "localhost");

NimbusClient cc = NimbusClient.getConfiguredClient(conf);

Nimbus.Client client = cc.getClient();
client.killTopology("MyStormTopologyName");
}

Do note that doing so will end your program too.