代码依赖
Package 依赖查找:https://www.npmjs.com/
dependencies
@alicloud/credentials
@alicloud/opensearch-util
@alicloud/tea-typescript
@alicloud/tea-util
devDependencies
typescript
ts-node
行为数据推送示例
import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";
// 创建 Config 配置实例
let config = new Config();
// 配置 endpoint 统一的请求入口, 此信息可从业务控制台获取.
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";
// 支持 protocol 配置网络请求协议, 默认为 'HTTP'. 支持配置 'HTTPS'/'HTTP'.
config.protocol = "HTTP";
// 支持 type 配置 sts/access_key 鉴权. 其中 type 默认为 'access_key' 鉴权. 使用 'sts' 可配置 RAM-STS 鉴权.
// 备选参数为: sts 或者 access_key
config.type = "access_key";
// 如果使用 RAM-STS 鉴权, 请配置 security_token, 可使用阿里云 AssumeRole 获取 相关 STS 鉴权结构.
config.securityToken = "";
// 配置运行时参数, 时间参数数值单位为 ms.
let runtime = new $Util.RuntimeOptions({
connectTimeout: 5000,
readTimeout: 10000,
autoretry: false,
ignoreSSL: false,
maxIdleConns: 50,
});
// 创建 opensearch Client 实例
let client = new Client(config);
// 指定 推送目标 app 名称或者版本信息.
const appName = "<appName>";
// 指定 推送App 采集名称.
const collectionName = "<collectionName>";
// --------------- 行为日志 ---------------
// item_id 信息 为 搜索结果返回的主键信息. 特定 为主键 id.
const item_id = "358713";
// ops_request_misc 为搜索请求返回的 ops_request_misc 信息.
const ops_request_misc = "%7B%22request%5Fid%22%3A%22161777635816780357273903%22%2C%22scm%22%3A%2220140713.130149759..%22%7D";
// bhv_type 为 行为事件的特征信息, 备选信息如下为类别:
// cart(加购物车)
// collect(收藏)
// like(点赞)
// comment(评论)
// buy(购买)
// click(点击/查看)
const bhv_type = "click";
// request_id 为搜索请求返回的 request_id 信息.
const request_id = "161777635816780357273903";
// 该数据到达服务端的时间,格式:时间戳,单位:秒
const reach_time = "1709708439";
// 用于唯一标识终端应用上的用户的ID.
// *一般为登录用户ID.
// *对于PC端,如果是未登录用户,也可以设置为cookieid.
const user_id = "a7a0d37c824b659f36a5b9e3b819fcdd"
let behavior_fields = {
"item_id": item_id,
"sdk_type": "opensearch_sdk",
"sdk_version": "<sdk_version>", // 当前使用的 opensearch sdk 的版本号.
"trace_id": "ALIBABA", // 用于区分调用了哪个服务商的服务
"trace_info": ops_request_misc,
"bhv_type": bhv_type,
"item_type": "item",
"rn": request_id,
"biz_id": "<biz_id>", // 手机或终端应用用于区分业务的一个数值ID,该字段可用来和OpenSearch上的应用或和AIRec上的实例做关联.
"reach_time": reach_time,
"user_id": user_id,
}
// 构造behavior_documents[]内容.
let behavior_documents = [{"cmd": "add", "fields": behavior_fields}];
try {
let pathname = `/v3/openapi/app-groups/${appName}/data-collections/${collectionName}/data-collection-type/BEHAVIOR/actions/bulk`;
let result= client._request("POST", pathname, null, null, behavior_documents, runtime);
result.then(function (result) {
console.log(result);
});
}
catch (e) {
console.log(e);
}
说明
数据采集参考:推送采集数据
该文章对您有帮助吗?