6 In the new Java project right click the src folder then choose New and Class

6 in the new java project right click the src folder

This preview shows page 389 - 391 out of 395 pages.

6. In the new Java project, right-click the src folder, then choose New and Class . 7. In the Java Class dialog, in the Name field, enter a name for your new class, for example main . 8. In the Which method stubs would you like to create? section, choose public static void main(String[] args) and Finish . 9. Enter the Java source code inside your new class and add the appropriate import statements for the classes and methods in the sample. For your convenience, the full source code listing is shown below. Note In the following sample code, replace the example cluster ID (JobFlowId), j- xxxxxxxxxxxx , with a valid cluster ID in your account found either in the AWS Management Console or by using the following AWS CLI command: aws emr list-clusters --active | grep "Id" In addition, replace the example Amazon S3 path, s3://path/to/my/jarfolder , with the valid path to your JAR. Lastly, replace the example class name, com.my.Main1 , with the correct name of the class in your JAR, if applicable. import com.amazonaws.AmazonClientException; import com.amazonaws.auth.AWSCredentials; 383
Image of page 389
Amazon EMR Management Guide Common Concepts for API Calls import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.elasticmapreduce.AmazonElasticMapReduce; import com.amazonaws.services.elasticmapreduce.AmazonElasticMapReduceClientBuilder; import com.amazonaws.services.elasticmapreduce.model.*; import com.amazonaws.services.elasticmapreduce.util.StepFactory; public class Main { public static void main(String[] args) { AWSCredentials credentials_profile = null; try { credentials_profile = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException( "Cannot load credentials from .aws/credentials file. " + "Make sure that the credentials file exists and the profile name is specified within it.", e); } AmazonElasticMapReduce emr = AmazonElasticMapReduceClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(credentials_profile)) .withRegion(Regions.US_WEST_1) .build(); // Run a bash script using a predefined step in the StepFactory helper class StepFactory stepFactory = new StepFactory(); StepConfig runBashScript = new StepConfig() .withName("Run a bash script") .withHadoopJarStep(stepFactory.newScriptRunnerStep("s3://jeffgoll/emr-scripts/ create_users.sh")) .withActionOnFailure("CONTINUE"); // Run a custom jar file as a step HadoopJarStepConfig hadoopConfig1 = new HadoopJarStepConfig() .withJar("s3://path/to/my/jarfolder") // replace with the location of the jar to run as a step .withMainClass("com.my.Main1") // optional main class, this can be omitted if jar above has a manifest .withArgs("--verbose"); // optional list of arguments to pass to the jar StepConfig myCustomJarStep = new StepConfig("RunHadoopJar", hadoopConfig1); AddJobFlowStepsResult result = emr.addJobFlowSteps(new AddJobFlowStepsRequest() .withJobFlowId("j-xxxxxxxxxxxx") // replace with cluster id to run the steps .withSteps(runBashScript,myCustomJarStep)); System.out.println(result.getStepIds()); } } 10. Choose Run , Run As , and Java Application .
Image of page 390
Image of page 391

You've reached the end of your free preview.

Want to read all 395 pages?

  • Spring '12
  • LauraParker
  • Amazon Web Services, Amazon Elastic Compute Cloud

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors