

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

# 管理 FHIR 的资源 AWS HealthLake
<a name="managing-fhir-resources"></a>

使用 FHIR R4 RESTful API 交互来管理数据存储中的 FHIR 资源。 HealthLake 以下各节描述了所有可用于 FH HealthLake IR 资源管理的 FHIR R4 RESTful API 交互。有关 HealthLake 数据存储功能及其支持 FHIR 规范的哪些部分的信息，请参见[FHIR R4 能力声明 AWS HealthLake](reference-fhir-capability-statement.md)。

**注意**  
本章中列出的FHIR交互是按照 HL7 FHIR R4医疗保健数据交换标准构建的。由于它们是 HL7 FHIR 服务的代表，因此它们不是通过 AWS CLI 和 AWS SDKs提供的。有关更多信息，请参阅 **FHIR R4 [RESTful API](https://hl7.org/fhir/R4/http.html) 文档中的 RESTful API**。

下表列出了支持的 FHIR R4 交互。 AWS HealthLake有关支持的 FHIR *资源类型*的信息 HealthLake，请参见[资源类型](reference-fhir-resource-types.md)。




**支持的 FHIR R4 交互作用 AWS HealthLake**  
<a name="supported-fhir-interactions"></a>[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/healthlake/latest/devguide/managing-fhir-resources.html)

**Topics**
+ [创建 FHIR 资源](managing-fhir-resources-create.md)
+ [读取 FHIR 资源](managing-fhir-resources-read.md)
+ [阅读 FHIR 资源历史记录](managing-fhir-resources-read-history.md)
+ [更新 FHIR 资源](managing-fhir-resources-update.md)
+ [使用 PATCH 操作修改资源](managing-fhir-resources-patch.md)
+ [捆绑 FHIR 资源](managing-fhir-resources-bundle.md)
+ [删除 FHIR 资源](managing-fhir-resources-delete.md)
+ [等性与并发性](managing-fhir-resources-idempotency.md)

# 创建 FHIR 资源
<a name="managing-fhir-resources-create"></a>

FHIR `create` 交互会在 HealthLake 数据存储中创建新的 FHIR 资源。有关更多信息，请参阅 **FHIR R4 RESTful API** 文档[https://hl7.org/fhir/R4/http.html#create](https://hl7.org/fhir/R4/http.html#create)中的。

**创建 FHIR 资源**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 确定要创建的 FHIR `Resource` 的类型。有关更多信息，请参阅 [资源类型](reference-fhir-resource-types.md)。

1. 使用收集的 HealthLake `region`和值为请求构造一个 URL `datastoreId`。还要包括要创建的 FHIR `Resource` 类型。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   POST https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource
   ```

1. 为请求构建 JSON 正文，指定新资源的 FHIR 数据。出于本过程的目的，我们使用的是 FHIR `Patient` 资源，因此请将文件另存为`create-patient.json`。

   ```
   {
       "resourceType": "Patient",
       "identifier": [
           {
               "system": "urn:oid:1.2.36.146.595.217.0.1",
               "value": "12345"
           }
       ],
       "name": [
           {
               "family": "Silva",
               "given": [
                   "Ana",
                   "Carolina"
               ]
           }
       ],
       "gender": "female",
       "birthDate": "1992-02-10"
   }
   ```

1. 发送 请求。FHIR `create` 交互在 FHIR 授权上使用[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 SMART 的`POST`请求。以下示例 HealthLake 使用 curl 或控制台在中创建 FHIR `Patient` 资源。 HealthLake 要查看整个示例，请滚动到 “复**制**” 按钮。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request POST \
     'https://healthlake.region.amazonaws.com/datastore/datastore-id/r4/Patient' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json' \
     --data @create-patient.json
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------
#### [ AWS Console ]

**注意**  
 HealthLake 控制台仅支持 [AWS SigV4 授权](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html)。

   1. 登录 HealthLake 控制台上的 “[运行查询](https://console.aws.amazon.com/healthlake/home#/crud)” 页面。

   2. 在 **“查询设置”** 部分下，进行以下选择。
   + **数据存储 ID**-选择数据存储 ID 以生成查询字符串。
   + **查询类型**-选择`Create`。
   + **资源类型**-选择要创建的 FHIR [资源类型](reference-fhir-resource-types.md)。
   + **请求正文** — 为请求构建 JSON 正文，指定新资源的 FHIR 数据。

   3. 选择**运行查询**。

------

**为资源创建配置验证级别**  


创建 FHIR 资源时，您可以选择指定 `x-amzn-healthlake-fhir-validation-level` HTTP 标头来配置资源的验证级别。 AWS HealthLake 目前支持以下验证级别：
+ `strict`：根据资源的配置文件元素对资源进行验证，如果不存在配置文件，则根据R4规格进行验证。这是的默认验证级别 AWS HealthLake。
+ `structure-only`：根据 R4 对资源进行验证，忽略任何引用的配置文件。
+ `minimal`：资源经过最低限度的验证，忽略了某些 R4 规则。未通过所需的结构检查的资源 search/analytics 将进行更新，以包括审计警告。

尽管搜索索引需要验证失败，但使用最低验证级别创建的资源仍可能被提取到数据存储中。在这种情况下，将更新资源，以包括专门用于记录上述故障的 Healthlake 扩展程序：

```
{
    "url": "http://healthlake.amazonaws.com/fhir/StructureDefinition/validation-issue",
    "valueString": "{\"resourceType\":\"OperationOutcome\",\"issue\":[{\"severity\":\"error\",\"code\":\"processing\",\"details\":{\"text\":\"FHIR resource in payload failed FHIR validation rules.\"},\"diagnostics\":\"FHIR resource in payload failed FHIR validation rules.\"}]}"
}
```

此外，将包含以下 HTTP 响应标头，其值为 “true”：

```
x-amzn-healthlake-validation-issues : true
```

**注意**  
如果存在这些错误，则根据 R4 规范提取的数据可能无法按预期进行搜索。

# 读取 FHIR 资源
<a name="managing-fhir-resources-read"></a>

FHIR `read` 交互读取 HealthLake数据存储中资源的当前状态。有关更多信息，请参阅 **FHIR R4 RESTful API** 文档[https://hl7.org/fhir/R4/http.html#read](https://hl7.org/fhir/R4/http.html#read)中的。

**要读取 FHIR 资源**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 确定`Resource`要读取的 FHIR 类型并收集相关`id`值。有关更多信息，请参阅 [资源类型](reference-fhir-resource-types.md)。

1. 使用收集到的 HealthLake `region`和值为请求构造一个 URL `datastoreId`。还要包括 FHIR `Resource` 类型及其关联`id`的。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   GET https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource/id
   ```

1. 发送 请求。FHIR `read` 交互在 FHIR 授权上使用[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 SMART 的`GET`请求。以下`curl`示例读取了 FHIR `Patient` 资源的当前状态。 HealthLake要查看整个示例，请滚动到 “复**制**” 按钮。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request GET \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient/id' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json'
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------
#### [ AWS Console ]

   1. 登录 HealthLake 控制台上的 “[运行查询](https://console.aws.amazon.com/healthlake/home#/crud)” 页面。

   2. 在 **“查询设置”** 部分下，进行以下选择。
   + **数据存储 ID**-选择数据存储 ID 以生成查询字符串。
   + **查询类型**-选择`Read`。
   + **资源类型**-选择要读取的 FHIR [资源类型](reference-fhir-resource-types.md)。
   + **资源 ID** — 输入 FHIR 资源 ID。

   3. 选择**运行查询**。

------

# 阅读 FHIR 资源历史记录
<a name="managing-fhir-resources-read-history"></a>

FHIR `history` 交互会检索数据存储中特定 FHIR 资源的历史记录。 HealthLake 使用此交互，您可以确定 FHIR 资源的内容如何随着时间的推移而发生变化。它还可用于与审计日志协调以查看修改前后的资源状态。FHIR 的交互作用`create`和`delete`结果是要保存的资源的历史版本。`update`有关更多信息，请参阅 **FHIR R4 RESTful API** 文档[https://hl7.org/fhir/R4/http.html#history](https://hl7.org/fhir/R4/http.html#history)中的。

**注意**  
您可以选择不使用特定`history`的 FHIR 资源类型。要选择退出，请使用创建案例[AWS Support Center Console](https://console.aws.amazon.com/support/home#/)。要创建您的案例，请登录您的 AWS 账户 并选择**创建案例**。

**阅读 FHIR 资源历史记录**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 确定`Resource`要读取的 FHIR 类型并收集相关`id`值。有关更多信息，请参阅 [资源类型](reference-fhir-resource-types.md)。

1. 使用收集的 HealthLake`region`和值为请求构造一个 URL `datastoreId`。还应包括 FHIR `Resource` 类型、其关联`id`的和可选的搜索参数。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   GET https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource/id/_history{?[parameters]}
   ```  
**HealthLake FHIR `history` 交互支持的搜索参数**    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/healthlake/latest/devguide/managing-fhir-resources-read-history.html)

1. 发送 请求。FHIR `history` 交互在 FHIR 授权上使用[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 SMART 的`GET`请求。以下`curl`示例使用`_count`搜索参数为中的 HealthLake FHIR `Patient` 资源每页返回 100 个历史搜索结果。要查看整个示例，请滚动到 “复**制**” 按钮。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request GET \
     'https://healthlake.region.amazonaws.com/datastore/datastore-id/r4/Patient/id/_history?_count=100' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json'
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------

   `history`交互的返回内容包含在 FHIR 资源中`Bundle`，类型设置为。`history`它包含指定的版本历史记录，按最旧版本排序，并包含已删除的资源。有关更多信息，请参阅 **FHIR R4** 文档[https://hl7.org/fhir/R4/bundle.html](https://hl7.org/fhir/R4/bundle.html)中的。

## 读取特定版本的 FHIR 资源历史记录
<a name="managing-fhir-data-get-version-specific-resource"></a>

FHIR `vread` 交互对数据存储中的资源执行特定版本的读取。 HealthLake 使用此交互，您可以像过去特定时间一样查看 FHIR 资源的内容。

**注意**  
如果您使用 FHIR `history` 交互*而不*使用`vread`，则 HealthLake 始终返回资源元数据的最新版本。

HealthLake 声明它支持对每个支持的资源[https://hl7.org/fhir/R4/capabilitystatement-definitions.html#CapabilityStatement.rest.resource.versioning](https://hl7.org/fhir/R4/capabilitystatement-definitions.html#CapabilityStatement.rest.resource.versioning)进行版本控制。所有资源上的所有 HealthLake 数据存储都包含 `Resource.meta.versionId` (`vid`)。

启用 FHIR `history` 交互时（默认情况下，对于在 2024 年 10 月 25 日之后创建的数据存储或通过请求较旧的数据存储创建的数据存储），`Bundle`响应会将`vid`作为元素的一部分包括在内。[https://hl7.org/fhir/R4/bundle-definitions.html#Bundle.entry.response.location](https://hl7.org/fhir/R4/bundle-definitions.html#Bundle.entry.response.location)在以下示例中，`vid`显示为数字`1`。要查看完整示例，请参阅[示例捆绑包/捆绑包响应 (JSON](https://build.fhir.org/bundle-response.json.html))。

```
"response" : {
    "status" : "201 Created",
    "location" : "Patient/12423/_history/1",
    ...}
```

**阅读特定版本的 FHIR 资源历史记录**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 确定要读取和收集关联`id`的 and `vid` 值的 FHIR `Resource` 类型。有关更多信息，请参阅 [资源类型](reference-fhir-resource-types.md)。

1. 使用为 HealthLake 和 FHIR 收集的值为请求构造一个 URL。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   GET https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource/id/_history/vid
   ```

1. 发送 请求。FHIR `history` 交互在 FHIR 授权上使用[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 SMART 的`GET`请求。以下`vread`交互返回单个实例，其中包含为 FHIR `Patient` 资源指定的内容，该版本由指定的资源元数据。`vid`要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request GET \
     'https://healthlake.region.amazonaws.com/datastore/datastore-id/r4/Patient/id/_history/vid' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json'
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------

# 更新 FHIR 资源
<a name="managing-fhir-resources-update"></a>

FHIR `update` 交互为现有资源创建新的当前版本，如果给定`id`资源尚不存在任何资源，则创建初始版本。有关更多信息，请参阅 **FHIR R4 RESTful API** 文档[https://hl7.org/fhir/R4/http.html#update](https://hl7.org/fhir/R4/http.html#update)中的。

**更新 FHIR 资源**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 确定`Resource`要更新的 FHIR 类型并收集关联`id`值。有关更多信息，请参阅 [资源类型](reference-fhir-resource-types.md)。

1. 使用收集到的 HealthLake `region`和值为请求构造一个 URL `datastoreId`。还要包括 FHIR `Resource` 类型及其关联`id`的。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   PUT https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource/id
   ```

1. 为请求构造`JSON`正文，指定要进行的 FHIR 数据更新。出于本过程的目的，请将文件另存为`update-patient.json`。

   ```
   {
       "id": "2de04858-ba65-44c1-8af1-f2fe69a977d9",
       "resourceType": "Patient",
       "active": true,
       "name": [
           {
               "use": "official",
               "family": "Doe",
               "given": [
                   "Jane"
               ]
           },
           {
               "use": "usual",
               "given": [
                   "Jane"
               ]
           }
       ],
       "gender": "female",
       "birthDate": "1985-12-31"
   }
   ```

1. 发送 请求。FHIR `update` 交互在 FHIR 授权上使用[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 SMART 的`PUT`请求。以下`curl`示例更新了中的`Patient`资源 HealthLake。要查看整个示例，请滚动到 “复**制**” 按钮。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request PUT \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient/id' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json' \
     --data @update-patient.json
   ```

   如果*更新*了现有资源，则您的请求将返回 `200` HTTP 状态码；如果创建了新资源，则将返回 `201` HTTP 状态码。

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------
#### [ AWS Console ]

   1. 登录 HealthLake 控制台上的 “[运行查询](https://console.aws.amazon.com/healthlake/home#/crud)” 页面。

   2. 在 **“查询设置”** 部分下，进行以下选择。
   + **数据存储 ID**-选择数据存储 ID 以生成查询字符串。
   + **查询类型**-选择`Update (PUT)`。
   + **资源类型**-选择要更新或创建的 FHIR [资源类型](reference-fhir-resource-types.md)。
   + **请求正文** — 为请求构建 JSON 正文，指定用于更新资源的 FHIR 数据。

   3. 选择**运行查询**。

------

## 根据条件更新 FHIR 资源
<a name="managing-fhir-resources-update-conditional"></a>

条件更新允许您根据某些标识搜索条件而不是逻辑 FHIR `id` 更新现有资源。当服务器处理更新时，它会使用其标准搜索功能对资源类型执行搜索，目标是解析请求`id`的单个逻辑。

服务器采取的操作取决于它找到的匹配项数量：
+ **没有匹配项，请求正文中未`id`提供**匹配项：服务器创建 FHIR 资源。
+ **未`id`提供匹配项，且资源尚不存在 `id`**：服务器将交互视为 “更新即创建” 交互。
+ **没有匹配项，已`id`提供且已存在**：服务器以`409 Conflict`错误拒绝更新。
+ **One Match，未提供任何资源 OR（`id`提供的资源并且它与找到的资源相匹配）**：服务器对匹配的资源执行更新，如上所述，如果资源已更新，则服务器应返回 a `200 OK`。`id`
+ **One Match，已`id`提供资源但与找到的资源不匹配**：服务器返回`409 Conflict`错误，表明客户端 ID 规范有问题，最好是 `OperationOutcome`
+ **多个匹配项**：服务器返回一个`412 Precondition Failed`错误，表明客户端的标准选择性不够好，最好是 OperationOutcome

以下示例更新了一个名为 peter、出生日期为 2000 年 1 月 1 日、电话号码为 1234567890 的`Patient`资源。

```
PUT https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient?name=peter&birthdate=2000-01-01&phone=1234567890
```

## 为资源更新配置验证级别
<a name="validation-level-resource-updates"></a>

更新 FHIR 资源时，您可以选择指定 `x-amzn-healthlake-fhir-validation-level` HTTP 标头来配置资源的验证级别。 AWS HealthLake 目前支持以下验证级别：
+ `strict`：根据资源的配置文件元素对资源进行验证，如果不存在配置文件，则根据R4规格进行验证。这是的默认验证级别 AWS HealthLake。
+ `structure-only`：根据 R4 对资源进行验证，忽略任何引用的配置文件。
+ `minimal`：资源经过最低限度的验证，忽略了某些 R4 规则。未通过所需的结构检查的资源 search/analytics 将进行更新，以包括审计警告。

尽管搜索索引需要验证失败，但使用最低验证级别更新的资源仍可能会被提取到数据存储中。在这种情况下，将更新资源，以包括专门用于记录上述故障的 Healthlake 扩展程序：

```
{
    "url": "http://healthlake.amazonaws.com/fhir/StructureDefinition/validation-issue",
    "valueString": "{\"resourceType\":\"OperationOutcome\",\"issue\":[{\"severity\":\"error\",\"code\":\"processing\",\"details\":{\"text\":\"FHIR resource in payload failed FHIR validation rules.\"},\"diagnostics\":\"FHIR resource in payload failed FHIR validation rules.\"}]}"
}
```

此外，将包含以下 HTTP 响应标头，其值为 “true”：

```
x-amzn-healthlake-validation-issues : true
```

**注意**  
请注意，如果存在这些错误，则根据 R4 规范提取的数据可能无法按预期进行搜索。

# 使用 PATCH 操作修改资源
<a name="managing-fhir-resources-patch"></a>

AWS HealthLake 支持 FHIR 资源的 PATCH 操作，使您能够通过将特定元素定位为添加、替换或删除来修改资源，而无需更新整个资源。当您需要执行以下操作时，此操作特别有用：
+ 对大型资源进行有针对性的更新
+ 减少网络带宽使用量
+ 对特定资源元素进行原子修改
+ 最大限度地降低覆盖并发更改的风险
+ 作为批处理和事务工作流程的一部分更新资源

## 支持的补丁格式
<a name="patch-supported-formats"></a>

AWS HealthLake 支持两种标准补丁格式：

### JSON 补丁 (RFC 6902)
<a name="patch-format-json"></a>

使用 JSON 指针语法按元素在资源结构中的位置来定位元素。

**Content-Type:** `application/json-patch+json`

### FHIRPath 补丁（FHIR R4 规范）
<a name="patch-format-fhirpath"></a>

使用 FHIRPath 表达式根据元素的内容和关系来定位元素，从而提供了 FHIR 原生的修补方法。

**Content-Type:** `application/fhir+json`

## 用法
<a name="patch-usage"></a>

### 直接补丁操作
<a name="patch-usage-direct"></a>

可以使用 PATCH HTTP 方法直接在 FHIR 资源上调用 PATCH 操作：

```
PATCH [base]/[resource-type]/[id]{?_format=[mime-type]}
```

### 捆绑包中的补丁
<a name="patch-usage-bundles"></a>

补丁操作可以作为类型为 FHIR Bundle 的条目包含在内`transaction`，`batch`也可以在单个请求中将补丁操作与其他 FHIR 交互（创建、读取、更新、删除）结合起来。
+ **交易捆绑包**：所有条目都以原子方式成功或失败
+ **Batch 捆绑包**：每个参赛作品均独立处理

## JSON 补丁格式
<a name="patch-json-format"></a>

### 支持的操作
<a name="patch-json-supported-operations"></a>


| 操作 | 说明 | 
| --- | --- | 
| add | 为资源添加新值 | 
| remove | 从资源中移除一个值 | 
| replace | 替换资源中的现有值 | 
| move | 从一个位置移除一个值并将其添加到另一个位置 | 
| copy | 将值从一个位置复制到另一个位置 | 
| test | 测试目标位置的值是否等于指定值 | 

### 路径语法
<a name="patch-json-path-syntax"></a>

JSON 补丁使用 JSON 指针语法 (RFC 6901)：


| 路径示例 | 说明 | 
| --- | --- | 
| /name/0/family | 名字的家族元素 | 
| /telecom/- | 追加到电信阵列 | 
| /active | 根级活动元素 | 
| /address/0/line/1 | 第一个地址的第二行 | 

### 示例
<a name="patch-json-examples"></a>

**包含多个操作的直接 JSON 补丁请求**  


```
PATCH [base]/Patient/example
Content-Type: application/json-patch+json
If-Match: W/"1"

[
  {
    "op": "replace",
    "path": "/name/0/family",
    "value": "Smith"
  },
  {
    "op": "add",
    "path": "/telecom/-",
    "value": {
      "system": "phone",
      "value": "555-555-5555",
      "use": "home"
    }
  },
  {
    "op": "remove",
    "path": "/address/0"
  },
  {
    "op": "move",
    "from": "/name/0/family",
    "path": "/name/1/family"
  },
  {
    "op": "test",
    "path": "/gender",
    "value": "male"
  },
  {
    "op": "copy",
    "from": "/name/0",
    "path": "/name/1"
  }
]
```

**通过单一操作直接请求 JSON 补丁**  


```
PATCH [base]/Patient/example
Content-Type: application/json-patch+json

[
  {
    "op": "replace",
    "path": "/active",
    "value": false
  }
]
```

**套装中的 JSON 补丁**  
使用包含 base64 编码的 JSON 补丁负载的二进制资源：

```
{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [{
    "resource": {
      "resourceType": "Binary",
      "contentType": "application/json-patch+json",
      "data": "W3sib3AiOiJhZGQiLCJwYXRoIjoiL2JpcnRoRGF0ZSIsInZhbHVlIjoiMTk5MC0wMS0wMSJ9XQ=="
    },
    "request": {
      "method": "PATCH",
      "url": "Patient/123"
    }
  }]
}
```

## FHIRPath 补丁格式
<a name="patch-fhirpath-format"></a>

### 支持的操作
<a name="patch-fhirpath-supported-operations"></a>


| 操作 | 说明 | 
| --- | --- | 
| add | 向资源添加新元素 | 
| insert | 在列表的特定位置插入一个元素 | 
| delete | 从资源中移除元素 | 
| replace | 替换现有元素的值 | 
| move | 对列表中的元素重新排序 | 

### 路径语法
<a name="patch-fhirpath-path-syntax"></a>

FHIRPath 补丁使用 FHIRPath 表达式，支持：
+ **基于索引的访问**：`Patient.name[0]`
+ 使用以下@@ **方式进行筛选 `where()`**：`Patient.name.where(use = 'official')`
+ **布尔逻辑**：`Patient.telecom.where(system = 'phone' and use = 'work')`
+ **子集设置函数**：`first()`，`last()`
+ **存在性检查**：`exists()`，`count()`
+ **多态导航**：`Observation.value`

### 示例
<a name="patch-fhirpath-examples"></a>

**直接申请 FHIRPath 补丁**  


```
PATCH [base]/Patient/123
Content-Type: application/fhir+json
Authorization: ...

{
  "resourceType": "Parameters",
  "parameter": [{
    "name": "operation",
    "part": [
      { "name": "type", "valueCode": "add" },
      { "name": "path", "valueString": "Patient" },
      { "name": "name", "valueString": "birthDate" },
      { "name": "value", "valueDate": "1990-01-01" }
    ]
  }]
}
```

**FHIRPath 捆绑包中的补丁**  
使用带有`method: PATCH`以下内容的参数资源作为入口资源：

```
{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [{
    "resource": {
      "resourceType": "Parameters",
      "parameter": [{
        "name": "operation",
        "part": [
          { "name": "type", "valueCode": "add" },
          { "name": "path", "valueString": "Patient" },
          { "name": "name", "valueString": "birthDate" },
          { "name": "value", "valueDate": "1990-01-01" }
        ]
      }]
    },
    "request": {
      "method": "PATCH",
      "url": "Patient/123"
    }
  }]
}
```

## 请求标头
<a name="patch-request-headers"></a>


| 标题 | 必需 | 描述 | 
| --- | --- | --- | 
| Content-Type | 是 | application/json-patch\$1json用于 JSON 补丁或application/fhir\$1json FHIRPath 补丁 | 
| If-Match | 否 | 使用特定版本的条件更新 ETag | 

## 示例响应
<a name="patch-sample-response"></a>

该操作将返回包含新版本信息的更新资源：

```
HTTP/1.1 200 OK
Content-Type: application/fhir+json
ETag: W/"2"
Last-Modified: Mon, 05 May 2025 10:10:10 GMT

{
  "resourceType": "Patient",
  "id": "example",
  "active": true,
  "name": [
    {
      "family": "Smith",
      "given": ["John"]
    }
  ],
  "telecom": [
    {
      "system": "phone",
      "value": "555-555-5555",
      "use": "home"
    }
  ],
  "meta": {
    "versionId": "2",
    "lastUpdated": "2025-05-05T10:10:10Z"
  }
}
```

## 行为
<a name="patch-behavior"></a>

补丁操作：
+ 根据相应的规范验证补丁语法（JSON 补丁为 RFC 6902，补丁为 FHIR R4） FHIRPath 
+ 以原子方式应用操作-所有操作成功或全部失败
+ 更新资源版本 ID 并创建新的历史记录条目
+ 在应用更改之前，将原始资源保留在历史记录中
+ 在应用补丁后验证 FHIR 资源限制
+ 支持使用带有 If-Match 标头的条件更新 ETag

## 错误处理
<a name="patch-error-handling"></a>

该操作处理以下错误情况：
+ **400 错误请求**：补丁语法无效（请求不符合要求或补丁文档格式错误）
+ **404 未找到**：未找到资源（指定的 ID 不存在）
+ **409 冲突**：版本冲突（并发更新或提供了非当前版本 ID）
+ **422 无法处理的实体**：补丁操作无法应用于指定的资源元素

## 功能摘要
<a name="patch-summary-of-capabilities"></a>


| 能力 | json 补丁 | FHIRPath 补丁 | 
| --- | --- | --- | 
| 内容类型 | application/json-patch\$1json | application/fhir\$1json | 
| 路径格式 | JSON Pointer (RFC 6901) | FHIRPath 表达式 | 
| 直接补丁 API | 支持 | 支持 | 
| 捆绑包 Batch | 支持（通过二进制） | 支持（通过参数） | 
| 捆绑交易 | 支持（通过二进制） | 支持（通过参数） | 
| 操作 | 添加、删除、替换、移动、复制、测试 | 添加、插入、删除、替换、移动 | 

## 限制
<a name="patch-limitations"></a>
+ 不支持使用搜索条件的条件补丁操作
+ 捆绑包中的 JSON 补丁必须使用带有 base64 编码内容的二进制资源
+ FHIRPath 捆绑包中的补丁必须使用参数资源

## 其他资源
<a name="patch-additional-resources"></a>

有关 PATCH 操作的更多信息，请参阅：
+ [FHIR R4 补丁文档](https://hl7.org/fhir/http.html#patch)
+ [FHIR R4 补丁规范 FHIRPath ](https://hl7.org/fhir/fhirpatch.html)
+ [RFC 6902-JSON 补丁](https://datatracker.ietf.org/doc/html/rfc6902#section-4)
+ [RFC 6901-JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901)

# 捆绑 FHIR 资源
<a name="managing-fhir-resources-bundle"></a>

FHIR `Bundle` 是存放 FHIR 资源集合的容器。 AWS HealthLake AWS HealthLake 支持两种具有不同处理行为的捆绑包。

[https://hl7.org/fhir/R4/http.html#transaction](https://hl7.org/fhir/R4/http.html#transaction)捆绑包独立处理每种资源。如果一个资源出现故障，其余资源仍然可以成功。每个操作都是单独处理的，即使某些操作失败，处理也会继续进行。在可以接受部分成功的情况下，使用批量捆绑包进行批量操作，例如上传多份不相关的患者记录。

[https://hl7.org/fhir/R4/http.html#transaction](https://hl7.org/fhir/R4/http.html#transaction)bundle 以原子方式将所有资源作为一个单元处理。要么所有资源操作都成功，要么都不 AWS HealthLake 提交。当您需要保证相关资源的参照完整性时，请使用交易捆绑包，例如创建具有相关观察结果和病症的患者，其中必须将所有数据记录在一起。


**批量捆绑包和交易捆绑包之间的区别**  

| 功能 | Batch | 事务 | 
| --- | --- | --- | 
| 处理模型 | 每个操作的成功或失败都是独立的。 | 所有操作都以单个原子单元的形式成功或失败。 | 
| 故障处理 | 即使个别操作失败，处理也会继续。 | 如果任何一个操作失败，则整个捆绑包将失败。 | 
| 执行顺序 | 不能保证执行顺序。 | 操作按指定的顺序处理。 | 
| 参照完整性 | 未在所有操作中强制执行。 | 对捆绑包中本地引用的资源强制执行。 | 
| 最适合用于 | 可以接受部分成功的批量操作。 | 必须一起创建或更新的相关资源。 | 

您可以捆绑相同或不同类型的 FHIR 资源，它们可以混合使用 FHIR 操作，例如、`create`、`read``update``delete`、和。`patch`有关更多信息，请参阅 **FHIR R4 文档**中的[资源包](https://hl7.org/fhir/R4/Bundle)。

以下是每种捆绑包类型的示例用例。

Batch 捆绑包  
+ 在夜间数据同步期间，上传来自不同设施的多份无关患者记录。
+ 批量上传历史用药记录，其中一些记录可能存在验证问题。
+ 加载参考数据，例如组织和从业人员，其中个人失败不会影响其他条目。

交易捆绑包  
+ 在急诊室住院期间，创建具有相关观察结果和状况的患者，所有数据都必须一起记录。
+ 更新患者的药物清单和相关的过敏信息，这些信息必须保持一致。
+ 将与患者的完整相遇、观察结果、程序和账单信息记录为单个原子单元。

**重要**  
批处理和交易捆绑包使用相同的`Bundle`资源结构。唯一的区别是`type`字段的值。

以下示例显示了具有多种资源类型和操作的事务捆绑包。

```
{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [
    {
      "fullUrl": "urn:uuid:4f6a30fb-cd3c-4ab6-8757-532101f72065",
      "resource": {
        "resourceType": "Patient",
        "id": "new-patient",
        "active": true,
        "name": [
          {
            "family": "Johnson",
            "given": [
              "Sarah"
            ]
          }
        ],
        "gender": "female",
        "birthDate": "1985-08-12",
        "telecom": [
          {
            "system": "phone",
            "value": "555-123-4567",
            "use": "home"
          }
        ]
      },
      "request": {
        "method": "POST",
        "url": "Patient"
      }
    },
    {
      "fullUrl": "urn:uuid:7f83f473-d8cc-4a8d-86d3-9d9876a3248b",
      "resource": {
        "resourceType": "Observation",
        "id": "blood-pressure",
        "status": "final",
        "code": {
          "coding": [
            {
              "system": "http://loinc.org",
              "code": "85354-9",
              "display": "Blood pressure panel"
            }
          ],
          "text": "Blood pressure panel"
        },
        "subject": {
          "reference": "urn:uuid:4f6a30fb-cd3c-4ab6-8757-532101f72065"
        },
        "effectiveDateTime": "2023-10-15T09:30:00Z",
        "component": [
          {
            "code": {
              "coding": [
                {
                  "system": "http://loinc.org",
                  "code": "8480-6",
                  "display": "Systolic blood pressure"
                }
              ]
            },
            "valueQuantity": {
              "value": 120,
              "unit": "mmHg",
              "system": "http://unitsofmeasure.org",
              "code": "mm[Hg]"
            }
          },
          {
            "code": {
              "coding": [
                {
                  "system": "http://loinc.org",
                  "code": "8462-4",
                  "display": "Diastolic blood pressure"
                }
              ]
            },
            "valueQuantity": {
              "value": 80,
              "unit": "mmHg",
              "system": "http://unitsofmeasure.org",
              "code": "mm[Hg]"
            }
          }
        ]
      },
      "request": {
        "method": "POST",
        "url": "Observation"
      }
    },
    {
      "resource": {
        "resourceType": "Appointment",
        "id": "appointment-123",
        "status": "booked",
        "description": "Annual physical examination",
        "start": "2023-11-15T09:00:00Z",
        "end": "2023-11-15T09:30:00Z",
        "participant": [
          {
            "actor": {
              "reference": "urn:uuid:4f6a30fb-cd3c-4ab6-8757-532101f72065"
            },
            "status": "accepted"
          }
        ]
      },
      "request": {
        "method": "PUT",
        "url": "Appointment/appointment-123"
      }
    },
    {
      "request": {
        "method": "DELETE",
        "url": "MedicationRequest/med-request-456"
      }
    }
  ]
}
```

## 将 FHIR 资源捆绑为独立实体
<a name="bundle-fhir-resources-batch-type"></a>

**将 FHIR 资源捆绑为独立实体**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 使用收集到的 HealthLake`region`和值为请求构造一个 URL `datastoreId`。请*勿*在 URL 中指定 FHIR 资源类型。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   POST https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/
   ```

1. 为请求构造一个 JSON 正文，将每个 HTTP 动词指定为`method`元素的一部分。以下示例使用与`Bundle`资源的`batch`类型交互来创建新的`Patient`和`Medication`资源。所有必填部分都有相应的注释。出于本过程的目的，请将文件另存为`batch-independent.json`。

   ```
   {
       "resourceType": "Bundle",
       "id": "bundle-batch",
       "meta": {
           "lastUpdated": "2014-08-18T01:43:30Z"
       },
       "type": "batch",
       "entry": [
           {
               "resource": {
                   "resourceType": "Patient",
                   "meta": {
                       "lastUpdated": "2022-06-03T17:53:36.724Z"
                   },
                   "text": {
                       "status": "generated",
                       "div": "Some narrative"
                   },
                   "active": true,
                   "name": [
                       {
                           "use": "official",
                           "family": "Jackson",
                           "given": [
                               "Mateo",
                               "James"
                           ]
                       }
                   ],
                   "gender": "male",
                   "birthDate": "1974-12-25"
               },
               "request": {
                   "method": "POST",
                   "url": "Patient"
               }
           },
           {
               "resource": {
                   "resourceType": "Medication",
                   "id": "med0310",
                   "contained": [
                       {
                           "resourceType": "Substance",
                           "id": "sub03",
                           "code": {
                               "coding": [
                                   {
                                       "system": "http://snomed.info/sct",
                                       "code": "55452001",
                                       "display": "Oxycodone (substance)"
                                   }
                               ]
                           }
                       }
                   ],
                   "code": {
                       "coding": [
                           {
                               "system": "http://snomed.info/sct",
                               "code": "430127000",
                               "display": "Oral Form Oxycodone (product)"
                           }
                       ]
                   },
                   "form": {
                       "coding": [
                           {
                               "system": "http://snomed.info/sct",
                               "code": "385055001",
                               "display": "Tablet dose form (qualifier value)"
                           }
                       ]
                   },
                   "ingredient": [
                       {
                           "itemReference": {
                               "reference": "#sub03"
                           },
                           "strength": {
                               "numerator": {
                                   "value": 5,
                                   "system": "http://unitsofmeasure.org",
                                   "code": "mg"
                               },
                               "denominator": {
                                   "value": 1,
                                   "system": "http://terminology.hl7.org/CodeSystem/v3-orderableDrugForm",
                                   "code": "TAB"
                               }
                           }
                       }
                   ]
               },
               "request": {
                   "method": "POST",
                   "url": "Medication"
               }
           }
       ]
   }
   ```

1. 发送 请求。FHIR `Bundle` 批处理类型使用具有[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 FHIR 授权的 SMART `POST` 请求。以下代码示例使用`curl`命令行工具进行演示。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request POST \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json' \
     --data @batch-type.json
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------

   服务器返回一个响应，其中显示`Patient`了作为`Bundle`批处理类型请求的结果而创建的和`Medication`资源。

## 以捆绑包 PUTs 为条件
<a name="bundle-conditional-PUTs"></a>

AWS HealthLake 支持使用以下查询参数在捆绑包中进行条件更新：
+ `_id`（独立）
+ `_id`与以下选项之一相结合：
  + `_tag`
  + `_createdAt`
  + `_lastUpdated`

当您在捆绑包 PUTs 中使用条件时，会根据现有资源 AWS HealthLake 评估查询参数，并根据匹配结果采取行动。


**有条件的更新行为**  

| 场景 | HTTP 状态 | 已采取的行动 | 
| --- | --- | --- | 
| 提供没有 ID 的资源 | 201 已创建 | 始终创建新资源。 | 
| 具有新 ID 的资源（不匹配） | 201 已创建 | 使用指定 ID 创建新资源。 | 
| 具有现有 ID 的资源（单一匹配） | 200 OK (200 确定) | 更新匹配的资源。 | 
| 具有现有 ID 的资源（检测到冲突） | 409 冲突 | 返回错误。未进行任何更改。 | 
| 具有现有 ID 的资源（ID 不匹配） | 400 错误请求 | 返回错误。未进行任何更改。 | 
| 多个资源匹配条件 | 412 前提条件失败 | 返回错误。未进行任何更改。 | 

在以下带有条件更新的捆绑包示例中，带有 FHIR ID 的`Patient`资源仅在满足条件`_lastUpdated=lt2025-04-20`时才会`476`更新。

```
{
    "resourceType": "Bundle",
    "id": "bundle-batch",
    "meta": {
        "lastUpdated": "2014-08-18T01:43:30Z"
    },
    "type": "batch",
    "entry": [
        {
            "resource": {
                "resourceType": "Patient",
                "id": "476",
                "meta": {
                    "lastUpdated": "2022-06-03T17:53:36.724Z"
                },
                "active": true,
                "name": [
                    {
                        "use": "official",
                        "family": "Jackson",
                        "given": [
                            "Mateo",
                            "James"
                        ]
                    }
                ],
                "gender": "male",
                "birthDate": "1974-12-25"
            },
            "request": {
                "method": "PUT",
                "url": "Patient?_id=476&_lastUpdated=lt2025-04-20"
            }
        },
        {
            "resource": {
                "resourceType": "Medication",
                "id": "med0310",
                "contained": [
                    {
                        "resourceType": "Substance",
                        "id": "sub03",
                        "code": {
                            "coding": [
                                {
                                    "system": "http://snomed.info/sct",
                                    "code": "55452001",
                                    "display": "Oxycodone (substance)"
                                }
                            ]
                        }
                    }
                ],
                "code": {
                    "coding": [
                        {
                            "system": "http://snomed.info/sct",
                            "code": "430127000",
                            "display": "Oral Form Oxycodone (product)"
                        }
                    ]
                },
                "form": {
                    "coding": [
                        {
                            "system": "http://snomed.info/sct",
                            "code": "385055001",
                            "display": "Tablet dose form (qualifier value)"
                        }
                    ]
                },
                "ingredient": [
                    {
                        "itemReference": {
                            "reference": "#sub03"
                        },
                        "strength": {
                            "numerator": {
                                "value": 5,
                                "system": "http://unitsofmeasure.org",
                                "code": "mg"
                            },
                            "denominator": {
                                "value": 1,
                                "system": "http://terminology.hl7.org/CodeSystem/v3-orderableDrugForm",
                                "code": "TAB"
                            }
                        }
                    }
                ]
            },
            "request": {
                "method": "POST",
                "url": "Medication"
            }
        }
    ]
}
```

## 将 FHIR 资源捆绑为一个实体
<a name="bundle-fhir-resources-document-type"></a>

**将 FHIR 资源捆绑为单个实体**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 使用收集到的 HealthLake`region`和值为请求构造一个 URL `datastoreId`。将 FHIR 资源类型`Bundle`作为网址的一部分。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   POST https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Bundle
   ```

1. 为请求构建 JSON 正文，指定要组合在一起的 FHIR 资源。以下示例将两个`Patient`资源分组在一起 HealthLake。出于本过程的目的，请将文件另存为`batch-single.json`。

   ```
   {
       "resourceType": "Bundle",
       "id": "bundle-minimal",
       "language": "en-US",
       "identifier": {
           "system": "urn:oid:1.2.3.4.5",
           "value": "28b95815-76ce-457b-b7ae-a972e527db4f"
       },
       "type": "document",
       "timestamp": "2020-12-11T14:30:00+01:00",
       "entry": [
           {
               "fullUrl": "urn:uuid:f40b07e3-37e8-48c3-bf1c-ae70fe12dabf",
               "resource": {
                   "resourceType": "Composition",
                   "id": "f40b07e3-37e8-48c3-bf1c-ae70fe12dabf",
                   "status": "final",
                   "type": {
                       "coding": [
                           {
                               "system": "http://loinc.org",
                               "code": "60591-5",
                               "display": "Patient summary Document"
                           }
                       ]
                   },
                   "date": "2020-12-11T14:30:00+01:00",
                   "author": [
                       {
                           "reference": "urn:uuid:45271f7f-63ab-4946-970f-3daaaa0663ff"
                       }
                   ],
                   "title": "Patient Summary as of December 7, 2020 14:30"
               }
           },
           {
               "fullUrl": "urn:uuid:45271f7f-63ab-4946-970f-3daaaa0663ff",
               "resource": {
                   "resourceType": "Practitioner",
                   "id": "45271f7f-63ab-4946-970f-3daaaa0663ff",
                   
                   "active": true,
                   "name": [
                       {
                           "family": "Doe",
                           "given": [
                               "John"
                           ]
                       }
                   ]                
               }
           }
       ]
   }
   ```

1. 发送 请求。FHIR `Bundle` 文档类型使用带有[AWS 签名版本 4 签名](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html)协议的`POST`请求。以下代码示例使用`curl`命令行工具进行演示。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request POST \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Bundle' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json' \
     --data @document-type.json
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------

   服务器返回一个响应，其中显示了由于`Bundle`文档类型请求而创建的两个`Patient`资源。

## 为捆绑包配置验证级别
<a name="validation-level-bundles"></a>

捆绑 FHIR 资源时，您可以选择指定 `x-amzn-healthlake-fhir-validation-level` HTTP 标头来配置资源的验证级别。将为捆绑包中的所有创建和更新请求设置此验证级别。 AWS HealthLake 目前支持以下验证级别：
+ `strict`：根据资源的配置文件元素对资源进行验证，如果不存在配置文件，则根据R4规格进行验证。这是的默认验证级别 AWS HealthLake。
+ `structure-only`：根据 R4 对资源进行验证，忽略任何引用的配置文件。
+ `minimal`：资源经过最低限度的验证，忽略了某些 R4 规则。未通过所需的结构检查的资源 search/analytics 将进行更新，以包括审计警告。

尽管搜索索引需要验证失败，但与最低验证级别捆绑在一起的资源仍可能会被提取到数据存储中。在这种情况下，资源将更新为包含用于记录上述故障的 Healthlake 专用扩展程序，Bundle 响应中的条目将包括以下 OperationOutcome 资源：

```
{
    "resourceType": "Bundle",
    "type": "batch-response",
    "timestamp": "2025-08-25T22:58:48.846287342Z",
    "entry": [
        {
            "response": {
                "status": "201",
                "location": "Patient/195abc49-ba8e-4c8b-95c2-abc88fef7544/_history/1",
                "etag": "W/\"1\"",
                "lastModified": "2025-08-25T22:58:48.801245445Z",
                "outcome": {
                    "resourceType": "OperationOutcome",
                    "issue": [
                        {
                            "severity": "error",
                            "code": "processing",
                            "details": {
                                "text": "FHIR resource in payload failed FHIR validation rules."
                            },
                            "diagnostics": "FHIR resource in payload failed FHIR validation rules."
                        }
                    ]
                }
            }
        }
    ]
}
```

此外，将包含以下 HTTP 响应标头，其值为 “true”：

```
x-amzn-healthlake-validation-issues : true
```

**注意**  
请注意，如果存在这些错误，则根据 R4 规范提取的数据可能无法按预期进行搜索。

## 对捆绑包类型 “消息” 的支持有限
<a name="bundle-message-type-limited-support"></a>

HealthLake `message`通过内部转换过程对 FHIR 捆绑包类型提供有限的支持。此支持专为无法在源头重新格式化消息包的场景而设计，例如从传统医院系统中摄取 ADT（入院、出院、转移）提要。

**警告**  
此功能需要明确的 AWS 账户许可名单，并且不强制执行 FHIR R4 消息语义或引用完整性。在使用消息包之前，请联系 Su AWS pport 申请启用您的账户。

### 与标准邮件处理的主要区别
<a name="bundle-message-key-differences"></a>
+ **消息包**（FHIR 规范）：第一个条目必须是引用其他`MessageHeader`资源的条目。资源缺少单个`request`对象， MessageHeader 事件决定处理操作。
+ **HealthLake 处理**：通过自动为每个资源条目分配 PUT 操作，将消息包转换为批量捆绑包。资源是独立处理的，不强制执行消息语义或引用完整性。

### 重要限制
<a name="bundle-message-limitations"></a>
+ 未强制执行 FHIR R4 邮件特定的处理规则
+ 资源之间没有交易完整性
+ 资源间引用未经过验证
+ 需要明确的账户许可名单

### 消息包结构示例
<a name="bundle-message-example"></a>

```
{
              "resourceType": "Bundle",
              "type": "message",
              "entry": [
                {
                  "resource": {
                    "resourceType": "MessageHeader",
                    "eventCoding": {
                      "system": "http://hl7.org/fhir/us/davinci-alerts/CodeSystem/notification-event",
                      "code": "notification-admit"
                    },
                    "focus": [{"reference": "Encounter/example-id"}]
                  }
                },
                {
                  "resource": {"resourceType": "Patient", "id": "example-id"}
                },
                {
                  "resource": {"resourceType": "Encounter", "id": "example-id"}
                }
              ]
            }
```

**注意**  
每个资源都是独立存储的，就像通过单独的 PUT 操作提交一样。如果需要完整的 FHIR 消息语义或参照完整性验证，请在提交之前对消息包进行预处理或实现应用程序级验证。

## 异步捆绑交易
<a name="managing-fhir-resources-async-transactions"></a>

AWS HealthLake 支持异步`Bundle`类型`transaction`，允许您提交包含最多 500 个资源的交易。当您提交异步事务时，会将其 HealthLake 排队等候处理，并立即返回轮询网址。您可以使用此 URL 来检查状态并检索响应。这遵循 [FHIR 异步捆绑包模式](https://hl7.org/fhir/async-bundle.html)。

**何时使用异步事务**  

+ 您需要在单笔交易中提交 100 个以上的资源（同步限制）。
+ 您希望避免在等待事务处理完成时阻塞应用程序。
+ 您需要以更高的吞吐量处理大量相关资源。

**重要**  
投票结果将在交易完成后的90天内公布。在这90天之后，轮询网址将不再返回结果。设计您的集成，以便在此窗口中检索和存储结果。

**注意**  
同步`Bundle`类型`transaction`继续支持多达 100 个资源，并且是默认的处理模式。如果您提交的资源超过 100 个`transaction`，但没有`Prefer: respond-async`标题的`Bundle`类型， HealthLake 则会返回`422 Unprocessable Entity`错误。异步处理`batch`不支持带类型的捆绑包，只能异步提交`Bundle`类型`transaction`（最多有 500 个操作）。

### 提交异步事务
<a name="async-transactions-submitting"></a>

要提交异步事务，请向数据存储端点发送带有`Prefer: respond-async`标头的`POST`请求。捆绑包必须有类型`transaction`。异步分发包处理`batch`不支持类型为 bundle。

HealthLake 在提交时对捆绑包进行初始验证。如果验证成功，则 HealthLake 返回 HTTP 202 Accepted，并返回包含轮询网址的`content-location`响应标头。

**提交异步`Bundle`类型 `transaction`**  


1. 向 HealthLake 数据存储端点发送`POST`请求。

   ```
   POST https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/
   ```

1. 使用捆绑包类型为请求构造一个 JSON 正文`transaction`。出于本过程的目的，请将文件另存为`async-transaction.json`。

   ```
   {
       "resourceType": "Bundle",
       "type": "transaction",
       "entry": [
           {
               "resource": {
                   "resourceType": "Patient",
                   "active": true,
                   "name": [
                       {
                           "use": "official",
                           "family": "Smith",
                           "given": ["Jane"]
                       }
                   ],
                   "gender": "female",
                   "birthDate": "1990-01-15"
               },
               "request": {
                   "method": "POST",
                   "url": "Patient"
               }
           },
           {
               "resource": {
                   "resourceType": "Observation",
                   "status": "final",
                   "code": {
                       "coding": [
                           {
                               "system": "http://loinc.org",
                               "code": "85354-9",
                               "display": "Blood pressure panel"
                           }
                       ]
                   },
                   "subject": {
                       "reference": "urn:uuid:example-patient-id"
                   }
               },
               "request": {
                   "method": "POST",
                   "url": "Observation"
               }
           }
       ]
   }
   ```

1. 发送带有`Prefer: respond-async`标题的请求。FHIR `Bundle` 交易类型使用具有[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 FHIR 授权的 SMART `POST` 请求。以下代码示例使用`curl`命令行工具进行演示。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request POST \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json' \
     --header 'Prefer: respond-async' \
     --data @async-transaction.json
   ```

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------

1. 成功提交后，服务器返回 HTTP 202 已接受。`content-location`响应标头包含轮询网址。响应正文是一种`OperationOutcome`资源。

   ```
   HTTP/1.1 202 Accepted
   content-location: https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Transaction/transactionId
   ```

   ```
   {
       "resourceType": "OperationOutcome",
       "issue": [
           {
               "severity": "information",
               "code": "informational",
               "diagnostics": "Submitted Asynchronous Bundle Transaction",
               "location": [
                   "https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Transaction/transactionId"
               ]
           }
       ]
   }
   ```

### 轮询交易状态
<a name="async-transactions-polling"></a>

提交异步事务后，使用`content-location`响应标头中的轮询网址来检查交易状态。向投票 URL 发送`GET`请求。

**注意**  
对于启用 SMART on FHIR 的数据存储，授权令牌必须包括对`Transaction`资源类型的`read`权限，以轮询交易状态。有关 FHIR 作用域上的 SMART 的更多信息，请参阅[支持的 FHIR OAuth 2.0 瞄准镜上的 SMART HealthLake](reference-smart-on-fhir-oauth-scopes.md)。

向投票 URL 发送`GET`请求。以下示例使用`curl`命令行工具。

------
#### [ SigV4 ]

Sigv4 授权

```
curl --request GET \
  'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Transaction/transactionId' \
  --aws-sigv4 'aws:amz:region:healthlake' \
  --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
  --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
  --header 'Accept: application/json'
```

------
#### [ SMART on FHIR ]

SMART 在 FHIR 授权上。授权令牌必须包含`Transaction`资源类型的`read`权限。

```
curl --request GET \
  'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Transaction/transactionId' \
  --header 'Authorization: Bearer $SMART_ACCESS_TOKEN' \
  --header 'Accept: application/json'
```

------

下表描述了可能的响应。


**轮询响应码**  

| HTTP 状态 | 含义 | 响应正文 | 
| --- | --- | --- | 
| 202 已接受 | 交易已排队 | OperationOutcome诊断 “已提交” | 
| 202 已接受 | 交易正在处理中 | OperationOutcome诊断 “进行中” | 
| 200 OK (200 确定) | 交易成功完成 | Bundle带类型 transaction-response | 
| 4xx/5xx | 交易失败 | OperationOutcome附有错误详情 | 

以下示例显示了每种响应类型。

**交易已排队 (202)**  


```
{
    "resourceType": "OperationOutcome",
    "id": "transactionId",
    "issue": [
        {
            "severity": "information",
            "code": "informational",
            "diagnostics": "SUBMITTED"
        }
    ]
}
```

**交易处理 (202)**  


```
{
    "resourceType": "OperationOutcome",
    "id": "transactionId",
    "issue": [
        {
            "severity": "information",
            "code": "informational",
            "diagnostics": "IN_PROGRESS"
        }
    ]
}
```

**交易已完成 (200)**  


```
{
    "resourceType": "Bundle",
    "type": "transaction-response",
    "entry": [
        {
            "response": {
                "status": "201",
                "location": "Patient/example-id/_history/1",
                "etag": "W/\"1\"",
                "lastModified": "2024-01-15T10:30:00.000Z"
            }
        },
        {
            "response": {
                "status": "201",
                "location": "Observation/example-id/_history/1",
                "etag": "W/\"1\"",
                "lastModified": "2024-01-15T10:30:00.000Z"
            }
        }
    ]
}
```

**交易失败 (4xx/5xx)**  


```
{
    "resourceType": "OperationOutcome",
    "issue": [
        {
            "severity": "error",
            "code": "exception",
            "diagnostics": "Transaction failed: conflict detected on resource Patient/example-id"
        }
    ]
}
```

### 处理订单
<a name="async-transactions-processing-order"></a>

类型的异步捆绑包`transaction`已排队，但不会按照严格的提交顺序进行处理。 HealthLake 根据可用容量和系统负载优化处理。

**重要**  
不要依赖交易按提交顺序进行处理。例如，如果您在上午 10:00 提交交易 A，在上午 10:01 提交交易 B，则事务 B 可能会在交易 A 之前完成。将您的申请设计为：  
处理 out-of-order完成。
使用轮询网址独立跟踪每笔交易。
如果顺序对您的用例很重要，请实施应用程序级排序。

### 配额和限制
<a name="async-transactions-quotas"></a>

以下配额和速率限制适用于异步交易。


**异步交易配额**  

| 配额 | 值 | 可调整 | 
| --- | --- | --- | 
| 每个异步事务的最大操作数 | 500 | 否 | 
| 每个数据存储的最大待处理事务数 | 500 | 是 | 
+ 异步交易共享中定义的相同 API 速率限制[服务限额](reference-healthlake-endpoints-quotas.md#reference-healthlake-quotas)。
+ 轮询交易状态与 FHIR 资源上的 read (`GET`) 操作具有相同的 API 速率限制。
+ 如果达到待处理交易限额，则后续提交将返回错误，直到现有交易完成。

### 错误处理
<a name="async-transactions-error-handling"></a>

对于 “事务” 捆绑包，捆绑包中包含的所有 FHIR 资源都将作为原子操作进行处理。操作中的所有资源都必须成功，否则不处理捆绑包中的任何操作。

错误分为两类：同步 HealthLake 返回的提交错误和通过轮询检索的处理错误。

**提交错误**  


HealthLake 在提交时验证捆绑包，并在事务排队之前同步返回错误。提交错误包括无效的 FHIR 资源验证错误、不支持的资源类型、超过 500 个操作限制以及将`Prefer: respond-async`标题与批处理捆绑包一起使用。如果已达到数据存储的待处理事务限制，则 HealthLake 返回 a `ThrottlingException`。当发生提交错误时，交易将不会排队。

**处理错误**  


处理错误是在事务排队之后发生的，并通过轮询 URL 返回。其中包括事务冲突，即另一个操作修改了作为事务一部分的资源，以及处理期间的服务器错误。发生处理错误时，不会对事务中的资源进行任何资源变更。轮询网址将返回`OperationOutcome`包含错误详情的。

# 删除 FHIR 资源
<a name="managing-fhir-resources-delete"></a>

FHIR `delete` 交互会将现有的 FHIR 资源从 HealthLake数据存储中移除。有关更多信息，请参阅 **FHIR R4 RESTful API** 文档[https://hl7.org/fhir/R4/http.html#delete](https://hl7.org/fhir/R4/http.html#delete)中的。

**删除 FHIR 资源**  


1. 收集 HealthLake `region`和`datastoreId`价值。有关更多信息，请参阅 [获取数据存储属性](managing-data-stores-describe.md)。

1. 确定`Resource`要删除的 FHIR 类型并收集关联`id`值。有关更多信息，请参阅 [资源类型](reference-fhir-resource-types.md)。

1. 使用收集到的 HealthLake `region`和值为请求构造一个 URL `datastoreId`。还要包括 FHIR `Resource` 类型及其关联`id`的。要查看以下示例中的整个 URL 路径，请滚动到 “**复制**” 按钮。

   ```
   DELETE https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Resource/id
   ```

1. 发送 请求。FHIR `delete` 交互在 FHIR 授权上使用[AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) 或 SMART 的`DELETE`请求。以下`curl`示例从 HealthLake 数据存储中移除现有 FHIR `Patient` 资源。要查看整个示例，请滚动到 “复**制**” 按钮。

------
#### [ SigV4 ]

   Sigv4 授权

   ```
   curl --request DELETE \
     'https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient/id' \
     --aws-sigv4 'aws:amz:region:healthlake' \
     --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
     --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
     --header 'Accept: application/json'
   ```

   服务器返回一个 `204` HTTP 状态码，确认资源已从 HealthLake 数据存储中删除。如果删除请求失败，您将收到一`400`系列 HTTP 状态代码，说明请求失败的原因。

------
#### [ SMART on FHIR ]

   该[https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html](https://docs.aws.amazon.com/healthlake/latest/APIReference/API_IdentityProviderConfiguration.html)数据类型的 SMART on FHIR 授权示例。

   ```
   {
       "AuthorizationStrategy": "SMART_ON_FHIR",
       "FineGrainedAuthorizationEnabled": true,
       "IdpLambdaArn": "arn:aws:lambda:your-region:your-account-id:function:your-lambda-name",
       "Metadata": "{\"issuer\":\"https://ehr.example.com\", \"jwks_uri\":\"https://ehr.example.com/.well-known/jwks.json\",\"authorization_endpoint\":\"https://ehr.example.com/auth/authorize\",\"token_endpoint\":\"https://ehr.token.com/auth/token\",\"token_endpoint_auth_methods_supported\":[\"client_secret_basic\",\"foo\"],\"grant_types_supported\":[\"client_credential\",\"foo\"],\"registration_endpoint\":\"https://ehr.example.com/auth/register\",\"scopes_supported\":[\"openId\",\"profile\",\"launch\"],\"response_types_supported\":[\"code\"],\"management_endpoint\":\"https://ehr.example.com/user/manage\",\"introspection_endpoint\":\"https://ehr.example.com/user/introspect\",\"revocation_endpoint\":\"https://ehr.example.com/user/revoke\",\"code_challenge_methods_supported\":[\"S256\"],\"capabilities\":[\"launch-ehr\",\"sso-openid-connect\",\"client-public\",\"permission-v2\"]}"
   }
   ```

   调用者可以在授权 lambda 中分配权限。有关更多信息，请参阅 [OAuth 2.0 瞄准镜](reference-smart-on-fhir-oauth-scopes.md)。

------
#### [ AWS Console ]

   1. 登录 HealthLake 控制台上的 “[运行查询](https://console.aws.amazon.com/healthlake/home#/crud)” 页面。

   2. 在 **“查询设置”** 部分下，进行以下选择。
   + **数据存储 ID**-选择数据存储 ID 以生成查询字符串。
   + **查询类型**-选择`Delete`。
   + **资源类型**-选择要删除的 FHIR [资源类型](reference-fhir-resource-types.md)。
   + **资源 ID** — 输入 FHIR 资源 ID。

   3. 选择**运行查询**。

------

## 根据条件删除 FHIR 资源
<a name="conditional-delete-fhir"></a>

当您不知道特定的 FHIR 资源 ID 但有关于要删除的资源的其他识别信息时，条件删除特别有用。

有条件删除允许您根据搜索标准而不是逻辑 FHIR ID 删除现有资源。当服务器处理删除请求时，它会使用标准搜索功能对资源类型执行搜索，以解析请求的单个逻辑 ID。

### 有条件删除的工作原理
<a name="conditional-delete-works"></a>

**服务器的操作取决于它找到的匹配项数量：**  


1. **无匹配项**：服务器尝试普通删除并做出相应响应（404 表示资源不存在，204 表示已删除的资源为 No Content）

1. **一场匹配**：服务器对匹配的资源执行普通删除

1. **多个匹配项**：返回 412 Precondition Failed 错误，表示客户端的标准选择性不足

### 响应场景
<a name="response-scenerios"></a>

AWS HealthLake 使用以下响应模式处理有条件的删除操作：

**成功运营**  

+ 当您的搜索条件成功识别出单个活动资源时，系统将在完成删除后返回 **204 No Conten** t，就像标准删除操作一样。

**基于身份的有条件删除**  
根据`id`附加参数（`createdAt``tag`、或`_lastUpdated`）执行有条件删除时：
+ **204 无内容**：资源已被删除
+ **404 未找到**：资源不存在
+ **409 冲突**：ID 匹配但其他参数不匹配

**Non-ID-Based 有条件删除**  
当`id`未提供或使用`createdAt``tag`、或之外的参数时`_lastUpdated`：
+ **404 未找到**：未找到匹配项

**冲突局势**  
有几种情况会导致 412 个先决条件失败的响应：
+ 多个资源与您的搜索条件相匹配（条件不够具体）
+ 将 ETag 标头与一起使用时会发生版本冲突 `If-Match`
+ 在搜索和删除操作之间发生资源更新

**成功执行有条件删除的示例**  
以下示例根据特定标准删除患者资源：

```
DELETE https://healthlake.region.amazonaws.com/datastore/datastoreId/r4/Patient?name=peter&birthdate=2000-01-01&phone=1234567890
```

此请求会删除患者资源，其中：
+ 名字叫 “彼得”
+ 出生日期是 2000 年 1 月 1 日
+ 电话号码是 1234567890

**最佳实践**  


1. 使用特定的搜索条件来避免多个匹配项并防止 412 错误。

1. 当需要处理并发修改时，可以考虑使用 ETag 标头进行版本控制。

1. 适当处理错误响应：
   + 对于 404：优化搜索条件
   + 对于 412：使标准更加具体或解决版本冲突

1. 为高并发环境中的时间冲突做好准备，在这种环境中，可能会在搜索和删除操作之间修改资源。

# 等性与并发性
<a name="managing-fhir-resources-idempotency"></a>

## 等能键
<a name="idempotency-keys"></a>

AWS HealthLake 支持 FHIR `POST` 操作的等效密钥，提供了一种强大的机制来确保资源创建期间的数据完整性。通过在请求标头中包含唯一的 UUID 作为等性密钥，医疗保健应用程序可以保证每个 FHIR 资源只创建一次，即使在涉及网络不稳定或自动重试的场景中也是如此。

此功能对于医疗保健系统尤其重要，因为在这些系统中，重复的病历可能会造成严重后果。当收到的请求与之前的请求具有相同的等效键时， HealthLake 将返回原始资源，而不是创建重复的资源。例如，这可能发生在重试循环中，或者由于请求管道冗余。使用等性键可以保持数据一致性 HealthLake ，同时为处理间歇性连接问题的客户端应用程序提供无缝体验。

### 实施
<a name="implementation"></a>

```
POST /<baseURL>/Patient
x-amz-fhir-idempotency-key: 123e4567-e89b-12d3-a456-426614174000
{
    "resourceType": "Patient",
    "name": [...]
}
```

### 响应场景
<a name="response-scenarios"></a>

第一个请求（已创建 201 个）  
+ 新资源已成功创建
+ 响应包含资源 ID

重复请求（409 冲突）  
+ 检测到相同的等性密钥
+ 已返回原始资源
+ 未创建任何新资源

请求无效（400 错误请求）  
+ 格式错误的 UUID
+ 缺少必填字段

### 最佳实践
<a name="best-practices"></a>
+ 为每个新创建的资源生成唯一的 UUID
+ 存储用于重试逻辑的等性键
+ 使用一致的密钥格式：推荐 UUID v4
+ 在处理资源创建的客户端应用程序中实现

**注意**  
对于要求严格数据准确性并防止重复医疗记录的医疗保健系统而言，此功能特别有用。

## ETag 在 AWS 中 HealthLake
<a name="healthlake-etag"></a>

AWS HealthLake ETags 用于对 FHIR 资源进行乐观的并发控制，为管理并发修改和维护数据一致性提供了一种可靠的机制。 ETag是代表资源的特定版本的唯一标识符，通过 HTTP 标头充当版本控制系统。在读取或修改资源时，应用程序可以使用 ETags 来防止意外重写并确保数据完整性，尤其是在可能并发更新的情况下。

### 实现示例
<a name="healthlake-etag-implementation"></a>

```
// Initial Read
GET /fhir/Patient/123
Response: 
ETag: W/"1"

// Update with If-Match
PUT /fhir/Patient/123
If-Match: W/"1"
{resource content}

// Create with If-None-Match
PUT /fhir/Patient/123
If-None-Match: *
{resource content}
// Succeeds only if resource doesn't exist
// Fails with 412 if resource exists
```

### 响应场景
<a name="healthlake-etag-scenarios"></a>

成功操作（200 OK 或 204 无内容）  
+ ETag 匹配当前版本
+ 操作按预期进行

版本冲突（412 前提条件失败）  
+ ETag 与当前版本不匹配
+ 为了防止数据丢失，更新被拒绝

### 最佳实践
<a name="healthlake-etag-practices"></a>
+ 包含 ETags 在所有更新和删除操作中
+ 实现用于处理版本冲突的重试逻辑
+ 使用 If-None-Match:\$1 用于 create-if-not-exists场景
+ 修改前务必验证 ETag 新鲜度

这种并发控制系统对于维护医疗保健数据的完整性至关重要，尤其是在有多个用户或系统访问和修改相同资源的环境中。