AWS SDK for Java を使用して Amazon EMR クラスターを作成する - Amazon EMR

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK for Java を使用して Amazon EMR クラスターを作成する

AWS SDK for Java には、Amazon EMR 機能を備えた 3 つのパッケージが用意されています。

これらのパッケージの詳細については、「AWS SDK for Java API リファレンス」を参照してください。

次の例では、SDK で Amazon EMR を使用してプログラミングを簡素化する方法について説明します。このコード例では、一般的な Amazon EMR ステップタイプを作成するためのヘルパークラスである StepFactory オブジェクトを使用して、デバッグを有効にした状態でインタラクティブな Hive クラスターを作成します。

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) { AWSCredentialsProvider profile = null; try { credentials_profile = new ProfileCredentialsProvider("default"); // specifies any named profile in // .aws/credentials as the credentials provider } 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); } // create an EMR client using the credentials and region specified in order to // create the cluster AmazonElasticMapReduce emr = AmazonElasticMapReduceClientBuilder.standard() .withCredentials(credentials_profile) .withRegion(Regions.US_WEST_1) .build(); // 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()); // 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"); // 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()); } }

少なくとも、DefaultRoleそれぞれ EMR_ と EMR_EC2_ に対応するサービスロールDefaultRole とジョブフローロールを渡す必要があります。これを行うには、同じアカウントに対してこの AWS CLI コマンドを呼び出します。最初に、ロールが既に存在するかどうかを確認します。

aws iam list-roles | grep EMR

インスタンスプロファイル (EMR_EC2_DefaultRole) とサービスロール (EMR_DefaultRole) の両方が存在する場合に表示されます。

"RoleName": "EMR_DefaultRole", "Arn": "arn:aws:iam::AccountID:role/EMR_DefaultRole" "RoleName": "EMR_EC2_DefaultRole", "Arn": "arn:aws:iam::AccountID:role/EMR_EC2_DefaultRole"

デフォルトのロールが存在しない場合は、次のコマンドを使用して作成できます。

aws emr create-default-roles