亚马逊使用软件开发工具包进行个性化设置的示例 JavaScript V3 - AWS SDK for JavaScript

帮助我们改进AWS SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub.

这些区域有:AWS SDK for JavaScriptV3 API 参考指南详细描述了所有的 API 操作AWS SDK for JavaScript版本 3 (V3)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

亚马逊使用软件开发工具包进行个性化设置的示例 JavaScript V3

以下代码示例显示如何使用AWS SDK for JavaScript使用Amazon Personalize 的 V3。

操作代码节选,展示如何调用具体的 Amazon Personalize 函数。

方案展示如何通过调用多个 Amazon Personalize Persion 函数来完成特定任务的代码示例。

每个示例都包含一个指向以下内容的链接 GitHub其中包含了有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示如何创建 Amazon Personalize 批处理接口任务。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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: { /* required */ s3DataSource: { /* required */ path: 'INPUT_PATH', /* required */ // kmsKeyArn: 'INPUT_KMS_KEY_ARN' /* optional */' } }, jobOutput: { /* required */ s3DataDestination: { /* required */ path: 'OUTPUT_PATH', /* required */ // kmsKeyArn: 'OUTPUT_KMS_KEY_ARN' /* optional */' } }, roleArn: 'ROLE_ARN', /* required */ solutionVersionArn: 'SOLUTION_VERSION_ARN', /* required */ numResults: 20 /* optional integer*/ }; 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();

以下代码示例显示如何创建 Amazon Personalize 批量段任务。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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: { /* required */ s3DataSource: { /* required */ path: 'INPUT_PATH', /* required */ // kmsKeyArn: 'INPUT_KMS_KEY_ARN' /* optional */' } }, jobOutput: { /* required */ s3DataDestination: { /* required */ path: 'OUTPUT_PATH', /* required */ // kmsKeyArn: 'OUTPUT_KMS_KEY_ARN' /* optional */' } }, roleArn: 'ROLE_ARN', /* required */ solutionVersionArn: 'SOLUTION_VERSION_ARN', /* required */ numResults: 20 /* optional */ }; 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();

以下代码示例显示如何创建Amazon Personalize 广告系列。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();
  • 有关详细信息,请参阅。CreateCampaignAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建 Amazon Personalize 数据集。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();
  • 有关详细信息,请参阅。CreateDatasetAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建 Amazon Personalize 数据集导出任务。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();

以下代码示例显示如何创建 Amazon Personalize 数据集组。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();

以下代码示例显示如何创建 Amazon Personalize 数据集导入任务。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();

以下代码示例显示如何创建 Amazon Personalize 域架构。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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 'fs'; let 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();
  • 有关详细信息,请参阅。CreateSchemaAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建 Amazon Personalize 筛选条件。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();
  • 有关详细信息,请参阅。CreateFilterAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建Amazon Personalize 推荐人。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();
  • 有关详细信息,请参阅。CreateRecommenderAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建 Amazon Personalize 架构。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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 'fs'; let 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();
  • 有关详细信息,请参阅。CreateSchemaAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建Amazon Personalize 解决方案。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();
  • 有关详细信息,请参阅。CreateSolutionAWS SDK for JavaScriptAPI 参考.

以下代码示例显示如何创建Amazon Personalize 解决方案。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();

以下代码示例显示如何创建Amazon Personalize 事件跟踪器。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// 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();

以下代码示例显示如何将实时互动事件数据导入 Amazon Pervication 中。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

// Get service clients module and commands using ES6 syntax. import { PutItemsCommand } from "@aws-sdk/client-personalize-events"; import { personalizeEventsClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeEventsClient = new PersonalizeEventsClient({ region: "REGION"}); // Set the put items parameters. For string properties and values, use the \ character to escape quotes. var putItemsParam = { datasetArn: 'DATASET_ARN', /* required */ items: [ /* required */ { 'itemId': 'ITEM_ID', /* required */ 'properties': "{\"PROPERTY1_NAME\": \"PROPERTY1_VALUE\", \"PROPERTY2_NAME\": \"PROPERTY2_VALUE\", \"PROPERTY3_NAME\": \"PROPERTY3_VALUE\"}" /* optional */ } ] }; export const run = async () => { try { const response = await personalizeEventsClient.send(new PutItemsCommand(putItemsParam)); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 有关详细信息,请参阅。PutItemsAWS SDK for JavaScriptAPI 参考.