使用 SDK for JavaScript (v3) 的 Amazon Personalize 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK for JavaScript (v3) 的 Amazon Personalize 範例

下列程式碼範例示範如何使用 AWS SDK for JavaScript (v3) 搭配 Amazon Personalize 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 CreateBatchInferenceJob

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateBatchInferenceJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the batch inference job's parameters. export const createBatchInferenceJobParam = { jobName: "JOB_NAME", jobInput: { s3DataSource: { path: "INPUT_PATH", }, }, jobOutput: { s3DataDestination: { path: "OUTPUT_PATH", }, }, roleArn: "ROLE_ARN", solutionVersionArn: "SOLUTION_VERSION_ARN", numResults: 20, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateBatchInferenceJobCommand(createBatchInferenceJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

下列程式碼範例示範如何使用 CreateBatchSegmentJob

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateBatchSegmentJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the batch segment job's parameters. export const createBatchSegmentJobParam = { jobName: "NAME", jobInput: { s3DataSource: { path: "INPUT_PATH", }, }, jobOutput: { s3DataDestination: { path: "OUTPUT_PATH", }, }, roleArn: "ROLE_ARN", solutionVersionArn: "SOLUTION_VERSION_ARN", numResults: 20, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateBatchSegmentJobCommand(createBatchSegmentJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

下列程式碼範例示範如何使用 CreateCampaign

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateCampaignCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the campaign's parameters. export const createCampaignParam = { solutionVersionArn: "SOLUTION_VERSION_ARN" /* required */, name: "NAME" /* required */, minProvisionedTPS: 1 /* optional integer */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateCampaignCommand(createCampaignParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateCampaign AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateDataset

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateDatasetCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the dataset's parameters. export const createDatasetParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, datasetType: "DATASET_TYPE" /* required */, name: "NAME" /* required */, schemaArn: "SCHEMA_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetCommand(createDatasetParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateDataset AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateDatasetExportJob

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateDatasetExportJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the export job parameters. export const datasetExportJobParam = { datasetArn: "DATASET_ARN" /* required */, jobOutput: { s3DataDestination: { path: "S3_DESTINATION_PATH" /* required */, //kmsKeyArn: 'ARN' /* include if your bucket uses AWS KMS for encryption }, }, jobName: "NAME" /* required */, roleArn: "ROLE_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetExportJobCommand(datasetExportJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

下列程式碼範例示範如何使用 CreateDatasetGroup

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateDatasetGroupCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the dataset group parameters. export const createDatasetGroupParam = { name: "NAME" /* required */, }; export const run = async (createDatasetGroupParam) => { try { const response = await personalizeClient.send( new CreateDatasetGroupCommand(createDatasetGroupParam), ); console.log("Success", response); return "Run successfully"; // For unit tests. } catch (err) { console.log("Error", err); } }; run(createDatasetGroupParam);

建立網域資料集群組。

// Get service clients module and commands using ES6 syntax. import { CreateDatasetGroupCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the domain dataset group parameters. export const domainDatasetGroupParams = { name: "NAME" /* required */, domain: "DOMAIN" /* required for a domain dsg, specify ECOMMERCE or VIDEO_ON_DEMAND */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetGroupCommand(domainDatasetGroupParams), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateDatasetGroup AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateDatasetImportJob

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateDatasetImportJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the dataset import job parameters. export const datasetImportJobParam = { datasetArn: "DATASET_ARN" /* required */, dataSource: { /* required */ dataLocation: "S3_PATH", }, jobName: "NAME" /* required */, roleArn: "ROLE_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetImportJobCommand(datasetImportJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

下列程式碼範例示範如何使用 CreateEventTracker

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateEventTrackerCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the event tracker's parameters. export const createEventTrackerParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, name: "NAME" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateEventTrackerCommand(createEventTrackerParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateEventTracker AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateFilter

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateFilterCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the filter's parameters. export const createFilterParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, name: "NAME" /* required */, filterExpression: "FILTER_EXPRESSION" /*required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateFilterCommand(createFilterParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateFilter AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateRecommender

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateRecommenderCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the recommender's parameters. export const createRecommenderParam = { name: "NAME" /* required */, recipeArn: "RECIPE_ARN" /* required */, datasetGroupArn: "DATASET_GROUP_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateRecommenderCommand(createRecommenderParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateRecommender AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateSchema

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateSchemaCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); import fs from "node:fs"; const schemaFilePath = "SCHEMA_PATH"; let mySchema = ""; try { mySchema = fs.readFileSync(schemaFilePath).toString(); } catch (err) { mySchema = "TEST"; // For unit tests. } // Set the schema parameters. export const createSchemaParam = { name: "NAME" /* required */, schema: mySchema /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSchemaCommand(createSchemaParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

使用網域建立結構描述。

// Get service clients module and commands using ES6 syntax. import { CreateSchemaCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); import fs from "node:fs"; const schemaFilePath = "SCHEMA_PATH"; let mySchema = ""; try { mySchema = fs.readFileSync(schemaFilePath).toString(); } catch (err) { mySchema = "TEST"; // for unit tests. } // Set the domain schema parameters. export const createDomainSchemaParam = { name: "NAME" /* required */, schema: mySchema /* required */, domain: "DOMAIN" /* required for a domain dataset group, specify ECOMMERCE or VIDEO_ON_DEMAND */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSchemaCommand(createDomainSchemaParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateSchema AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateSolution

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateSolutionCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the solution parameters. export const createSolutionParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, recipeArn: "RECIPE_ARN" /* required */, name: "NAME" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSolutionCommand(createSolutionParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需 API 詳細資訊,請參閱 CreateSolution AWS SDK for JavaScript 參考中的 API

下列程式碼範例示範如何使用 CreateSolutionVersion

SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { CreateSolutionVersionCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the solution version parameters. export const solutionVersionParam = { solutionArn: "SOLUTION_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSolutionVersionCommand(solutionVersionParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();