将端点批量添加到 Amazon Pinpoint - Amazon Pinpoint

终止支持通知: AWS 将于 2026 年 10 月 30 日终止对亚马逊 Pinpoint 的支持。2026 年 10 月 30 日之后,您将不再能够访问 Amazon Pinpoint 控制台或 Amazon Pinpoint 资源(端点、分段、活动、旅程和分析)。有关更多信息,请参阅 Amazon Pinpoint 终止支持注意: APIs 与短信相关、语音、移动推送、OTP 和电话号码验证不受此更改的影响,并受 AWS 最终用户消息的支持。

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

将端点批量添加到 Amazon Pinpoint

您可以通过批量提供端点的方式在单个操作中添加或更新多个端点。每个批处理请求最多可以包含 100 个端点定义。

如果要在一个操作中添加或更新 100 个以上的端点,请参阅将端点导入 Amazon Pinpoint

示例

以下示例演示如何通过将两个端点包含在一个批处理请求中来一次添加两个端点。

AWS CLI

可以通过在 AWS CLI 中运行命令来使用 Amazon Pinpoint。

例更新端点批处理命令

要提交端点批处理请求,请使用 update-endpoints-batch 命令:

$ aws pinpoint update-endpoints-batch \ > --application-id application-id \ > --endpoint-batch-request file://endpoint_batch_request_file.json

其中:

  • application-id 是要在其中添加或更新端点的 Amazon Pinpoint 项目的 ID。

  • endpoint_batch_request_file.json 是至包含 --endpoint-batch-request 参数输入的本地 JSON 文件的文件路径。

例端点批处理请求文件

示例 update-endpoints-batch 命令使用 JSON 文件作为 --endpoint-request 形参 (parameter) 的实参 (argument)。此文件包含一批与下类似的端点定义:

{ "Item": [ { "ChannelType": "EMAIL", "Address": "richard_roe@example.com", "Attributes": { "Interests": [ "Music", "Books" ] }, "Metrics": { "music_interest_level": 3.0, "books_interest_level": 7.0 }, "Id": "example_endpoint_1", "User":{ "UserId": "example_user_1", "UserAttributes": { "FirstName": "Richard", "LastName": "Roe" } } }, { "ChannelType": "SMS", "Address": "+16145550100", "Attributes": { "Interests": [ "Cooking", "Politics", "Finance" ] }, "Metrics": { "cooking_interest_level": 5.0, "politics_interest_level": 8.0, "finance_interest_level": 4.0 }, "Id": "example_endpoint_2", "User": { "UserId": "example_user_2", "UserAttributes": { "FirstName": "Mary", "LastName": "Major" } } } ] }

有关可用于定义一批端点的属性,请参阅《Amazon Pinpoint API 参考》中的 EndpointBatchRequest 架构

AWS SDK for Java

您可以通过使用 AWS SDK for Java 提供的客户端在您的 Java 应用程序中使用 Amazon Pinpoint API。

例代码

