

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Contoh perintah terprogram untuk EMR Notebooks
<a name="emr-managed-notebooks-headless"></a>

## Ikhtisar
<a name="emr-managed-notebooks-headless-overview"></a>

Anda dapat menjalankan notebook EMR dengan eksekusi APIs dari skrip atau dari baris perintah. Saat Anda memulai, menghentikan, membuat daftar, dan menjelaskan eksekusi notebook EMR di luar AWS konsol, Anda dapat mengontrol notebook EMR secara terprogram. Anda dapat meneruskan nilai parameter yang berbeda ke notebook dengan sel notebook berparameter. Ini menghilangkan kebutuhan untuk membuat salinan notebook untuk setiap set nilai parameter baru. Untuk informasi selengkapnya, lihat [tindakan Amazon EMR API](https://docs.aws.amazon.com/emr/latest/APIReference/API_Operations.html).

Anda dapat menjadwalkan atau mengelompokkan eksekusi notebook EMR dengan acara Amazon CloudWatch dan. AWS Lambda Untuk informasi selengkapnya, lihat [Menggunakan AWS Lambda dengan CloudWatch Acara Amazon](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html).

**catatan**  
EMR Notebooks tersedia sebagai EMR Studio Workspaces di konsol. Tombol **Create Workspace** di konsol memungkinkan Anda membuat notebook baru. Untuk mengakses atau membuat Ruang Kerja, pengguna EMR Notebooks memerlukan izin peran IAM tambahan. [Untuk informasi selengkapnya, lihat [Amazon EMR Notebook adalah Amazon EMR Studio Workspaces di konsol dan konsol Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-migration.html).](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html)

## Izin peran untuk eksekusi terprogram
<a name="emr-managed-notebooks-headless-permissions"></a>

Untuk menggunakan eksekusi terprogram dengan EMR Notebooks, Anda harus mengonfigurasi izin pengguna dengan kebijakan berikut:

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowExecutionActions",
      "Effect": "Allow",
      "Action": [
        "elasticmapreduce:StartNotebookExecution",
        "elasticmapreduce:DescribeNotebookExecution",
        "elasticmapreduce:ListNotebookExecutions"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Sid": "AllowPassingServiceRole",
      "Effect": "Allow",
      "Action": [
        "iam:PassRole"
      ],
      "Resource": [
        "arn:aws:iam::123456789012:role/EMR_Notebooks_DefaultRole"
      ]
    }
  ]
}
```

------

Saat menjalankan EMR Notebooks secara terprogram di kluster EMR Notebooks, Anda harus menambahkan izin tambahan ini:

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowRetrievingManagedEndpointCredentials",
      "Effect": "Allow",
      "Action": [
        "emr-containers:GetManagedEndpointSessionCredentials"
      ],
      "Resource": [
        "arn:aws:emr-containers:*:123456789012:/virtualclusters/virtual-cluster-id/endpoints/managed-endpoint-id"
      ],
      "Condition": {
        "StringEquals": {
          "emr-containers:ExecutionRoleArn": [
            "arn:aws:iam::123456789012:role/emr-on-eks-execution-role"
          ]
        }
      }
    },
    {
      "Sid": "AllowDescribingManagedEndpoint",
      "Effect": "Allow",
      "Action": [
        "emr-containers:DescribeManagedEndpoint"
      ],
      "Resource": [
        "arn:aws:emr-containers:*:123456789012:/virtualclusters/virtual-cluster-id/endpoints/managed-endpoint-id"
      ]
    }
  ]
}
```

------

## Keterbatasan dengan eksekusi terprogram
<a name="emr-managed-notebooks-headless-limit"></a>
+ Maksimal 100 eksekusi bersamaan didukung Wilayah AWS per akun.
+ Eksekusi dihentikan jika berjalan selama lebih dari 30 hari.
+ Eksekusi terprogram notebook tidak didukung dengan aplikasi interaktif Amazon EMR Serverless.

## Contoh eksekusi notebook EMR terprogram
<a name="emr-managed-notebooks-headless-examples"></a>

