全部产品

行为数据推送Demo

Package 依赖地址: https://www.nuget.org/packages

安装Package 依赖

dotnet add package AlibabaCloud.TeaUtil --version  0.1.5
dotnet add package AlibabaCloud.OpenSearchUtil --version 1.0.2
dotnet add package Aliyun.Credentials --version  1.2.1
dotnet add package Tea --version 0.4.0

示例代码

以下是C# SDK 的示例代码:

using System;
using System.Collections.Generic;
using AlibabaCloud.TeaUtil.Models;
using Tea;

namespace ConsoleApp2
{
    internal class Program
    {
        public static Dictionary<string, object> behaviorBulk(Client opensearchClient, string appName,
            string collectionName, List<object> docContent, Dictionary<string, string> header, RuntimeOptions runTime)
        {
            string pathName = "/v3/openapi/app-groups/" + appName + "/data-collections/" + collectionName +
                              "/data-collection-type/BEHAVIOR/actions/bulk";
            try
            {
                return opensearchClient._request("POST", pathName, null, header, docContent, runTime);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }


        private static void Main(string[] args)
        {
            var runtime = new RuntimeOptions
            {
                ConnectTimeout = 5000,
                ReadTimeout = 10000,
                MaxAttempts = 0,
                Autoretry = false,
                IgnoreSSL = false,
                MaxIdleConns = 50
            };

            var config = new Config
            {
                // 配置统一的请求入口, 此信息 可从 业务控制台获取.
                Endpoint = "opensearch-cn-hangzhou.aliyuncs.com",

                // 支持 protocol 配置 HTTPS/HTTP
                Protocol = "HTTPS",

                // 用户识别信息
                AccessKeyId = "<access_key_id>",
                AccessKeySecret = "<access_key_secret>",

                // 支持 type 配置 sts/access_key 鉴权. 其中 type 默认为 access_key 鉴权. 使用 sts 可配置 RAM-STS 鉴权.
                // 备选参数为:  sts 或者 access_key
                Type = "access_key",

                // 如果使用 RAM-STS 鉴权, 请配置 security_token, 可使用阿里云 AssumeRole 获取 相关 STS 鉴权结构.
                SecurityToken = "",
            };

            // 创建 opensearch client 实例
            var openSearch = new Client(config);

            // 要搜索的appName.
            var appName = "appName";

            // 自定义HttpHeaders .
            var header = new Dictionary<string, string>();

            // ---------------  行为日志 ---------------
            // 文档参考 https://help.aliyun.com/document_detail/193165.html

            // item_id  信息 为 搜索结果返回的主键信息. 特定 为主键 id.
            string item_id = "358713";

            // ops_request_misc 为搜索请求返回的 ops_request_misc 信息.
            string 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(点击/查看)
            string bhv_type = "click";

            // request_id 为搜索请求返回的 request_id 信息.
            string request_id = "161777635816780357273903";

            // 到达日志服务端的时间,格式:yyyyMMddHHmmss
            string reach_time = "20210405111111";

            // 用于唯一标识终端应用上的用户的ID。
            // *一般为登录用户ID。
            // *对于PC端,如果是未登录用户,也可以设置为cookieid
            string user_id = "a7a0d37c824b659f36a5b9e3b819fcdd";

            List<object> behaviorDocs = new List<object>();
            Dictionary<string, object> behaviorDoc = new Dictionary<string, object>();

            Dictionary<string, object> behaviorDocFields = new Dictionary<string, object>()
            {
                {"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}
            };

            behaviorDoc.Add("cmd", "ADD");
            behaviorDoc.Add("fields", behaviorDocFields);

            behaviorDocs.Add(behaviorDoc);
            try
            {
                behaviorBulk(openSearch, appName, appName, behaviorDocs, header, runtime);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}