Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use GetPersonalizedRanking
with an Amazon SDK or CLI
The following code examples show how to use GetPersonalizedRanking
.
- Java
-
- SDK for Java 2.x
-
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)
-
// 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();
For a complete list of Amazon SDK developer guides and code examples, see
Using Amazon Personalize with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.