要提交端点批处理请求,请初始化 EndpointBatchRequest 对象并将其传递到 AmazonPinpoint 客户端的 updateEndpointsBatch 方法。以下示例使用两个 EndpointBatchItem 对象填充 EndpointBatchRequest 对象:

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.UpdateEndpointsBatchResponse; import software.amazon.awssdk.services.pinpoint.model.EndpointUser; import software.amazon.awssdk.services.pinpoint.model.EndpointBatchItem; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.EndpointBatchRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.UpdateEndpointsBatchRequest; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.HashMap;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.UpdateEndpointsBatchResponse; import software.amazon.awssdk.services.pinpoint.model.EndpointUser; import software.amazon.awssdk.services.pinpoint.model.EndpointBatchItem; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.EndpointBatchRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpoint.model.UpdateEndpointsBatchRequest; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.HashMap; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class AddExampleEndpoints { public static void main(String[] args) { final String usage = """ Usage: <appId> Where: appId - The ID of the application. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String applicationId = args[0]; PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); updateEndpointsViaBatch(pinpoint, applicationId); pinpoint.close(); } public static void updateEndpointsViaBatch(PinpointClient pinpoint, String applicationId) { try { List<String> myList = new ArrayList<>(); myList.add("music"); myList.add("books"); Map myMap = new HashMap<String, List>(); myMap.put("attributes", myList); List<String> myNames = new ArrayList<String>(); myList.add("Richard"); myList.add("Roe"); Map myMap2 = new HashMap<String, List>(); myMap2.put("name", myNames); EndpointUser richardRoe = EndpointUser.builder() .userId("example_user_1") .userAttributes(myMap2) .build(); // Create an EndpointBatchItem object for Richard Roe. EndpointBatchItem richardRoesEmailEndpoint = EndpointBatchItem.builder() .channelType(ChannelType.EMAIL) .address("richard_roe@example.com") .id("example_endpoint_1") .attributes(myMap) .user(richardRoe) .build(); List<String> myListMary = new ArrayList<String>(); myListMary.add("cooking"); myListMary.add("politics"); myListMary.add("finance"); Map myMapMary = new HashMap<String, List>(); myMapMary.put("interests", myListMary); List<String> myNameMary = new ArrayList<String>(); myNameMary.add("Mary "); myNameMary.add("Major"); Map maryName = new HashMap<String, List>(); myMapMary.put("name", myNameMary); EndpointUser maryMajor = EndpointUser.builder() .userId("example_user_2") .userAttributes(maryName) .build(); // Create an EndpointBatchItem object for Mary Major. EndpointBatchItem maryMajorsSmsEndpoint = EndpointBatchItem.builder() .channelType(ChannelType.SMS) .address("+16145550100") .id("example_endpoint_2") .attributes(myMapMary) .user(maryMajor) .build(); // Adds multiple endpoint definitions to a single request object. EndpointBatchRequest endpointList = EndpointBatchRequest.builder() .item(richardRoesEmailEndpoint) .item(maryMajorsSmsEndpoint) .build(); // Create the UpdateEndpointsBatchRequest. UpdateEndpointsBatchRequest batchRequest = UpdateEndpointsBatchRequest.builder() .applicationId(applicationId) .endpointBatchRequest(endpointList) .build(); // Updates the endpoints with Amazon Pinpoint. UpdateEndpointsBatchResponse result = pinpoint.updateEndpointsBatch(batchRequest); System.out.format("Update endpoints batch result: %s\n", result.messageBody().message()); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

有关完整的 SDK 示例,请参阅 GitHub 上的 AddExampleEndpoints.java

HTTP

可以通过直接向 REST API 发出 HTTP 请求来使用 Amazon Pinpoint。

例 Put 端点请求

要提交端点批处理请求,请向位于以下 URI 的端点资源发出 PUT 请求:

/v1/apps/application-id/endpoints

其中,application-id 是要在其中添加或更新端点的 Amazon Pinpoint 项目的 ID。

在您的请求中,包括所需标头,并提供 EndpointBatchRequest JSON 作为正文:

PUT /v1/apps/application_id/endpoints HTTP/1.1 Host: pinpoint.us-east-1.amazonaws.com Content-Type: application/json Accept: application/json X-Amz-Date: 20180501T184948Z Authorization: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20180501/us-east-1/mobiletargeting/aws4_request, SignedHeaders=accept;content-length;content-type;host;x-amz-date, Signature=c25cbd6bf61bd3b3667c571ae764b9bf2d8af61b875cacced95d1e68d91b4170 Cache-Control: no-cache { "Item": [ { "ChannelType": "EMAIL", "Address": "richard_roe@example.com", "Attributes": { "Interests": [ "Music", "Books" ] }, "Metrics": { "music_interest_level": 3.0, "books_interest_level": 7.0 }, "Id": "example_endpoint_1", "User":{ "UserId": "example_user_1", "UserAttributes": { "FirstName": "Richard", "LastName": "Roe" } } }, { "ChannelType": "SMS", "Address": "+16145550100", "Attributes": { "Interests": [ "Cooking", "Politics", "Finance" ] }, "Metrics": { "cooking_interest_level": 5.0, "politics_interest_level": 8.0, "finance_interest_level": 4.0 }, "Id": "example_endpoint_2", "User": { "UserId": "example_user_2", "UserAttributes": { "FirstName": "Mary", "LastName": "Major" } } } ] }

如果您的请求成功,将收到与下类似的响应:

{ "RequestID": "67e572ed-41d5-11e8-9dc5-db288f3cbb72", "Message": "Accepted" }

有关 Amazon Pinpoint API 中的端点资源的更多信息,包括支持的 HTTP 方法和请求参数,请参阅《Amazon Pinpoint API 参考》中的端点