Amazon Personalize Events examples using SDK for Java 2.x - AWS SDK for Java 2.x

Amazon Personalize Events examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Amazon Personalize Events.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use PutEvents.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public static int putItems(PersonalizeEventsClient personalizeEventsClient, String datasetArn, String item1Id, String item1PropertyName, String item1PropertyValue, String item2Id, String item2PropertyName, String item2PropertyValue) { int responseCode = 0; ArrayList<Item> items = new ArrayList<>(); try { Item item1 = Item.builder() .itemId(item1Id) .properties(String.format("{\"%1$s\": \"%2$s\"}", item1PropertyName, item1PropertyValue)) .build(); items.add(item1); Item item2 = Item.builder() .itemId(item2Id) .properties(String.format("{\"%1$s\": \"%2$s\"}", item2PropertyName, item2PropertyValue)) .build(); items.add(item2); PutItemsRequest putItemsRequest = PutItemsRequest.builder() .datasetArn(datasetArn) .items(items) .build(); responseCode = personalizeEventsClient.putItems(putItemsRequest).sdkHttpResponse().statusCode(); System.out.println("Response code: " + responseCode); return responseCode; } catch (PersonalizeEventsException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return responseCode; }
  • For API details, see PutEvents in AWS SDK for Java 2.x API Reference.

The following code example shows how to use PutUsers.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public static int putUsers(PersonalizeEventsClient personalizeEventsClient, String datasetArn, String user1Id, String user1PropertyName, String user1PropertyValue, String user2Id, String user2PropertyName, String user2PropertyValue) { int responseCode = 0; ArrayList<User> users = new ArrayList<>(); try { User user1 = User.builder() .userId(user1Id) .properties(String.format("{\"%1$s\": \"%2$s\"}", user1PropertyName, user1PropertyValue)) .build(); users.add(user1); User user2 = User.builder() .userId(user2Id) .properties(String.format("{\"%1$s\": \"%2$s\"}", user2PropertyName, user2PropertyValue)) .build(); users.add(user2); PutUsersRequest putUsersRequest = PutUsersRequest.builder() .datasetArn(datasetArn) .users(users) .build(); responseCode = personalizeEventsClient.putUsers(putUsersRequest).sdkHttpResponse().statusCode(); System.out.println("Response code: " + responseCode); return responseCode; } catch (PersonalizeEventsException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return responseCode; }
  • For API details, see PutUsers in AWS SDK for Java 2.x API Reference.