

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

# EMR Studio の Notebook CLI コマンドのサンプル
<a name="emr-managed-notebooks-headless-cli"></a>

このトピックでは、EMR ノートブックの CLI コマンドサンプルを示します。この例では、EMR Notebooks コンソールのデモノートブックを使用します。ノートブックを見つけるには、ホームディレクトリへの相対ファイルパスを使用します。この例では、`demo_pyspark.ipynb` と `my_folder/python3.ipynb` の 2 つのノートブックファイルを実行できます。

**注記**  
EMR Notebooks は、コンソールで EMR Studio Workspace として使用できます。コンソールの **[ワークスペースの作成]** ボタンを使用すると、新しいノートブックを作成できます。EMR Notebooks ユーザーが Workspace にアクセスしたり作成したりするには、追加の IAM ロール権限が必要です。詳細については、「[Amazon EMR Notebooks are Amazon EMR Studio Workspaces in the console](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-migration.html)」および「[Amazon EMR console](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html)」を参照してください。

ファイル `demo_pyspark.ipynb` の相対パスは、以下に示す `demo_pyspark.ipynb` です。

![コードセルを含む demo_pyspark.ipynb ファイルと、必要なライブラリの見出しのインストールを示す Jupyter ノートブックインターフェイス。](http://docs.aws.amazon.com/ja_jp/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/ja_jp/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)」を参照してください。

## ノートブックを実行する
<a name="emr-managed-notebooks-api-actions"></a>

次の例に示すように、 を使用して `start-notebook-execution`アクションでノートブック AWS CLI を実行できます。

**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 Notebooks を実行する**  

```
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 Notebooks を実行する**  

```
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 は、新しく挿入されたパラメータ値を示します。

![値がセル 3 で上書きされたパラメータインジェクションを示す Jupyter Notebook セル。](http://docs.aws.amazon.com/ja_jp/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>

ノートブックの実行を開始時間別に一覧表示するには、`list-notebook-executions` に `--from` パラメータを渡します。

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