

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

# EMR Notebook 的 Ruby 示例
<a name="emr-managed-notebooks-headless-ruby"></a>

本主题包含一个演示 Notebook 功能的 Ruby 示例。

**注意**  
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)。

以下 Ruby 代码示例演示了如何使用 Notebook 执行 API。

```
# prepare an Amazon EMR client

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

## 启动 Notebook 执行并获取执行 ID
<a name="emr-managed-notebooks-headless-ruby-startretrieve"></a>

在此示例中，Amazon S3 编辑器和 EMR Notebook 是 `s3://amzn-s3-demo-bucket/notebooks/e-EA8VGAA429FEQTC8HC9ZHWISK/test.ipynb`。

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

```
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
```

## 描述 Notebook 执行并打印详细信息
<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
```

上述命令的输出如下。

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

## Notebook 筛选条件
<a name="emr-managed-notebooks-headless-ruby-filters"></a>

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

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

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