

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

# EMR Studio 中的筆記本 CLI 命令範例
<a name="emr-managed-notebooks-headless-cli"></a>

本主題顯示 EMR 筆記本的 CLI 命令範例。此範例使用來自 EMR Notebooks 主控台的示範筆記本。若要尋找筆記本，請使用相對於主目錄的檔案路徑。在此範例中，您可以執行兩個筆記本檔案：`demo_pyspark.ipynb` 和 `my_folder/python3.ipynb`。

**注意**  
EMR Notebooks 可在 主控台中做為 EMR Studio Workspaces 使用。主控台中的**建立工作區**按鈕可讓您建立新的筆記本。若要存取或建立工作區，EMR Notebooks 使用者需要其他 IAM 角色許可。如需詳細資訊，請參閱 [主控台中的 Amazon EMR Notebooks 是 Amazon EMR Studio Workspaces](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-migration.html) 和 [Amazon EMR 主控台](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html)。

檔案 `demo_pyspark.ipynb` 的相對路徑為 `demo_pyspark.ipynb`，如下所示。

![Jupyter 筆記本界面顯示 demo_pyspark.ipynb 檔案，其中包含程式碼儲存格和安裝所需的程式庫標題。](http://docs.aws.amazon.com/zh_tw/emr/latest/ManagementGuide/images/notebook_exe_folder_structure_1.png)


`python3.ipynb` 的相對路徑為 `my_folder/python3.ipynb`，如下所示。

![檔案瀏覽器顯示位於 my_folder 目錄中的 python3.ipynb 檔案。](http://docs.aws.amazon.com/zh_tw/emr/latest/ManagementGuide/images/notebook_exe_folder_structure_2.png)


如需有關 Amazon EMR API `NotebookExecution` 動作的資訊，請參閱 [Amazon EMR API 動作](https://docs.aws.amazon.com/emr/latest/APIReference/API_Operations.html)。

## 執行筆記本
<a name="emr-managed-notebooks-api-actions"></a>

您可以使用 AWS CLI 搭配 `start-notebook-execution`動作執行筆記本，如下列範例所示。

**Example - 在具有 Amazon EMR (在 Amazon EC2 上執行) 叢集的 EMR Studio Workspace 中執行 EMR 筆記本**  

```
aws emr --region {{us-east-1}} \
start-notebook-execution \
--editor-id {{e-ABCDEFG123456}} \
--notebook-params '{"input_param":"{{my-value}}", "{{good_superhero}}":["{{superman}}", "{{batman}}"]}' \
--relative-path {{test.ipynb}} \
--notebook-execution-name {{my-execution}} \
--execution-engine '{"Id" : "{{j-1234ABCD123}}"}' \
--service-role EMR_Notebooks_DefaultRole 
 
{
    "NotebookExecutionId": "{{ex-ABCDEFGHIJ1234ABCD}}"
}
```

**Example - 在具有 EMR Notebooks 叢集的 EMR Studio Workspace 中執行 EMR 筆記本**  

```
aws emr start-notebook-execution \
    --region {{us-east-1}} \
    --service-role EMR_Notebooks_DefaultRole \
    --environment-variables '{"KERNEL_EXTRA_SPARK_OPTS": "--conf spark.executor.instances=1", "KERNEL_LAUNCH_TIMEOUT": "350"}' \
    --output-notebook-format HTML \
    --execution-engine Id=arn:aws:emr-containers:us-west-2:{{account-id}}:/virtualclusters/{{ABCDEFG}}/endpoints/{{ABCDEF}},Type=EMR_ON_EKS,ExecutionRoleArn=arn:aws:iam::{{account-id}}:role/{{execution-role}} \
    --editor-id e-{{ABCDEFG}} \
    --relative-path {{EMRonEKS-spark_python.ipynb}}
```

**Example - 執行指定其 Amazon S3 位置的 EMR 筆記本**  

```
aws emr start-notebook-execution \
    --region {{us-east-1}} \
    --notebook-execution-name {{my-execution-on-emr-on-eks-cluster}} \
    --service-role EMR_Notebooks_DefaultRole \
    --environment-variables '{"KERNEL_EXTRA_SPARK_OPTS": "--conf spark.executor.instances=1", "KERNEL_LAUNCH_TIMEOUT": "350"}' \
    --output-notebook-format HTML \
    --execution-engine Id=arn:aws:emr-containers:us-west-2:{{account-id}}:/virtualclusters/{{ABCDEF}}/endpoints/{{ABCDEF}},Type=EMR_ON_EKS,ExecutionRoleArn=arn:aws:iam::{{account-id}}:role/{{execution-role}} \
    --notebook-s3-location '{"Bucket": "{{amzn-s3-demo-bucket}}","Key": "{{s3-prefix-to-notebook-location}}/EMRonEKS-spark_python.ipynb"}' \
    --output-notebook-s3-location '{"Bucket": "{{amzn-s3-demo-bucket}}","Key": "{{s3-prefix-for-storing-output-notebook}}"}'
```

## 筆記本輸出
<a name="emr-managed-notebooks-headless-cli-output"></a>

 以下是範例筆記本的輸出。儲存格 3 顯示新插入的參數值。

![Jupyter 筆記本儲存格顯示在儲存格 3 中覆寫值的參數注入。](http://docs.aws.amazon.com/zh_tw/emr/latest/ManagementGuide/images/HelloWorld_notebook.png)


## 描述筆記本
<a name="emr-managed-notebooks-headless-cli-describe"></a>

可以使用 `describe-notebook-execution` 動作來存取有關特定筆記本執行的資訊。

```
aws emr --region us-east-1 \
describe-notebook-execution --notebook-execution-id ex-IZWZZVR9DKQ9WQ7VZWXJZR29UGHTE
 
{
    "NotebookExecution": {
        "NotebookExecutionId": "ex-IZWZZVR9DKQ9WQ7VZWXJZR29UGHTE",
        "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
        "ExecutionEngine": {
            "Id": "j-2QMOV6JAX1TS2",
            "Type": "EMR",
            "MasterInstanceSecurityGroupId": "sg-05ce12e58cd4f715e"
        },
        "NotebookExecutionName": "my-execution",
        "NotebookParams": "{\"input_param\":\"my-value\", \"good_superhero\":[\"superman\", \"batman\"]}",
        "Status": "FINISHED",
        "StartTime": 1593490857.009,
        "Arn": "arn:aws:elasticmapreduce:us-east-1:123456789012:notebook-execution/ex-IZWZZVR9DKQ9WQ7VZWXJZR29UGHTE",
        "LastStateChangeReason": "Execution is finished for cluster j-2QMOV6JAX1TS2.",
        "NotebookInstanceSecurityGroupId": "sg-0683b0a39966d4a6a",
        "Tags": []
    }
}
```

## 停止筆記本
<a name="emr-managed-notebooks-headless-cli-stop"></a>

如果筆記本正在運作您想要停止的執行，可以使用 `stop-notebook-execution` 命令來完成。

```
# stop a running execution
aws emr --region us-east-1 \
stop-notebook-execution --notebook-execution-id ex-IZWZX78UVPAATC8LHJR129B1RBN4T
 
 
# describe it
aws emr --region us-east-1 \
describe-notebook-execution --notebook-execution-id ex-IZWZX78UVPAATC8LHJR129B1RBN4T
 
{
    "NotebookExecution": {
        "NotebookExecutionId": "ex-IZWZX78UVPAATC8LHJR129B1RBN4T",
        "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
        "ExecutionEngine": {
            "Id": "j-2QMOV6JAX1TS2",
            "Type": "EMR"
        },
        "NotebookExecutionName": "my-execution",
        "NotebookParams": "{\"input_param\":\"my-value\", \"good_superhero\":[\"superman\", \"batman\"]}",
        "Status": "STOPPED",
        "StartTime": 1593490876.241,
        "Arn": "arn:aws:elasticmapreduce:us-east-1:123456789012:editor-execution/ex-IZWZX78UVPAATC8LHJR129B1RBN4T",
        "LastStateChangeReason": "Execution is stopped for cluster j-2QMOV6JAX1TS2. Internal error",
        "Tags": []
    }
}
```

## 依開始時間列出筆記本的執行
<a name="emr-managed-notebooks-headless-cli-list"></a>

可以將 `--from` 參數傳遞給 `list-notebook-executions`，依開始時間列出筆記本的執行。

```
# filter by start time 
aws emr --region us-east-1 \ 
list-notebook-executions --from 1593400000.000
 
{
    "NotebookExecutions": [
        {
            "NotebookExecutionId": "ex-IZWZX78UVPAATC8LHJR129B1RBN4T",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "STOPPED",
            "StartTime": 1593490876.241
        },
        {
            "NotebookExecutionId": "ex-IZWZZVR9DKQ9WQ7VZWXJZR29UGHTE",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "RUNNING",
            "StartTime": 1593490857.009
        },
        {
            "NotebookExecutionId": "ex-IZWZYRS0M14L5V95WZ9OQ399SKMNW",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "STOPPED",
            "StartTime": 1593490292.995
        },
        {
            "NotebookExecutionId": "ex-IZX009ZK83IVY5E33VH8MDMELVK8K",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "FINISHED",
            "StartTime": 1593489834.765
        },
        {
            "NotebookExecutionId": "ex-IZWZXOZF88JWDF9J09GJ91R57VI0N",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "FAILED",
            "StartTime": 1593488934.688
        }
    ]
}
```

## 依開始時間和狀態列出筆記本的執行
<a name="emr-managed-notebooks-headless-cli-list"></a>

`list-notebook-executions` 命令也可使用 `--status` 參數來篩選結果。

```
# filter by start time and status 
aws emr --region us-east-1 \                 
list-notebook-executions --from 1593400000.000 --status FINISHED
{
    "NotebookExecutions": [
        {
            "NotebookExecutionId": "ex-IZWZZVR9DKQ9WQ7VZWXJZR29UGHTE",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "FINISHED",
            "StartTime": 1593490857.009
        },
        {
            "NotebookExecutionId": "ex-IZX009ZK83IVY5E33VH8MDMELVK8K",
            "EditorId": "e-BKTM2DIHXBEDRU44ANWRKIU8N",
            "NotebookExecutionName": "my-execution",
            "Status": "FINISHED",
            "StartTime": 1593489834.765
        }
    ]
}
```