Question
I am launching emr cluster using Java API but not able to associate a tag to it. Pl can you help me on this.
Using EMR CLI, it is very easy as below but I have to do this using my Java code
./elastic-mapreduce --create --alive --tag tagKey=stackOverflow
If you need more details on this, pl let me know..
Thanks in advance.
Regards, Vineet
Solution
In earlier versions of EMR Java SDK there wasn't a way to add tags(not a direct one for sure) to add tags, but in newer versions of the Java SDK for EMR, there is a method named addTags(Collection<Tag> tags)
, using which you can add tags to the resources(EC2) being launched as part of the EMR cluster.
You would use it as follows:
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonElasticMapReduce emr = new AmazonElasticMapReduceClient(credentials);
List<Tag> tags = new ArrayList<Tag>();
Tag stackOverflowTag = new Tag();
stackOverflowTag.setKey("stackOverflow");
tags.add(stackOverflowTag);
AddTagsRequest addTagsRequest = new AddTagsRequest();
addTagsRequest.setTags(tags);
emr.addTags(addTagsRequest);
StepFactory stepFactory = new StepFactory();
// set up the cluster to launch and add steps
RunJobFlowResult result = emr.runJobFlow(request);
Use com.amazonaws.services.elasticmapreduce.model.Tag
class in order to create tags, as there are many Tag
classes present in the SDK, and for a moment I had imported the wrong one as well.
Read the doc here.