2019-02-02 04:30:04 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[emr.java.create-cluster.runjobflow]
import com.amazonaws.AmazonClientException ;
import com.amazonaws.auth.AWSCredentials ;
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 ) {
2021-06-28 12:51:34 -07:00
AWSCredentialsProvider profile = null ;
2019-02-02 04:30:04 +00:00
try {
2021-06-28 12:51:34 -07:00
credentials_profile = new ProfileCredentialsProvider ( " default " ) ; // specifies any named profile in
// .aws/credentials as the credentials provider
2019-02-02 04:30:04 +00:00
} catch ( Exception e ) {
throw new AmazonClientException (
" Cannot load credentials from .aws/credentials file. " +
" Make sure that the credentials file exists and that the profile name is defined within it. " ,
e ) ;
}
2024-01-16 10:41:11 -05:00
2019-02-02 04:30:04 +00:00
// create an EMR client using the credentials and region specified in order to
// create the cluster
AmazonElasticMapReduce emr = AmazonElasticMapReduceClientBuilder . standard ( )
2021-06-28 12:51:34 -07:00
. withCredentials ( credentials_profile )
2019-02-06 20:25:22 +00:00
. withRegion ( Regions . US_WEST_1 )
2019-02-02 04:30:04 +00:00
. build ( ) ;
2024-01-16 10:41:11 -05:00
2019-02-02 04:30:04 +00:00
// create a step to enable debugging in the AWS Management Console
StepFactory stepFactory = new StepFactory ( ) ;
StepConfig enabledebugging = new StepConfig ( )
. withName ( " Enable debugging " )
. withActionOnFailure ( " TERMINATE_JOB_FLOW " )
. withHadoopJarStep ( stepFactory . newEnableDebuggingStep ( ) ) ;
2024-01-16 10:41:11 -05:00
2019-02-02 04:30:04 +00:00
// specify applications to be installed and configured when EMR creates the
// cluster
Application hive = new Application ( ) . withName ( " Hive " ) ;
Application spark = new Application ( ) . withName ( " Spark " ) ;
Application ganglia = new Application ( ) . withName ( " Ganglia " ) ;
Application zeppelin = new Application ( ) . withName ( " Zeppelin " ) ;
2024-01-16 10:41:11 -05:00
2019-02-02 04:30:04 +00:00
// create the cluster
RunJobFlowRequest request = new RunJobFlowRequest ( )
. withName ( " MyClusterCreatedFromJava " )
. withReleaseLabel ( " emr-5.20.0 " ) // specifies the EMR release version label, we recommend the latest release
. withSteps ( enabledebugging )
. withApplications ( hive , spark , ganglia , zeppelin )
. withLogUri ( " s3://path/to/my/emr/logs " ) // a URI in S3 for log files is required when debugging is enabled
. withServiceRole ( " EMR_DefaultRole " ) // replace the default with a custom IAM service role if one is used
. withJobFlowRole ( " EMR_EC2_DefaultRole " ) // replace the default with a custom EMR role for the EC2 instance
// profile if one is used
. withInstances ( new JobFlowInstancesConfig ( )
. withEc2SubnetId ( " subnet-12ab34c56 " )
. withEc2KeyName ( " myEc2Key " )
. withInstanceCount ( 3 )
. withKeepJobFlowAliveWhenNoSteps ( true )
. withMasterInstanceType ( " m4.large " )
. withSlaveInstanceType ( " m4.large " ) ) ;
RunJobFlowResult result = emr . runJobFlow ( request ) ;
System . out . println ( " The cluster ID is " + result . toString ( ) ) ;
}
}
2021-06-28 12:51:34 -07:00
// snippet-end:[emr.java.create-cluster.runjobflow]