Data Collection SDK
Overview of SDK features
This class is used to push data collection documents.
It manages document pushes for data collection in search applications, including pushing single documents and pushing documents in batches.
Python SDK Behavioral Data Push Demo
Go SDK Behavioral Data Push Demo
C# SDK Behavioral Data Push Demo
Class name: DataCollectionClient
Namespace: OpenSearch\Client
Description
Constructor method.
Interface definition
voidOpenSearch\Client\DataCollectionClient::__construct(\OpenSearch\Client\OpenSearchClient $openSearchClient)Parameters
Parameter | Type | Description |
$openSearchClient | OpenSearch\Client\OpenSearchClient | The base class. It calculates signatures, interacts with the server-side, and returns results. |
add
Description
Adds a document.
This document is only added to the SDK client buffer and is not committed to the server-side. The document is committed to the server-side only when you call the `commit` method. You can call `add` multiple times and then call `commit()` to submit all documents at once.
Interface Definition
\OpenSearch\Generated\Common\OpenSearchResult OpenSearch\Client\DataCollectionClient::add(array $fields)Parameters
Parameter | Type | Description |
$fields | array | All fields of a document for behavioral data, user data, or item data. For example: `array("user_id" => "1021468", "bhv_type" => "click")`. |
commit
Description
Pushes the documents in the SDK client buffer to the server-side.
The buffer is cleared before the documents are sent. If the server-side returns an error and you need to retry the operation, you must regenerate the documents and call `commit` again. This prevents potential data loss.
Interface definition
\OpenSearch\Generated\Common\OpenSearchResult OpenSearch\Client\DataCollectionClient::commit(string $searchAppName,string $dataCollectionName,string $dataCollectionType)Parameters
Parameter | Type | Description |
$searchAppName | string | The name of the associated search application. |
$dataCollectionName | string | The name of the data collection. The console returns this name when you enable the feature. |
$dataCollectionType | string | Data collection type: BEHAVIOR |
push
Description
Pushes a batch of documents.
This operation synchronously sends documents to the server-side.
Interface definition
\OpenSearch\Generated\Common\OpenSearchResult OpenSearch\Client\DataCollectionClient::push(string $docJson,string $searchAppName,string $dataCollectionName,string $dataCollectionType)Parameters
Parameter | Type | Description |
$docJson | string | A list of documents in JSON format. |
$searchAppName | string | The name of the associated search application. |
$dataCollectionName | string | The name of the data collection. The console returns this name when you enable the feature. |
$dataCollectionType | string | The type of data collection. Set this to `BEHAVIOR`. |
PHP demo: Push data using Push
<?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" => [
// Unique user ID.
"user_id" => "1120021255",
// A numeric ID used on the business side to distinguish different services. It corresponds to an OpenSearch application.
"biz_id" => 1365378,
// The value of request_id returned in the search results. Return it as is.
"rn" => "156516585419723283227314",
// If the result is from OpenSearch, set this field to Alibaba.
"trace_id" => "Alibaba",
// The value of ops_request_misc returned in the search results. Return it as is.
"trace_info" => "%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D",
// The primary key value of the primary table in the OpenSearch application.
"item_id" => "2223",
// The item type, such as an item or a product.
"item_type" => "goods",
// Click-through behavioral data.
"bhv_type" => "click",
// The UNIX timestamp in seconds when the behavior occurred.
"bhv_time" => "1566475047"
]
]
));
// Create a DataCollectionClient object with an OpenSearchClient object as the constructor parameter.
$dataCollectionClient = new DataCollectionClient($client);
$ret = $dataCollectionClient->push($docs, $searchAppName, $dataCollectionName, $dataCollectionType);
print_r(json_decode($ret->result, true));PHP demo: Pushing data using Commit
<?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";
// Create a DataCollectionClient object with an OpenSearchClient object as the constructor parameter.
$dataCollectionClient = new DataCollectionClient($client);
// Add a document.
// This document is only added to the SDK client buffer. It is not committed to the server-side. The document is committed to the server-side only when you call the commit method.
// Call add multiple times and then call commit() to submit all documents at once.
$dataCollectionClient->add([
// Unique user ID.
"user_id" => "1120021255",
// A numeric ID used on the business side to distinguish different services. It corresponds to an OpenSearch application.
"biz_id" => 1365378,
// The value of request_id returned in the search results. Return it as is.
"rn" => "156516585419723283227314",
// If the result is from OpenSearch, set this field to Alibaba.
"trace_id" => "Alibaba",
// The value of ops_request_misc returned in the search results. Return it as is.
"trace_info" => "%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D",
// The primary key value of the primary table in the OpenSearch application.
"item_id" => "2223",
// The item type, such as an item or a product.
"item_type" => "goods",
// Click-through behavioral data.
"bhv_type" => "click",
// The UNIX timestamp in seconds when the behavior occurred.
"bhv_time" => "1566475047"
]);
$ret = $dataCollectionClient->commit($searchAppName, $dataCollectionName, $dataCollectionType);
print_r(json_decode($ret->result, true));Java Demo: Push Data Collection
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) {
// Create and construct an OpenSearch object.
OpenSearch opensearch = new OpenSearch(accesskey, secret, host);
// Create an OpenSearchClient object with the OpenSearch object as the constructor parameter.
OpenSearchClient client = new OpenSearchClient(opensearch);
// Create a DataCollectionClient object with the OpenSearchClient object as the constructor parameter.
DataCollectionClient dataCollectionClient = new DataCollectionClient(client);
// Push the document directly.
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;
}
}
}Java demo: Pushing data using the Commit method
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) {
// Create and construct an OpenSearch object.
OpenSearch opensearch = new OpenSearch(accesskey, secret, host);
// Create an OpenSearchClient object with the OpenSearch object as the constructor parameter.
OpenSearchClient client = new OpenSearchClient(opensearch);
// Create a DataCollectionClient object with the OpenSearchClient object as the constructor parameter.
DataCollectionClient dataCollectionClient = new DataCollectionClient(client);
Map<String, Object> fields = new HashMap<String, Object>();
// Unique user ID.
fields.put("user_id", "1120021255");
// A numeric ID used on the business side to distinguish different services. It corresponds to an OpenSearch application.
fields.put("biz_id", 1365378);
// The value of request_id returned in the search results. Return it as is.
fields.put("rn", "1564455556323223680397827");
// If the result is from OpenSearch, set this field to Alibaba.
fields.put("trace_id", "Alibaba");
// The value of ops_request_misc returned in the search results. Return it as is.
fields.put("trace_info", "%7B%22request%5Fid%22%3A%22156516585419723283227314%22%2C%22scm%22%3A%2220140713.120006678..%22%7D");
// The primary key value of the primary table in the OpenSearch application.
fields.put("item_id", "2223");
// The item type, such as an item or a product.
fields.put("item_type", "goods");
// Click-through behavioral data.
fields.put("bhv_type", "click");
// The UNIX timestamp in seconds when the behavior occurred.
fields.put("bhv_time", "1566475047");
// Add a document.
// This document is only added to the SDK client buffer. It is not committed to the server-side. The document is committed to the server-side only when you call the commit method.
// Call add multiple times and then call commit() to submit all documents at once.
dataCollectionClient.add(fields);
try {
OpenSearchResult openSearchResult = dataCollectionClient.commit(searchAppName, dataCollectionName, dataCollectionType);
System.out.println(openSearchResult);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}