Amazon Personalize examples using SDK for Java 2.x - Amazon SDK for Java 2.x
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).

Amazon Personalize examples using SDK for Java 2.x

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

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

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 Amazon Code Examples Repository.

public static String createPersonalizeBatchInferenceJob(PersonalizeClient personalizeClient, String solutionVersionArn, String jobName, String s3InputDataSourcePath, String s3DataDestinationPath, String roleArn, String explorationWeight, String explorationItemAgeCutOff) { long waitInMilliseconds = 60 * 1000; String status; String batchInferenceJobArn; try { // Set up data input and output parameters. S3DataConfig inputSource = S3DataConfig.builder() .path(s3InputDataSourcePath) .build(); S3DataConfig outputDestination = S3DataConfig.builder() .path(s3DataDestinationPath) .build(); BatchInferenceJobInput jobInput = BatchInferenceJobInput.builder() .s3DataSource(inputSource) .build(); BatchInferenceJobOutput jobOutputLocation = BatchInferenceJobOutput.builder() .s3DataDestination(outputDestination) .build(); // Optional code to build the User-Personalization specific item exploration // config. HashMap<String, String> explorationConfig = new HashMap<>(); explorationConfig.put("explorationWeight", explorationWeight); explorationConfig.put("explorationItemAgeCutOff", explorationItemAgeCutOff); BatchInferenceJobConfig jobConfig = BatchInferenceJobConfig.builder() .itemExplorationConfig(explorationConfig) .build(); // End optional User-Personalization recipe specific code. CreateBatchInferenceJobRequest createBatchInferenceJobRequest = CreateBatchInferenceJobRequest .builder() .solutionVersionArn(solutionVersionArn) .jobInput(jobInput) .jobOutput(jobOutputLocation) .jobName(jobName) .roleArn(roleArn) .batchInferenceJobConfig(jobConfig) // Optional .build(); batchInferenceJobArn = personalizeClient.createBatchInferenceJob(createBatchInferenceJobRequest) .batchInferenceJobArn(); DescribeBatchInferenceJobRequest describeBatchInferenceJobRequest = DescribeBatchInferenceJobRequest .builder() .batchInferenceJobArn(batchInferenceJobArn) .build(); long maxTime = Instant.now().getEpochSecond() + 3 * 60 * 60; while (Instant.now().getEpochSecond() < maxTime) { BatchInferenceJob batchInferenceJob = personalizeClient .describeBatchInferenceJob(describeBatchInferenceJobRequest) .batchInferenceJob(); status = batchInferenceJob.status(); System.out.println("Batch inference job status: " + status); if (status.equals("ACTIVE") || status.equals("CREATE FAILED")) { break; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } return batchInferenceJobArn; } catch (PersonalizeException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return ""; }

The following code example shows how to use CreateCampaign.

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 Amazon Code Examples Repository.

public static void createPersonalCompaign(PersonalizeClient personalizeClient, String solutionVersionArn, String name) { try { CreateCampaignRequest createCampaignRequest = CreateCampaignRequest.builder() .minProvisionedTPS(1) .solutionVersionArn(solutionVersionArn) .name(name) .build(); CreateCampaignResponse campaignResponse = personalizeClient.createCampaign(createCampaignRequest); System.out.println("The campaign ARN is " + campaignResponse.campaignArn()); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see CreateCampaign in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateDataset.

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 Amazon Code Examples Repository.

public static String createDataset(PersonalizeClient personalizeClient, String datasetName, String datasetGroupArn, String datasetType, String schemaArn) { try { CreateDatasetRequest request = CreateDatasetRequest.builder() .name(datasetName) .datasetGroupArn(datasetGroupArn) .datasetType(datasetType) .schemaArn(schemaArn) .build(); String datasetArn = personalizeClient.createDataset(request) .datasetArn(); System.out.println("Dataset " + datasetName + " created."); return datasetArn; } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • For API details, see CreateDataset in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateDatasetExportJob.

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 Amazon Code Examples Repository.

public static String createDatasetExportJob(PersonalizeClient personalizeClient, String jobName, String datasetArn, IngestionMode ingestionMode, String roleArn, String s3BucketPath, String kmsKeyArn) { long waitInMilliseconds = 30 * 1000; // 30 seconds String status = null; try { S3DataConfig exportS3DataConfig = S3DataConfig.builder().path(s3BucketPath).kmsKeyArn(kmsKeyArn).build(); DatasetExportJobOutput jobOutput = DatasetExportJobOutput.builder().s3DataDestination(exportS3DataConfig) .build(); CreateDatasetExportJobRequest createRequest = CreateDatasetExportJobRequest.builder() .jobName(jobName) .datasetArn(datasetArn) .ingestionMode(ingestionMode) .jobOutput(jobOutput) .roleArn(roleArn) .build(); String datasetExportJobArn = personalizeClient.createDatasetExportJob(createRequest).datasetExportJobArn(); DescribeDatasetExportJobRequest describeDatasetExportJobRequest = DescribeDatasetExportJobRequest.builder() .datasetExportJobArn(datasetExportJobArn) .build(); long maxTime = Instant.now().getEpochSecond() + 3 * 60 * 60; while (Instant.now().getEpochSecond() < maxTime) { DatasetExportJob datasetExportJob = personalizeClient .describeDatasetExportJob(describeDatasetExportJobRequest) .datasetExportJob(); status = datasetExportJob.status(); System.out.println("Export job status: " + status); if (status.equals("ACTIVE") || status.equals("CREATE FAILED")) { return status; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } } catch (PersonalizeException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return ""; }

The following code example shows how to use CreateDatasetGroup.

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 Amazon Code Examples Repository.

public static String createDatasetGroup(PersonalizeClient personalizeClient, String datasetGroupName) { try { CreateDatasetGroupRequest createDatasetGroupRequest = CreateDatasetGroupRequest.builder() .name(datasetGroupName) .build(); return personalizeClient.createDatasetGroup(createDatasetGroupRequest).datasetGroupArn(); } catch (PersonalizeException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return ""; }

Create a domain dataset group.

public static String createDomainDatasetGroup(PersonalizeClient personalizeClient, String datasetGroupName, String domain) { try { CreateDatasetGroupRequest createDatasetGroupRequest = CreateDatasetGroupRequest.builder() .name(datasetGroupName) .domain(domain) .build(); return personalizeClient.createDatasetGroup(createDatasetGroupRequest).datasetGroupArn(); } catch (PersonalizeException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return ""; }

The following code example shows how to use CreateDatasetImportJob.

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 Amazon Code Examples Repository.

public static String createPersonalizeDatasetImportJob(PersonalizeClient personalizeClient, String jobName, String datasetArn, String s3BucketPath, String roleArn) { long waitInMilliseconds = 60 * 1000; String status; String datasetImportJobArn; try { DataSource importDataSource = DataSource.builder() .dataLocation(s3BucketPath) .build(); CreateDatasetImportJobRequest createDatasetImportJobRequest = CreateDatasetImportJobRequest.builder() .datasetArn(datasetArn) .dataSource(importDataSource) .jobName(jobName) .roleArn(roleArn) .build(); datasetImportJobArn = personalizeClient.createDatasetImportJob(createDatasetImportJobRequest) .datasetImportJobArn(); DescribeDatasetImportJobRequest describeDatasetImportJobRequest = DescribeDatasetImportJobRequest.builder() .datasetImportJobArn(datasetImportJobArn) .build(); long maxTime = Instant.now().getEpochSecond() + 3 * 60 * 60; while (Instant.now().getEpochSecond() < maxTime) { DatasetImportJob datasetImportJob = personalizeClient .describeDatasetImportJob(describeDatasetImportJobRequest) .datasetImportJob(); status = datasetImportJob.status(); System.out.println("Dataset import job status: " + status); if (status.equals("ACTIVE") || status.equals("CREATE FAILED")) { break; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } return datasetImportJobArn; } catch (PersonalizeException e) { System.out.println(e.awsErrorDetails().errorMessage()); } return ""; }

The following code example shows how to use CreateEventTracker.

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 Amazon Code Examples Repository.

public static String createEventTracker(PersonalizeClient personalizeClient, String eventTrackerName, String datasetGroupArn) { String eventTrackerId = ""; String eventTrackerArn; long maxTime = 3 * 60 * 60; // 3 hours long waitInMilliseconds = 20 * 1000; // 20 seconds String status; try { CreateEventTrackerRequest createEventTrackerRequest = CreateEventTrackerRequest.builder() .name(eventTrackerName) .datasetGroupArn(datasetGroupArn) .build(); CreateEventTrackerResponse createEventTrackerResponse = personalizeClient .createEventTracker(createEventTrackerRequest); eventTrackerArn = createEventTrackerResponse.eventTrackerArn(); eventTrackerId = createEventTrackerResponse.trackingId(); System.out.println("Event tracker ARN: " + eventTrackerArn); System.out.println("Event tracker ID: " + eventTrackerId); maxTime = Instant.now().getEpochSecond() + maxTime; DescribeEventTrackerRequest describeRequest = DescribeEventTrackerRequest.builder() .eventTrackerArn(eventTrackerArn) .build(); while (Instant.now().getEpochSecond() < maxTime) { status = personalizeClient.describeEventTracker(describeRequest).eventTracker().status(); System.out.println("EventTracker status: " + status); if (status.equals("ACTIVE") || status.equals("CREATE FAILED")) { break; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } return eventTrackerId; } catch (PersonalizeException e) { System.out.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return eventTrackerId; }

The following code example shows how to use CreateFilter.

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 Amazon Code Examples Repository.

public static String createFilter(PersonalizeClient personalizeClient, String filterName, String datasetGroupArn, String filterExpression) { try { CreateFilterRequest request = CreateFilterRequest.builder() .name(filterName) .datasetGroupArn(datasetGroupArn) .filterExpression(filterExpression) .build(); return personalizeClient.createFilter(request).filterArn(); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • For API details, see CreateFilter in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateRecommender.

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 Amazon Code Examples Repository.

public static String createRecommender(PersonalizeClient personalizeClient, String name, String datasetGroupArn, String recipeArn) { long maxTime = 0; long waitInMilliseconds = 30 * 1000; // 30 seconds String recommenderStatus = ""; try { CreateRecommenderRequest createRecommenderRequest = CreateRecommenderRequest.builder() .datasetGroupArn(datasetGroupArn) .name(name) .recipeArn(recipeArn) .build(); CreateRecommenderResponse recommenderResponse = personalizeClient .createRecommender(createRecommenderRequest); String recommenderArn = recommenderResponse.recommenderArn(); System.out.println("The recommender ARN is " + recommenderArn); DescribeRecommenderRequest describeRecommenderRequest = DescribeRecommenderRequest.builder() .recommenderArn(recommenderArn) .build(); maxTime = Instant.now().getEpochSecond() + 3 * 60 * 60; while (Instant.now().getEpochSecond() < maxTime) { recommenderStatus = personalizeClient.describeRecommender(describeRecommenderRequest).recommender() .status(); System.out.println("Recommender status: " + recommenderStatus); if (recommenderStatus.equals("ACTIVE") || recommenderStatus.equals("CREATE FAILED")) { break; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } return recommenderArn; } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

The following code example shows how to use CreateSchema.

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 Amazon Code Examples Repository.

public static String createSchema(PersonalizeClient personalizeClient, String schemaName, String filePath) { String schema = null; try { schema = new String(Files.readAllBytes(Paths.get(filePath))); } catch (IOException e) { System.out.println(e.getMessage()); } try { CreateSchemaRequest createSchemaRequest = CreateSchemaRequest.builder() .name(schemaName) .schema(schema) .build(); String schemaArn = personalizeClient.createSchema(createSchemaRequest).schemaArn(); System.out.println("Schema arn: " + schemaArn); return schemaArn; } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

Create a schema with a domain.

public static String createDomainSchema(PersonalizeClient personalizeClient, String schemaName, String domain, String filePath) { String schema = null; try { schema = new String(Files.readAllBytes(Paths.get(filePath))); } catch (IOException e) { System.out.println(e.getMessage()); } try { CreateSchemaRequest createSchemaRequest = CreateSchemaRequest.builder() .name(schemaName) .domain(domain) .schema(schema) .build(); String schemaArn = personalizeClient.createSchema(createSchemaRequest).schemaArn(); System.out.println("Schema arn: " + schemaArn); return schemaArn; } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • For API details, see CreateSchema in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateSolution.

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 Amazon Code Examples Repository.

public static String createPersonalizeSolution(PersonalizeClient personalizeClient, String datasetGroupArn, String solutionName, String recipeArn) { try { CreateSolutionRequest solutionRequest = CreateSolutionRequest.builder() .name(solutionName) .datasetGroupArn(datasetGroupArn) .recipeArn(recipeArn) .build(); CreateSolutionResponse solutionResponse = personalizeClient.createSolution(solutionRequest); return solutionResponse.solutionArn(); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • For API details, see CreateSolution in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use CreateSolutionVersion.

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 Amazon Code Examples Repository.

public static String createPersonalizeSolutionVersion(PersonalizeClient personalizeClient, String solutionArn) { long maxTime = 0; long waitInMilliseconds = 30 * 1000; // 30 seconds String solutionStatus = ""; String solutionVersionStatus = ""; String solutionVersionArn = ""; try { DescribeSolutionRequest describeSolutionRequest = DescribeSolutionRequest.builder() .solutionArn(solutionArn) .build(); maxTime = Instant.now().getEpochSecond() + 3 * 60 * 60; // Wait until solution is active. while (Instant.now().getEpochSecond() < maxTime) { solutionStatus = personalizeClient.describeSolution(describeSolutionRequest).solution().status(); System.out.println("Solution status: " + solutionStatus); if (solutionStatus.equals("ACTIVE") || solutionStatus.equals("CREATE FAILED")) { break; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } if (solutionStatus.equals("ACTIVE")) { CreateSolutionVersionRequest createSolutionVersionRequest = CreateSolutionVersionRequest.builder() .solutionArn(solutionArn) .build(); CreateSolutionVersionResponse createSolutionVersionResponse = personalizeClient .createSolutionVersion(createSolutionVersionRequest); solutionVersionArn = createSolutionVersionResponse.solutionVersionArn(); System.out.println("Solution version ARN: " + solutionVersionArn); DescribeSolutionVersionRequest describeSolutionVersionRequest = DescribeSolutionVersionRequest.builder() .solutionVersionArn(solutionVersionArn) .build(); while (Instant.now().getEpochSecond() < maxTime) { solutionVersionStatus = personalizeClient.describeSolutionVersion(describeSolutionVersionRequest) .solutionVersion().status(); System.out.println("Solution version status: " + solutionVersionStatus); if (solutionVersionStatus.equals("ACTIVE") || solutionVersionStatus.equals("CREATE FAILED")) { break; } try { Thread.sleep(waitInMilliseconds); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } return solutionVersionArn; } } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

The following code example shows how to use DeleteCampaign.

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 Amazon Code Examples Repository.

public static void deleteSpecificCampaign(PersonalizeClient personalizeClient, String campaignArn) { try { DeleteCampaignRequest campaignRequest = DeleteCampaignRequest.builder() .campaignArn(campaignArn) .build(); personalizeClient.deleteCampaign(campaignRequest); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see DeleteCampaign in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DeleteEventTracker.

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 Amazon Code Examples Repository.

public static void deleteEventTracker(PersonalizeClient personalizeClient, String eventTrackerArn) { try { DeleteEventTrackerRequest deleteEventTrackerRequest = DeleteEventTrackerRequest.builder() .eventTrackerArn(eventTrackerArn) .build(); int status = personalizeClient.deleteEventTracker(deleteEventTrackerRequest).sdkHttpResponse().statusCode(); System.out.println("Status code:" + status); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to use DeleteSolution.

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 Amazon Code Examples Repository.

public static void deleteGivenSolution(PersonalizeClient personalizeClient, String solutionArn) { try { DeleteSolutionRequest solutionRequest = DeleteSolutionRequest.builder() .solutionArn(solutionArn) .build(); personalizeClient.deleteSolution(solutionRequest); System.out.println("Done"); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see DeleteSolution in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DescribeCampaign.

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 Amazon Code Examples Repository.

public static void describeSpecificCampaign(PersonalizeClient personalizeClient, String campaignArn) { try { DescribeCampaignRequest campaignRequest = DescribeCampaignRequest.builder() .campaignArn(campaignArn) .build(); DescribeCampaignResponse campaignResponse = personalizeClient.describeCampaign(campaignRequest); Campaign myCampaign = campaignResponse.campaign(); System.out.println("The Campaign name is " + myCampaign.name()); System.out.println("The Campaign status is " + myCampaign.status()); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see DescribeCampaign in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DescribeRecipe.

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 Amazon Code Examples Repository.

public static void describeSpecificRecipe(PersonalizeClient personalizeClient, String recipeArn) { try { DescribeRecipeRequest recipeRequest = DescribeRecipeRequest.builder() .recipeArn(recipeArn) .build(); DescribeRecipeResponse recipeResponse = personalizeClient.describeRecipe(recipeRequest); System.out.println("The recipe name is " + recipeResponse.recipe().name()); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see DescribeRecipe in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use DescribeSolution.

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 Amazon Code Examples Repository.

public static void describeSpecificSolution(PersonalizeClient personalizeClient, String solutionArn) { try { DescribeSolutionRequest solutionRequest = DescribeSolutionRequest.builder() .solutionArn(solutionArn) .build(); DescribeSolutionResponse response = personalizeClient.describeSolution(solutionRequest); System.out.println("The Solution name is " + response.solution().name()); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see DescribeSolution in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use ListCampaigns.

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 Amazon Code Examples Repository.

public static void listAllCampaigns(PersonalizeClient personalizeClient, String solutionArn) { try { ListCampaignsRequest campaignsRequest = ListCampaignsRequest.builder() .maxResults(10) .solutionArn(solutionArn) .build(); ListCampaignsResponse response = personalizeClient.listCampaigns(campaignsRequest); List<CampaignSummary> campaigns = response.campaigns(); for (CampaignSummary campaign : campaigns) { System.out.println("Campaign name is : " + campaign.name()); System.out.println("Campaign ARN is : " + campaign.campaignArn()); } } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see ListCampaigns in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use ListDatasetGroups.

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 Amazon Code Examples Repository.

public static void listDSGroups(PersonalizeClient personalizeClient) { try { ListDatasetGroupsRequest groupsRequest = ListDatasetGroupsRequest.builder() .maxResults(15) .build(); ListDatasetGroupsResponse groupsResponse = personalizeClient.listDatasetGroups(groupsRequest); List<DatasetGroupSummary> groups = groupsResponse.datasetGroups(); for (DatasetGroupSummary group : groups) { System.out.println("The DataSet name is : " + group.name()); System.out.println("The DataSet ARN is : " + group.datasetGroupArn()); } } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

The following code example shows how to use ListRecipes.

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 Amazon Code Examples Repository.

public static void listAllRecipes(PersonalizeClient personalizeClient) { try { ListRecipesRequest recipesRequest = ListRecipesRequest.builder() .maxResults(15) .build(); ListRecipesResponse response = personalizeClient.listRecipes(recipesRequest); List<RecipeSummary> recipes = response.recipes(); for (RecipeSummary recipe : recipes) { System.out.println("The recipe ARN is: " + recipe.recipeArn()); System.out.println("The recipe name is: " + recipe.name()); } } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see ListRecipes in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use ListSolutions.

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 Amazon Code Examples Repository.

public static void listAllSolutions(PersonalizeClient personalizeClient, String datasetGroupArn) { try { ListSolutionsRequest solutionsRequest = ListSolutionsRequest.builder() .maxResults(10) .datasetGroupArn(datasetGroupArn) .build(); ListSolutionsResponse response = personalizeClient.listSolutions(solutionsRequest); List<SolutionSummary> solutions = response.solutions(); for (SolutionSummary solution : solutions) { System.out.println("The solution ARN is: " + solution.solutionArn()); System.out.println("The solution name is: " + solution.name()); } } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see ListSolutions in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use UpdateCampaign.

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 Amazon Code Examples Repository.

public static String updateCampaign(PersonalizeClient personalizeClient, String campaignArn, String solutionVersionArn, Integer minProvisionedTPS) { try { // build the updateCampaignRequest UpdateCampaignRequest updateCampaignRequest = UpdateCampaignRequest.builder() .campaignArn(campaignArn) .solutionVersionArn(solutionVersionArn) .minProvisionedTPS(minProvisionedTPS) .build(); // update the campaign personalizeClient.updateCampaign(updateCampaignRequest); DescribeCampaignRequest campaignRequest = DescribeCampaignRequest.builder() .campaignArn(campaignArn) .build(); DescribeCampaignResponse campaignResponse = personalizeClient.describeCampaign(campaignRequest); Campaign updatedCampaign = campaignResponse.campaign(); System.out.println("The Campaign status is " + updatedCampaign.status()); return updatedCampaign.status(); } catch (PersonalizeException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • For API details, see UpdateCampaign in Amazon SDK for Java 2.x API Reference.