Bagian berikut memberikan beberapa contoh eksekusi notebook EMR terprogram dengan AWS CLI, Boto3 SDK (Python), dan Ruby:
+ [Contoh perintah CLI Notebook di EMR Studio](emr-managed-notebooks-headless-cli.md)
+ [Sampel Python untuk notebook EMR](emr-managed-notebooks-headless-python.md)
+ [Sampel Ruby untuk notebook EMR](emr-managed-notebooks-headless-ruby.md)

Anda juga dapat menjalankan notebook berparameter sebagai bagian dari alur kerja terjadwal dengan alat orkestrasi seperti Apache Airflow atau Amazon Managed Workflows for Apache Airflow (MWAA). *Untuk informasi selengkapnya, lihat [Mengatur pekerjaan analitik di EMR Notebooks menggunakan](https://aws.amazon.com/blogs/big-data/orchestrating-analytics-jobs-on-amazon-emr-notebooks-using-amazon-mwaa/) MWAA di Big Data Blog.AWS *

# Contoh perintah CLI Notebook di EMR Studio
<a name="emr-managed-notebooks-headless-cli"></a>

Topik ini menunjukkan contoh perintah CLI untuk notebook EMR. Contoh menggunakan notebook demo dari konsol EMR Notebooks. Untuk menemukan buku catatan, gunakan jalur file relatif ke direktori home. Dalam contoh ini, ada dua file notebook yang dapat Anda jalankan: `demo_pyspark.ipynb` dan`my_folder/python3.ipynb`. 

**catatan**  
EMR Notebooks tersedia sebagai EMR Studio Workspaces di konsol. Tombol **Create Workspace** di konsol memungkinkan Anda membuat notebook baru. Untuk mengakses atau membuat Ruang Kerja, pengguna EMR Notebooks memerlukan izin peran IAM tambahan. [Untuk informasi selengkapnya, lihat [Amazon EMR Notebook adalah Amazon EMR Studio Workspaces di konsol dan konsol Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-migration.html).](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html)

Jalur relatif untuk file `demo_pyspark.ipynb` adalah`demo_pyspark.ipynb`, ditunjukkan di bawah ini.

![\[Jupyter notebook interface showing a file explorer and code editor with PySpark content.\]](http://docs.aws.amazon.com/id_id/emr/latest/ManagementGuide/images/notebook_exe_folder_structure_1.png)


Jalur relatif untuk `python3.ipynb` adalah`my_folder/python3.ipynb`, ditunjukkan di bawah ini.

![\[File explorer showing python3.ipynb in my_folder, and Jupyter notebook interface with code.\]](http://docs.aws.amazon.com/id_id/emr/latest/ManagementGuide/images/notebook_exe_folder_structure_2.png)


Untuk informasi tentang tindakan Amazon EMR API, lihat `NotebookExecution` tindakan Amazon [EMR](https://docs.aws.amazon.com/emr/latest/APIReference/API_Operations.html) API. .

## Jalankan buku catatan
<a name="emr-managed-notebooks-api-actions"></a>

Anda dapat menggunakan AWS CLI untuk menjalankan notebook Anda dengan `start-notebook-execution` tindakan, seperti contoh berikut menunjukkan. 

**Example — Menjalankan notebook EMR di EMR Studio Workspace dengan klaster Amazon EMR (berjalan di Amazon EC2)**  

```
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 - Menjalankan notebook EMR di Ruang Kerja EMR Studio dengan cluster 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 - Menjalankan notebook EMR yang menentukan lokasi Amazon S3-nya**  

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

## Keluaran notebook
<a name="emr-managed-notebooks-headless-cli-output"></a>

 Berikut adalah output dari notebook sampel. Sel 3 menunjukkan nilai parameter yang baru disuntikkan.

![\[Jupyter notebook cells showing Python code and output for parameter injection and manipulation.\]](http://docs.aws.amazon.com/id_id/emr/latest/ManagementGuide/images/HelloWorld_notebook.png)


## Jelaskan buku catatan
<a name="emr-managed-notebooks-headless-cli-describe"></a>

Anda dapat menggunakan `describe-notebook-execution` tindakan untuk mengakses informasi tentang eksekusi notebook tertentu.

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

## Hentikan buku catatan
<a name="emr-managed-notebooks-headless-cli-stop"></a>

Jika notebook Anda menjalankan eksekusi yang ingin Anda hentikan, Anda dapat melakukannya dengan `stop-notebook-execution` perintah.

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

## Buat daftar eksekusi untuk buku catatan berdasarkan waktu mulai
<a name="emr-managed-notebooks-headless-cli-list"></a>

Anda dapat meneruskan `--from` parameter `list-notebook-executions` ke daftar eksekusi buku catatan Anda berdasarkan waktu mulai.

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

## Buat daftar eksekusi untuk buku catatan berdasarkan waktu mulai dan status
<a name="emr-managed-notebooks-headless-cli-list"></a>

`list-notebook-executions`Perintah juga dapat mengambil `--status` parameter untuk memfilter hasil.

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

# Sampel Python untuk notebook EMR
<a name="emr-managed-notebooks-headless-python"></a>

Topik ini berisi contoh file perintah. Contoh kode adalah file SDK for Python (Boto3) bernama. `demo.py` Ini menunjukkan eksekusi notebook APIs.

**catatan**  
EMR Notebooks tersedia sebagai EMR Studio Workspaces di konsol. Tombol **Create Workspace** di konsol memungkinkan Anda membuat notebook baru. Untuk mengakses atau membuat Ruang Kerja, pengguna EMR Notebooks memerlukan izin peran IAM tambahan. [Untuk informasi selengkapnya, lihat [Amazon EMR Notebook adalah Amazon EMR Studio Workspaces di konsol dan konsol Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-migration.html).](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html)

Untuk informasi tentang tindakan Amazon EMR API, lihat `NotebookExecution` tindakan Amazon [EMR](https://docs.aws.amazon.com/emr/latest/APIReference/API_Operations.html) API.

```
import boto3,time

emr = boto3.client(
    'emr',
    region_name='us-west-1'
)     
     
start_resp = emr.start_notebook_execution(
    EditorId='e-40AC8ZO6EGGCPJ4DLO48KGGGI',
    RelativePath='boto3_demo.ipynb',
    ExecutionEngine={'Id':'j-1HYZS6JQKV11Q'},
    ServiceRole='EMR_Notebooks_DefaultRole'
)

execution_id = start_resp["NotebookExecutionId"]
print(execution_id)
print("\n")
     
describe_response = emr.describe_notebook_execution(NotebookExecutionId=execution_id)
     
print(describe_response)
print("\n")
     
list_response = emr.list_notebook_executions()
print("Existing notebook executions:\n")
for execution in list_response['NotebookExecutions']:
    print(execution)
    print("\n")  
     
print("Sleeping for 5 sec...")
time.sleep(5)
     
print("Stop execution " + execution_id)
emr.stop_notebook_execution(NotebookExecutionId=execution_id)
describe_response = emr.describe_notebook_execution(NotebookExecutionId=execution_id)
print(describe_response)
print("\n")
```

Berikut output dari menjalankan`demo.py`.

```
ex-IZX56YJDW1D29Q1PHR32WABU2SAPK
     
{'NotebookExecution': {'NotebookExecutionId': 'ex-IZX56YJDW1D29Q1PHR32WABU2SAPK', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'ExecutionEngine': {'Id': 'j-1HYZS6JQKV11Q', 'Type': 'EMR'}, 'NotebookExecutionName': '', 'Status': 'STARTING', 'StartTime': datetime.datetime(2020, 8, 19, 0, 49, 19, 418000, tzinfo=tzlocal()), 'Arn': 'arn:aws:elasticmapreduce:us-west-1:123456789012:notebook-execution/ex-IZX56YJDW1D29Q1PHR32WABU2SAPK', 'LastStateChangeReason': 'Execution is starting for cluster j-1HYZS6JQKV11Q.', 'Tags': []}, 'ResponseMetadata': {'RequestId': '70f12c5f-1dda-45b7-adf6-964987d373b7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '70f12c5f-1dda-45b7-adf6-964987d373b7', 'content-type': 'application/x-amz-json-1.1', 'content-length': '448', 'date': 'Wed, 19 Aug 2020 00:49:22 GMT'}, 'RetryAttempts': 0}}
     
Existing notebook executions:
     
{'NotebookExecutionId': 'ex-IZX56YJDW1D29Q1PHR32WABU2SAPK', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'NotebookExecutionName': '', 'Status': 'STARTING', 'StartTime': datetime.datetime(2020, 8, 19, 0, 49, 19, 418000, tzinfo=tzlocal())}
     
     
{'NotebookExecutionId': 'ex-IZX5ABS5PR1E5AHMFYEMX3JJIORRB', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'NotebookExecutionName': '', 'Status': 'RUNNING', 'StartTime': datetime.datetime(2020, 8, 19, 0, 48, 36, 373000, tzinfo=tzlocal())}
     
     
{'NotebookExecutionId': 'ex-IZX5GLVXIU1HNI8BWVW057F6MF4VE', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'NotebookExecutionName': '', 'Status': 'FINISHED', 'StartTime': datetime.datetime(2020, 8, 19, 0, 45, 14, 646000, tzinfo=tzlocal()), 'EndTime': datetime.datetime(2020, 8, 19, 0, 46, 26, 543000, tzinfo=tzlocal())}
     
     
{'NotebookExecutionId': 'ex-IZX5CV8YDUO8JAIWMXN2VH32RUIT1', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'NotebookExecutionName': '', 'Status': 'FINISHED', 'StartTime': datetime.datetime(2020, 8, 19, 0, 43, 5, 807000, tzinfo=tzlocal()), 'EndTime': datetime.datetime(2020, 8, 19, 0, 44, 31, 632000, tzinfo=tzlocal())}
     
     
{'NotebookExecutionId': 'ex-IZX5AS0PPW55CEDEURZ9NSOWSUJZ6', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'NotebookExecutionName': '', 'Status': 'FINISHED', 'StartTime': datetime.datetime(2020, 8, 19, 0, 42, 29, 265000, tzinfo=tzlocal()), 'EndTime': datetime.datetime(2020, 8, 19, 0, 43, 48, 320000, tzinfo=tzlocal())}
     
     
{'NotebookExecutionId': 'ex-IZX57YF5Q53BKWLR4I5QZ14HJ7DRS', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'NotebookExecutionName': '', 'Status': 'FINISHED', 'StartTime': datetime.datetime(2020, 8, 19, 0, 38, 37, 81000, tzinfo=tzlocal()), 'EndTime': datetime.datetime(2020, 8, 19, 0, 40, 39, 646000, tzinfo=tzlocal())}
     
Sleeping for 5 sec...
Stop execution ex-IZX56YJDW1D29Q1PHR32WABU2SAPK
{'NotebookExecution': {'NotebookExecutionId': 'ex-IZX56YJDW1D29Q1PHR32WABU2SAPK', 'EditorId': 'e-40AC8ZO6EGGCPJ4DLO48KGGGI', 'ExecutionEngine': {'Id': 'j-1HYZS6JQKV11Q', 'Type': 'EMR'}, 'NotebookExecutionName': '', 'Status': 'STOPPING', 'StartTime': datetime.datetime(2020, 8, 19, 0, 49, 19, 418000, tzinfo=tzlocal()), 'Arn': 'arn:aws:elasticmapreduce:us-west-1:123456789012:notebook-execution/ex-IZX56YJDW1D29Q1PHR32WABU2SAPK', 'LastStateChangeReason': 'Execution is being stopped for cluster j-1HYZS6JQKV11Q.', 'Tags': []}, 'ResponseMetadata': {'RequestId': '2a77ef73-c1c6-467c-a1d1-7204ab2f6a53', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '2a77ef73-c1c6-467c-a1d1-7204ab2f6a53', 'content-type': 'application/x-amz-json-1.1', 'content-length': '453', 'date': 'Wed, 19 Aug 2020 00:49:30 GMT'}, 'RetryAttempts': 0}}
```

# Sampel Ruby untuk notebook EMR
<a name="emr-managed-notebooks-headless-ruby"></a>

Topik ini berisi contoh Ruby yang menunjukkan fungsionalitas notebook.

**catatan**  
EMR Notebooks tersedia sebagai EMR Studio Workspaces di konsol. Tombol **Create Workspace** di konsol memungkinkan Anda membuat notebook baru. Untuk mengakses atau membuat Ruang Kerja, pengguna EMR Notebooks memerlukan izin peran IAM tambahan. [Untuk informasi selengkapnya, lihat [Amazon EMR Notebook adalah Amazon EMR Studio Workspaces di konsol dan konsol Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-migration.html).](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html)

Contoh kode Ruby berikut menunjukkan menggunakan API eksekusi notebook.

```
# prepare an Amazon EMR client

emr = Aws::EMR::Client.new(
  region: 'us-east-1',
  access_key_id: 'AKIA...JKPKA',
  secret_access_key: 'rLMeu...vU0OLrAC1',
)
```

## Memulai eksekusi notebook dan mendapatkan id eksekusi
<a name="emr-managed-notebooks-headless-ruby-startretrieve"></a>

Dalam contoh ini, editor Amazon S3 dan notebook EMR adalah. `s3://amzn-s3-demo-bucket/notebooks/e-EA8VGAA429FEQTC8HC9ZHWISK/test.ipynb`

Untuk informasi tentang tindakan Amazon EMR API, lihat `NotebookExecution` tindakan Amazon [EMR](https://docs.aws.amazon.com/emr/latest/APIReference/API_Operations.html) API.

```
start_response = emr.start_notebook_execution({
    editor_id: "e-EA8VGAA429FEQTC8HC9ZHWISK",
    relative_path: "test.ipynb",
    
    execution_engine: {id: "j-3U82I95AMALGE"},
    
    service_role: "EMR_Notebooks_DefaultRole",
})


notebook_execution_id = start_resp.notebook_execution_id
```

## Menggambarkan eksekusi notebook dan mencetak detailnya
<a name="emr-managed-notebooks-headless-ruby-describeprint"></a>

```
describe_resp = emr.describe_notebook_execution({
    notebook_execution_id: notebook_execution_id
})
puts describe_resp.notebook_execution
```

Output dari perintah di atas adalah sebagai berikut.

```
{
:notebook_execution_id=>"ex-IZX3VTVZWVWPP27KUB90BZ7V9IEDG", 
:editor_id=>"e-EA8VGAA429FEQTC8HC9ZHWISK",
:execution_engine=>{:id=>"j-3U82I95AMALGE", :type=>"EMR", :master_instance_security_group_id=>nil}, 
:notebook_execution_name=>"", 
:notebook_params=>nil, 
:status=>"STARTING", 
:start_time=>2020-07-23 15:07:07 -0700, 
:end_time=>nil, 
:arn=>"arn:aws:elasticmapreduce:us-east-1:123456789012:notebook-execution/ex-IZX3VTVZWVWPP27KUB90BZ7V9IEDG", 
:output_notebook_uri=>nil, 
:last_state_change_reason=>"Execution is starting for cluster j-3U82I95AMALGE.", :notebook_instance_security_group_id=>nil, 
:tags=>[]
}
```

## Filter notebook
<a name="emr-managed-notebooks-headless-ruby-filters"></a>

```
"EditorId": "e-XXXX",           [Optional]
"From" : "1593400000.000",    [Optional]
"To" :
```

### Menghentikan eksekusi notebook
<a name="emr-managed-notebooks-headless-ruby-stop"></a>

```
stop_resp = emr.stop_notebook_execution({
    notebook_execution_id: notebook_execution_id
})
```