

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定 Amazon EMR 的受管擴展
<a name="managed-scaling-configure"></a>

下列各節說明如何啟動使用受管擴展搭配 適用於 Java 的 AWS SDK AWS 管理主控台、 或 的 EMR 叢集 AWS Command Line Interface。

**Topics**
+ [使用 AWS 管理主控台 設定受管擴展](#managed-scaling-console)
+ [使用 AWS CLI 設定受管擴展](#managed-scaling-cli)
+ [使用 適用於 Java 的 AWS SDK 設定受管擴展](#managed-scaling-sdk)

## 使用 AWS 管理主控台 設定受管擴展
<a name="managed-scaling-console"></a>

可以使用 Amazon EMR 主控台在建立叢集時設定受管擴展，或變更執行中叢集的受管擴展政策。

------
#### [ Console ]

**使用主控台建立叢集時設定受管擴展**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/emr](https://console.aws.amazon.com/emr) 開啟 Amazon EMR 主控台。

1. 在左側導覽窗格中的 **EMR on EC2** 下方，選擇**叢集**，然後選擇**建立叢集**。

1. 選擇 Amazon EMR 版本 **emr-5.30.0** 或更新版本，**emr-6.0.0** 版本除外。

1. 在**叢集擴展和佈建**選項下，選擇**使用 EMR 受管擴展**。指定執行個體數量的**下限**和**上限**、**核心節點執行個體上限**以及**隨需執行個體上限**。

1. 選擇適用於您的叢集的任何其他選項。

1. 若要啟動您的叢集，請選擇**建立叢集**。

**使用主控台在現有叢集上設定受管擴展**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/emr](https://console.aws.amazon.com/emr) 開啟 Amazon EMR 主控台。

1. 在左側導覽窗格中的 **EMR on EC2** 下，選擇**叢集**，然後選取您要更新的叢集。

1. 在叢集詳細資訊頁面的**執行個體**索引標籤中，尋找**執行個體群組設定**區段。選擇**編輯叢集擴展**，指定執行個體數量**下限**和**上限**以及**隨需限制**的新值。

------

## 使用 AWS CLI 設定受管擴展
<a name="managed-scaling-cli"></a>

您可以在建立叢集時使用 Amazon EMR AWS CLI 命令來設定受管擴展。可使用速記語法來指定相關命令中內嵌的 JSON 組態，或是以含有組態 JSON 的檔案做為參照。您也可以將受管擴展政策套用至現有叢集，並移除先前套用的受管理擴展政策。此外還能從執行中的叢集上擷取調整規模政策組態的詳細資訊。

**在叢集啟動期間啟用受管擴展**

您可以在叢集啟動期間啟用受管擴展，如下面範例所示。

```
aws emr create-cluster \
 --service-role EMR_DefaultRole \
 --release-label emr-7.12.0 \
 --name EMR_Managed_Scaling_Enabled_Cluster \
 --applications Name=Spark Name=Hbase \
 --ec2-attributes KeyName=keyName,InstanceProfile=EMR_EC2_DefaultRole \
 --instance-groups InstanceType=m4.xlarge,InstanceGroupType=MASTER,InstanceCount=1 InstanceType=m4.xlarge,InstanceGroupType=CORE,InstanceCount=2 \
 --region us-east-1 \
 --managed-scaling-policy ComputeLimits='{MinimumCapacityUnits=2,MaximumCapacityUnits=4,UnitType=Instances}'
```

您也可以指定受管政策設定，要求在使用 `create-cluster` 時，使用管理擴展政策選項 。

**將受管擴展政策套用至現有叢集**

您可以將受管擴展政策套用至現有叢集，如下列範例所示。

```
aws emr put-managed-scaling-policy  
--cluster-id j-123456  
--managed-scaling-policy ComputeLimits='{MinimumCapacityUnits=1,
MaximumCapacityUnits=10,  MaximumOnDemandCapacityUnits=10, UnitType=Instances}'
```

您也可以使用 `aws emr put-managed-scaling-policy` 命令，將受管擴展政策套用至現有叢集。以下範例參考的是 JSON 檔案 `managedscaleconfig.json`，該檔案指定了受管擴展政策的組態。

```
aws emr put-managed-scaling-policy --cluster-id j-123456 --managed-scaling-policy file://./managedscaleconfig.json
```

下列範例中所示的 `managedscaleconfig.json` 檔案，其內容會定義受管擴展政策。

```
{
    "ComputeLimits": {
        "UnitType": "Instances",
        "MinimumCapacityUnits": 1,
        "MaximumCapacityUnits": 10,
        "MaximumOnDemandCapacityUnits": 10
    }
}
```

**擷取受管擴展政策組態**

`GetManagedScalingPolicy` 命令會擷取此政策組態。舉例而言，以下命令會擷取叢集 ID 為 `j-123456` 的叢集的組態。

```
aws emr get-managed-scaling-policy --cluster-id j-123456
```

該命令會產生以下的輸出範例。

```
 1. {
 2.    "ManagedScalingPolicy": { 
 3.       "ComputeLimits": { 
 4.          "MinimumCapacityUnits": 1,
 5.          "MaximumOnDemandCapacityUnits": 10,
 6.          "MaximumCapacityUnits": 10,
 7.          "UnitType": "Instances"
 8.       }
 9.    }
10. }
```

如需在 中使用 Amazon EMR 命令的詳細資訊 AWS CLI，請參閱 [https://docs.aws.amazon.com/cli/latest/reference/emr](https://docs.aws.amazon.com/cli/latest/reference/emr)。

**移除受管擴展政策**

`RemoveManagedScalingPolicy` 命令會移除此政策組態。舉例而言，以下命令會移除叢集 ID 為 `j-123456` 的叢集的組態。

```
aws emr remove-managed-scaling-policy --cluster-id j-123456
```

## 使用 適用於 Java 的 AWS SDK 設定受管擴展
<a name="managed-scaling-sdk"></a>

以下程式摘錄顯示如何使用 適用於 Java 的 AWS SDK設定受管擴展：

```
package com.amazonaws.emr.sample;

import java.util.ArrayList;
import java.util.List;

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.elasticmapreduce.AmazonElasticMapReduce;
import com.amazonaws.services.elasticmapreduce.AmazonElasticMapReduceClientBuilder;
import com.amazonaws.services.elasticmapreduce.model.Application;
import com.amazonaws.services.elasticmapreduce.model.ComputeLimits;
import com.amazonaws.services.elasticmapreduce.model.ComputeLimitsUnitType;
import com.amazonaws.services.elasticmapreduce.model.InstanceGroupConfig;
import com.amazonaws.services.elasticmapreduce.model.JobFlowInstancesConfig;
import com.amazonaws.services.elasticmapreduce.model.ManagedScalingPolicy;
import com.amazonaws.services.elasticmapreduce.model.RunJobFlowRequest;
import com.amazonaws.services.elasticmapreduce.model.RunJobFlowResult;

public class CreateClusterWithManagedScalingWithIG {

	public static void main(String[] args) {
		AWSCredentials credentialsFromProfile = getCreadentials("AWS-Profile-Name-Here");
		
		/**
		 * Create an Amazon EMR client with the credentials and region specified in order to create the cluster
		 */
		AmazonElasticMapReduce emr = AmazonElasticMapReduceClientBuilder.standard()
			.withCredentials(new AWSStaticCredentialsProvider(credentialsFromProfile))
			.withRegion(Regions.US_EAST_1)
			.build();
		
		/**
		 * Create Instance Groups - Primary, Core, Task
		 */
		InstanceGroupConfig instanceGroupConfigMaster = new InstanceGroupConfig()
				.withInstanceCount(1)
				.withInstanceRole("MASTER")
				.withInstanceType("m4.large")
				.withMarket("ON_DEMAND"); 
				
		InstanceGroupConfig instanceGroupConfigCore = new InstanceGroupConfig()
			.withInstanceCount(4)
			.withInstanceRole("CORE")
			.withInstanceType("m4.large")
			.withMarket("ON_DEMAND");
			
		InstanceGroupConfig instanceGroupConfigTask = new InstanceGroupConfig()
			.withInstanceCount(5)
			.withInstanceRole("TASK")
			.withInstanceType("m4.large")
			.withMarket("ON_DEMAND");

		List<InstanceGroupConfig> igConfigs = new ArrayList<>();
		igConfigs.add(instanceGroupConfigMaster);
		igConfigs.add(instanceGroupConfigCore);
		igConfigs.add(instanceGroupConfigTask);
		
        /**
         *  specify applications to be installed and configured when Amazon 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");
		
		/** 
		 * Managed Scaling Configuration - 
         * Using UnitType=Instances for clusters composed of instance groups
		 *
         * Other options are: 
         * UnitType = VCPU ( for clusters composed of instance groups)
         * UnitType = InstanceFleetUnits ( for clusters composed of instance fleets)
         **/
		ComputeLimits computeLimits = new ComputeLimits()
				.withMinimumCapacityUnits(1)
				.withMaximumCapacityUnits(20)
				.withUnitType(ComputeLimitsUnitType.Instances);
		
		ManagedScalingPolicy managedScalingPolicy = new ManagedScalingPolicy();
		managedScalingPolicy.setComputeLimits(computeLimits);
		
		// create the cluster with a managed scaling policy
		RunJobFlowRequest request = new RunJobFlowRequest()
	       		.withName("EMR_Managed_Scaling_TestCluster")
	       		.withReleaseLabel("emr-7.12.0")          // Specifies the version label for the Amazon EMR release; we recommend the latest release
	       		.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")      // If you use a custom IAM service role, replace the default role with the custom role.
	       		.withJobFlowRole("EMR_EC2_DefaultRole")  // If you use a custom Amazon EMR role for EC2 instance profile, replace the default role with the custom Amazon EMR role.
	       		.withInstances(new JobFlowInstancesConfig().withInstanceGroups(igConfigs)
	       	   		.withEc2SubnetId("subnet-123456789012345")
	           		.withEc2KeyName("my-ec2-key-name") 
	           		.withKeepJobFlowAliveWhenNoSteps(true))    
	       		.withManagedScalingPolicy(managedScalingPolicy);
	   RunJobFlowResult result = emr.runJobFlow(request); 
	   
	   System.out.println("The cluster ID is " + result.toString());
	}
	
	public static AWSCredentials getCredentials(String profileName) {
		// specifies any named profile in .aws/credentials as the credentials provider
		try {
			return new ProfileCredentialsProvider("AWS-Profile-Name-Here")
					.getCredentials(); 
        } 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);
        }
	}
	
	public CreateClusterWithManagedScalingWithIG() { }
}
```