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

Amazon Personalize Runtime 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 Runtime.

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 GetPersonalizedRanking.

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 List<PredictedItem> getRankedRecs(PersonalizeRuntimeClient personalizeRuntimeClient, String campaignArn, String userId, ArrayList<String> items) { try { GetPersonalizedRankingRequest rankingRecommendationsRequest = GetPersonalizedRankingRequest.builder() .campaignArn(campaignArn) .userId(userId) .inputList(items) .build(); GetPersonalizedRankingResponse recommendationsResponse = personalizeRuntimeClient .getPersonalizedRanking(rankingRecommendationsRequest); List<PredictedItem> rankedItems = recommendationsResponse.personalizedRanking(); int rank = 1; for (PredictedItem item : rankedItems) { System.out.println("Item ranked at position " + rank + " details"); System.out.println("Item Id is : " + item.itemId()); System.out.println("Item score is : " + item.score()); System.out.println("---------------------------------------------"); rank++; } return rankedItems; } catch (PersonalizeRuntimeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; }

The following code example shows how to use GetRecommendations.

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.

Get a list of recommended items.

public static void getRecs(PersonalizeRuntimeClient personalizeRuntimeClient, String campaignArn, String userId) { try { GetRecommendationsRequest recommendationsRequest = GetRecommendationsRequest.builder() .campaignArn(campaignArn) .numResults(20) .userId(userId) .build(); GetRecommendationsResponse recommendationsResponse = personalizeRuntimeClient .getRecommendations(recommendationsRequest); List<PredictedItem> items = recommendationsResponse.itemList(); for (PredictedItem item : items) { System.out.println("Item Id is : " + item.itemId()); System.out.println("Item score is : " + item.score()); } } catch (AwsServiceException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

Get a list of recommended items from a recommender created in a domain dataset group.

public static void getRecs(PersonalizeRuntimeClient personalizeRuntimeClient, String recommenderArn, String userId) { try { GetRecommendationsRequest recommendationsRequest = GetRecommendationsRequest.builder() .recommenderArn(recommenderArn) .numResults(20) .userId(userId) .build(); GetRecommendationsResponse recommendationsResponse = personalizeRuntimeClient .getRecommendations(recommendationsRequest); List<PredictedItem> items = recommendationsResponse.itemList(); for (PredictedItem item : items) { System.out.println("Item Id is : " + item.itemId()); System.out.println("Item score is : " + item.score()); } } catch (AwsServiceException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

Use a filter when requesting recommendations.

public static void getFilteredRecs(PersonalizeRuntimeClient personalizeRuntimeClient, String campaignArn, String userId, String filterArn, String parameter1Name, String parameter1Value1, String parameter1Value2, String parameter2Name, String parameter2Value) { try { Map<String, String> filterValues = new HashMap<>(); filterValues.put(parameter1Name, String.format("\"%1$s\",\"%2$s\"", parameter1Value1, parameter1Value2)); filterValues.put(parameter2Name, String.format("\"%1$s\"", parameter2Value)); GetRecommendationsRequest recommendationsRequest = GetRecommendationsRequest.builder() .campaignArn(campaignArn) .numResults(20) .userId(userId) .filterArn(filterArn) .filterValues(filterValues) .build(); GetRecommendationsResponse recommendationsResponse = personalizeRuntimeClient .getRecommendations(recommendationsRequest); List<PredictedItem> items = recommendationsResponse.itemList(); for (PredictedItem item : items) { System.out.println("Item Id is : " + item.itemId()); System.out.println("Item score is : " + item.score()); } } catch (PersonalizeRuntimeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }