问题描述
有人可以帮助您开始使用AWS Java SDK for Personalize吗?我习惯在控制台上制作广告系列。现在,我想使用Java查询建议。
Amazon在Python中有很多示例,但是我需要Java。从客户端构建器开始特别有用。
谢谢。
解决方法
要使用Java V2查询建议,请使用 PersonalizeRuntimeClient 对象。
以下是使用Java V2的示例。我们很快就会在Java V2 Github中发布完整的示例。
**
package com.example.personalize;
//snippet-start:[personalize.java2.get_recommendations.import]
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.personalizeruntime.PersonalizeRuntimeClient;
import software.amazon.awssdk.services.personalizeruntime.model.GetRecommendationsRequest;
import software.amazon.awssdk.services.personalizeruntime.model.GetRecommendationsResponse;
import software.amazon.awssdk.services.personalizeruntime.model.PredictedItem;
import java.util.List;
//snippet-end:[personalize.java2.get_recommendations.import]
public class GetRecommendations {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
" GetRecommendations <campaignArn> <userId>\n\n" +
"Where:\n" +
" campaignArn - The ARN of the campaign.\n\n" +
" userId - The user ID to provide recommendations for\n\n";
if (args.length < 2) {
System.out.println(USAGE);
System.exit(1);
}
/* Read the name from command args */
String campaignArn = args[0];
String userId = args[1];
Region region = Region.US_EAST_1;
PersonalizeRuntimeClient personalizeRuntimeClient = PersonalizeRuntimeClient.builder()
.region(region)
.build();
getRecs(personalizeRuntimeClient,campaignArn,userId);
}
//snippet-start:[personalize.java2.get_recommendations.main]
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);
}
}
//snippet-end:[personalize.java2.get_recommendations.main]
}
**