數(shù)據(jù)采集2.0 SDK
數(shù)據(jù)采集SDK
SDK功能簡介
數(shù)據(jù)采集文檔推送類。
管理搜索應(yīng)用的數(shù)據(jù)采集文檔推送,包含單條推送文檔、批量推送文檔等。
Python SDK 行為數(shù)據(jù)推送Demo
類名:DataCollectionClient
命名空間:OpenSearch\Client
接口描述
構(gòu)造方法。
接口定義
voidOpenSearch\Client\DataCollectionClient::__construct(\OpenSearch\Client\OpenSearchClient $openSearchClient)
參數(shù)描述
參數(shù)名稱 | 類型 | 描述 |
$openSearchClient | OpenSearch\Client\OpenSearchClient | 基礎(chǔ)類,負(fù)責(zé)計算簽名,和服務(wù)端進(jìn)行交互和返回結(jié)果。 |
add
接口描述
增加一條文檔。
Note: 這條文檔只是增加到 SDK Client buffer 中,沒有正式提交到服務(wù)端;只有調(diào)用了 commit 方法才會被提交到服務(wù)端。 你可以多次 add 然后調(diào)用 commit() 統(tǒng)一提交。
接口定義
\OpenSearch\Generated\Common\OpenSearchResult OpenSearch\Client\DataCollectionClient::add(array $fields)
參數(shù)描述
參數(shù)名稱 | 類型 | 描述 |
$fields | array | 一條行為數(shù)據(jù)(或用戶數(shù)據(jù)、物品數(shù)據(jù))文檔的所有字段,例如array(“user_id” => “1021468”, “bhv_type” => “click”); |
commit
接口描述
把 SDK Client buffer 中的文檔發(fā)布到服務(wù)端。
Note: 在發(fā)送之前會把 buffer 中的文檔清空,所以如果服務(wù)端返回錯誤需要重試的情況下,需要重新生成文檔并 commit,避免丟數(shù)據(jù)的可能。
接口定義
\OpenSearch\Generated\Common\OpenSearchResult OpenSearch\Client\DataCollectionClient::commit(string $searchAppName,string $dataCollectionName,string $dataCollectionType)
參數(shù)描述
參數(shù)名稱 | 類型 | 描述 |
$searchAppName | string | 關(guān)聯(lián)的搜索應(yīng)用名 |
$dataCollectionName | string | 數(shù)據(jù)采集名稱,開通時控制臺會返回該名稱 |
$dataCollectionType | string | 數(shù)據(jù)采集類型:BEHAVIOR |
push
接口描述
批量推送文檔。
Note: 此操作會同步發(fā)送文檔到服務(wù)端。
接口定義
\OpenSearch\Generated\Common\OpenSearchResult OpenSearch\Client\DataCollectionClient::push(string $docJson,string $searchAppName,string $dataCollectionName,string $dataCollectionType)
參數(shù)描述
參數(shù)名稱 | 類型 | 描述 |
$docJson | string | 文檔 list,為 JSON 格式 |
$searchAppName | string | 關(guān)聯(lián)的搜索應(yīng)用名 |
$dataCollectionName | string | 數(shù)據(jù)采集名稱,開通時控制臺會返回該名稱 |
$dataCollectionType | string | 數(shù)據(jù)采集類型:BEHAVIOR |
Push 推送采集數(shù)據(jù) PHP Demo
<?php
require_once("Config.inc.php");
use OpenSearch\Client\DataCollectionClient;
use OpenSearch\Generated\DataCollection\Command;
$searchAppName = "opensearch_app_name";
$dataCollectionName = "opened_data_collection_name";
$dataCollectionType = "BEHAVIOR";
$docs = json_encode(array(
[
"cmd" => Command::$__names[Command::ADD],
"fields" => [
// 用戶唯一標(biāo)識
"user_id" => "1120021255",
// 業(yè)務(wù)側(cè)用于區(qū)分不同業(yè)務(wù)的數(shù)值ID 對應(yīng)一個開放搜索應(yīng)用
"biz_id" => 1365378,
// 搜索結(jié)果中返回的request_id的值,原樣回傳即可
"rn" => "156516585419723283227314",
// 如果是來自開放搜索的結(jié)果,該字段值設(shè)置為Alibaba
"trace_id" => "Alibaba",
// 搜索結(jié)果中返回的ops_request_misc的值,原樣回傳即可
"trace_info" => "%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D",
// 開放搜索應(yīng)用中主表主鍵值
"item_id" => "2223",
// item類型為 物品、商品
"item_type" => "goods",
// 點(diǎn)擊類行為數(shù)據(jù)
"bhv_type" => "click",
// 行為發(fā)生的秒級時間戳
"bhv_time" => "1566475047"
]
]
));
// 創(chuàng)建DataCollectionClient對象,并以O(shè)penSearchClient對象作為構(gòu)造參數(shù)
$dataCollectionClient = new DataCollectionClient($client);
$ret = $dataCollectionClient->push($docs, $searchAppName, $dataCollectionName, $dataCollectionType);
print_r(json_decode($ret->result, true));
Commit 推送采集數(shù)據(jù) PHP Demo
<?php
require_once("Config.inc.php");
use OpenSearch\Client\DataCollectionClient;
use OpenSearch\Generated\DataCollection\Command;
$searchAppName = "opensearch_app_name";
$dataCollectionName = "opened_data_collection_name";
$dataCollectionType = "BEHAVIOR";
// 創(chuàng)建DataCollectionClient對象,并以O(shè)penSearchClient對象作為構(gòu)造參數(shù)
$dataCollectionClient = new DataCollectionClient($client);
// 增加一條文檔
// 這條文檔只是增加到 SDK Client buffer中,沒有正式提交到服務(wù)端;只有調(diào)用了 commit 方法才會被提交到服務(wù)端。
// 可以多次調(diào)用 add,然后調(diào)用commit() 統(tǒng)一提交。
$dataCollectionClient->add([
// 用戶唯一標(biāo)識
"user_id" => "1120021255",
// 業(yè)務(wù)側(cè)用于區(qū)分不同業(yè)務(wù)的數(shù)值ID 對應(yīng)一個開放搜索應(yīng)用
"biz_id" => 1365378,
// 搜索結(jié)果中返回的request_id的值,原樣回傳即可
"rn" => "156516585419723283227314",
// 如果是來自開放搜索的結(jié)果,該字段值設(shè)置為Alibaba
"trace_id" => "Alibaba",
// 搜索結(jié)果中返回的ops_request_misc的值,原樣回傳即可
"trace_info" => "%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D",
// 開放搜索應(yīng)用中主表主鍵值
"item_id" => "2223",
// item類型為 物品、商品
"item_type" => "goods",
// 點(diǎn)擊類行為數(shù)據(jù)
"bhv_type" => "click",
// 行為發(fā)生的秒級時間戳
"bhv_time" => "1566475047"
]);
$ret = $dataCollectionClient->commit($searchAppName, $dataCollectionName, $dataCollectionType);
print_r(json_decode($ret->result, true));
Push 推送采集數(shù)據(jù) Java Demo
package com.aliyun.opensearch.demo;
import com.aliyun.opensearch.DataCollectionClient;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
public class PushDataCollectionDoc {
private static String accesskey = "your ak";
private static String secret = "your secret";
private static String host = "your host";
private static String searchAppName = "opensearch_app_name";
private static String dataCollectionName = "opened_data_collection_name";
private static String dataCollectionType = "BEHAVIOR";
public static void main(String[] args) {
//創(chuàng)建并構(gòu)造OpenSearch對象
OpenSearch opensearch = new OpenSearch(accesskey, secret, host);
//創(chuàng)建OpenSearchClient對象,并以O(shè)penSearch對象作為構(gòu)造參數(shù)
OpenSearchClient client = new OpenSearchClient(opensearch);
//創(chuàng)建DataCollectionClient對象,并以O(shè)penSearchClient對象作為構(gòu)造參數(shù)
DataCollectionClient dataCollectionClient = new DataCollectionClient(client);
// 直接推送文檔
String docJson = "[{\"cmd\":\"ADD\",\"fields\":{\"user_id\":\"1120021255\","+
"\"biz_id\":1365378,\"rn\":\"156516585419723283227314\","+
"\"trace_id\":\"Alibaba\","+
"\"trace_info\":\"%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D\","+
"\"item_id\":\"id\",\"item_type\":\"goods\","+
"\"bhv_type\":\"click\",\"bhv_time\":\"1566475047\"}}]";
try {
OpenSearchResult openSearchResult = dataCollectionClient.push(docJson,
searchAppName, dataCollectionName,
dataCollectionType);
System.out.println(openSearchResult);
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
return;
}
}
}
Commit 推送采集數(shù)據(jù) Java Demo
package com.aliyun.opensearch.demo;
import com.aliyun.opensearch.DataCollectionClient;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import java.util.HashMap;
import java.util.Map;
public class PushDataCollectionDoc {
private static String accesskey = "your ak";
private static String secret = "your secret";
private static String host = "your host";
private static String searchAppName = "opensearch_app_name";
private static String dataCollectionName = "opened_data_collection_name";
private static String dataCollectionType = "BEHAVIOR";
public static void main(String[] args) {
//創(chuàng)建并構(gòu)造OpenSearch對象
OpenSearch opensearch = new OpenSearch(accesskey, secret, host);
//創(chuàng)建OpenSearchClient對象,并以O(shè)penSearch對象作為構(gòu)造參數(shù)
OpenSearchClient client = new OpenSearchClient(opensearch);
//創(chuàng)建DataCollectionClient對象,并以O(shè)penSearchClient對象作為構(gòu)造參數(shù)
DataCollectionClient dataCollectionClient = new DataCollectionClient(client);
Map<String, Object> fields = new HashMap<String, Object>();
// 用戶唯一標(biāo)識
fields.put("user_id", "1120021255");
// 業(yè)務(wù)側(cè)用于區(qū)分不同業(yè)務(wù)的數(shù)值ID 對應(yīng)一個開放搜索應(yīng)用
fields.put("biz_id", 1365378);
// 搜索結(jié)果中返回的request_id的值,原樣回傳即可
fields.put("rn", "1564455556323223680397827");
// 如果是來自開放搜索的結(jié)果,該字段值設(shè)置為Alibaba
fields.put("trace_id", "Alibaba");
// 搜索結(jié)果中返回的ops_request_misc的值,原樣回傳即可
fields.put("trace_info", "%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D");
// 開放搜索應(yīng)用中主表主鍵值
fields.put("item_id", "2223");
// item類型為 物品、商品
fields.put("item_type", "goods");
// 點(diǎn)擊類行為數(shù)據(jù)
fields.put("bhv_type", "click");
// 行為發(fā)生的秒級時間戳
fields.put("bhv_time", "1566475047");
//增加一條文檔
//這條文檔只是增加到 SDK Client buffer中,沒有正式提交到服務(wù)端;只有調(diào)用了 commit 方法才會被提交到服務(wù)端。
//可以多次調(diào)用 add,然后調(diào)用commit() 統(tǒng)一提交。
dataCollectionClient.add(fields);
try {
OpenSearchResult openSearchResult = dataCollectionClient.commit(searchAppName, dataCollectionName, dataCollectionType);
System.out.println(openSearchResult);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}