

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

# EMR Studio 中的 Notebook CLI 命令示例
<a name="emr-managed-notebooks-headless-cli"></a>

本主题显示了 EMR Notebook 的 CLI 命令示例。该示例使用 EMR Notebooks 控制台中的演示 Notebook。要查找 Notebook，请使用相对于主目录的文件路径。在此示例中，您可以运行两个 Notebook 文件：`demo_pyspark.ipynb` 和 `my_folder/python3.ipynb`。

**注意**  
EMR Notebooks 在控制台中作为 Amazon EMR Studio Workspaces 提供。通过控制台中的**创建 Workspace** 按钮，可以创建新的 Notebooks。要访问或创建 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_cn/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_cn/emr/latest/ManagementGuide/images/notebook_exe_folder_structure_2.png)


有关 Amazon EMR API `NotebookExecution` 操作的更多信息，请参阅 [Amazon EMR API actions](https://docs.aws.amazon.com/emr/latest/APIReference/API_Operations.html)。

## 运行 Notebook
<a name="emr-managed-notebooks-api-actions"></a>

您可以使用 AWS CLI 来运行带有`start-notebook-execution`操作的笔记本，如以下示例所示。

**Example ：使用（运行在 Amazon EC2 上的）Amazon EMR 集群在 EMR Studio Workspace 中执行 EMR Notebook。**  

```
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 Notebook。**  

```
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 ：执行 EMR Notebook 来指定其 Amazon S3 位置**  

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

## Notebook 输出
<a name="emr-managed-notebooks-headless-cli-output"></a>

 下面是示例 Notebook 的输出内容。单元格 3 显示新注入的参数值。

![Jupyter 笔记本单元格显示参数注入，单元格 3 中的值被覆盖。](http://docs.aws.amazon.com/zh_cn/emr/latest/ManagementGuide/images/HelloWorld_notebook.png)


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

您可以使用 `describe-notebook-execution` 操作以访问关于特定 Notebook 执行的信息。

```
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": []
    }
}
```

## 停止运行 Notebook
<a name="emr-managed-notebooks-headless-cli-stop"></a>

如果您想要停止 Notebook 正在运行的执行，您可以借助 `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": []
    }
}
```

## 按开启时间列出 Notebook 的执行情况
<a name="emr-managed-notebooks-headless-cli-list"></a>

您可以将 `--from` 参数输入至 `list-notebook-executions`，按开启时间列出 Notebook 的执行情况。

```
# 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
        }
    ]
}
```

## 按开启时间和状态列出 Notebook 的执行情况
<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
        }
    ]
}
```