

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用创建 适用于 Java 的 Amazon SDK Amazon EMR 集群
<a name="calling-emr-with-java-sdk"></a>

 适用于 Java 的 Amazon SDK 提供了三个具有 Amazon EMR 功能的软件包：
+  [com.amazonaws.services.elasticmapreduce](https://docs.amazonaws.cn/AWSJavaSDK/latest/javadoc/com/amazonaws/services/elasticmapreduce/package-summary.html) 
+  [com.amazonaws.services.elasticmapreduce.model](https://docs.amazonaws.cn/AWSJavaSDK/latest/javadoc/com/amazonaws/services/elasticmapreduce/model/package-summary.html) 
+  [com.amazonaws.services.elasticmapreduce.util](https://docs.amazonaws.cn/AWSJavaSDK/latest/javadoc/com/amazonaws/services/elasticmapreduce/util/package-summary.html) 

有关这些包的更多信息，请参阅 [适用于 Java 的 Amazon SDK API 参考](https://docs.amazonaws.cn/sdk-for-java/latest/reference/)。

以下示例说明了如何 SDKs 使用 Amazon EMR 简化编程。下面的代码示例使用 `StepFactory` 对象（用于创建通用 Amazon EMR 步骤类型的帮助程序类）创建一个启用了调试的交互式 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());

	}

}
```

您必须至少分别传递与 EMR\$1 和 EMR\$1 \$1 对应的服务角色DefaultRole 和任务流角色。EC2 DefaultRole你可以通过为同一个账户调用这个 Amazon CLI 命令来做到这一点。首先，查看这两个角色是否已存在：

```
aws iam list-roles | grep EMR
```

如果存在实例配置文件 (EMR EC2 \$1DefaultRole) 和服务角色 (EMR\$1DefaultRole)，则会同时显示它们：

```
"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
```