Use GetPersonalizedRanking with an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use GetPersonalizedRanking with an AWS SDK

The following code examples show how to use GetPersonalizedRanking.

Java
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; }
JavaScript
SDK for JavaScript (v3)
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 service clients module and commands using ES6 syntax. import { GetPersonalizedRankingCommand } from "@aws-sdk/client-personalize-runtime"; import { personalizeRuntimeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeRuntimeClient = new PersonalizeRuntimeClient({ region: "REGION"}); // Set the ranking request parameters. export const getPersonalizedRankingParam = { campaignArn: "CAMPAIGN_ARN" /* required */, userId: "USER_ID" /* required */, inputList: ["ITEM_ID_1", "ITEM_ID_2", "ITEM_ID_3", "ITEM_ID_4"], }; export const run = async () => { try { const response = await personalizeRuntimeClient.send( new GetPersonalizedRankingCommand(getPersonalizedRankingParam), ); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();