通过召回引擎版 Java SDK 向实例批量推送数据。高可用版本支持批量更新已有文档的指定属性字段。
add 操作示例
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author alibaba
*/
public class PushDoc {
/**
* 问天引擎client
*/
private Client client;
@Before
public void clientInit() throws Exception {
Config config = new Config();
// 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
config.setInstanceId("ha-cn-i7*****605");
// 用户名,可在实例详情页>API入口 查看
config.setAccessUserName("username");
// 密码,可在实例详情页>API入口 修改
config.setAccessPassWord("password");
// API域名,可在实例详情页>API入口 查看
config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
client = new Client(config);
}
@Test
public void add() throws Exception {
// 文档推送的表名称
String tableName = "<table_name>";
// 文档推送的文档主键字段.
String pkField = "<field_pk>";
try {
// 文档推送外层结构, 可添加对文档操作的结构体. 结构内支持一个或多个文档操作内容.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// 添加文档
Map<String, Object> add2Document = new HashMap<>();
Map<String, Object> add2DocumentFields = new HashMap<>();
// 插入文档内容信息, keyValue 成对匹配.
// field_pk 字段需与 pkField 字段配置一致.
add2DocumentFields.put("<field_pk>", "<field_pk_value>");
add2DocumentFields.put("<field_map_key_1>", "<field_map_value_1>");
add2DocumentFields.put("<field_map_key_2>", "<field_map_value_2>");
// 问天引擎支持的多值属性类型,索引内配置为"multi_value": true
ArrayList<Object> addDocumentMultiFields = new ArrayList<>();
addDocumentMultiFields.add("multi_value_1");
addDocumentMultiFields.add("multi_value_2");
add2DocumentFields.put("<multi_value_key>", addDocumentMultiFields);
// 将文档内容添如 add2Document 结构.
add2Document.put("fields", add2DocumentFields);
// 新增对应的文档命令: add
add2Document.put("cmd", "add");
documents.add(add2Document);
// 推送数据
PushDocumentsRequestModel requestModel = new PushDocumentsRequestModel();
// 推送数据时,默认校验是否存在主键字段。如需关闭校验,请设置请求头 X-Opensearch-Validate-Data: false
// requestModel.headers = new HashMap<>();
// requestModel.headers.put("X-Opensearch-Validate-Data", "false");
requestModel.setBody(documents);
PushDocumentsResponseModel responseModel = client.pushDocuments(tableName, pkField, requestModel);
String responseBody = responseModel.getBody();
System.out.println("result:" + responseBody);
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
结构展示:
[
{
"cmd": "add",
"fields": {
"id": "1",
"title": "This is the title",
"body": "This is the body",
"tags" : [1, 2, 3]
}
}
]
update_field 操作示例
此示例适用于高可用版本。假设数据表的主键为 id,stock 为 INT32 类型的属性字段,且主键为 1 和 2 的文档已存在。推送后,查询两条文档以确认字段值已更新。
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.Config;
import com.aliyun.ha3engine.models.PushDocumentsRequestModel;
import com.aliyun.ha3engine.models.PushDocumentsResponseModel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class UpdateFields {
public static void main(String[] args) throws Exception {
Config config = new Config()
.setEndpoint("<instance_services_domain>")
.setInstanceId("<instance_id>")
.setProtocol("http")
.setAccessUserName("<user_name>")
.setAccessPassWord("<user_password>");
Client client = new Client(config);
PushDocumentsRequestModel request = new PushDocumentsRequestModel()
.setBody(Arrays.asList(stockUpdate("1", 12), stockUpdate("2", 18)));
PushDocumentsResponseModel response =
client.pushDocuments("<table_name>", "id", request);
System.out.println(response.getBody());
}
private static Map<String, ?> stockUpdate(String id, int stock) {
Map<String, Object> fields = new HashMap<>();
fields.put("id", id);
fields.put("stock", stock);
Map<String, Object> document = new HashMap<>();
document.put("cmd", "update_field");
document.put("fields", fields);
return document;
}
}
delete 操作示例
package com.aliyun.ha3engine;
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author alibaba
*/
public class PushDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// API域名,可在实例详情页>API入口 查看
config.setEndpoint("<instance_services_domain>");
// 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
config.setInstanceId("<instance_id>");
// 用户名,可在实例详情页>网络信息 查看
config.setAccessUserName("<user_name>");
// 密码,可在实例详情页>网络信息 修改
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// 文档推送的数据源配置名称.可在实例管理>配置中心>数据源配置 查看
String tableName = "<instance_datasource_table_name>";
// 文档推送的文档主键字段.
String pkField = "<field_pk>";
try {
// 文档推送外层结构, 可添加对文档操作的结构体. 结构内支持一个或多个文档操作内容.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// 删除文档
Map<String, Object> delete2Document = new HashMap<>();
Map<String, Object> delete2DocumentFields = new HashMap<>();
// 插入文档内容信息, keyValue 成对匹配.
// field_pk 字段需与 pkField 字段配置一致.
delete2DocumentFields.put("<field_pk>", "<field_pk_value>");
// 将文档内容添如 delete2Document 结构.
delete2Document.put("fields", delete2DocumentFields);
// 删除对应的文档命令: delete
delete2Document.put("cmd", "delete");
documents.add(delete2Document);
// 推送数据
PushDocumentsRequestModel requestModel = new PushDocumentsRequestModel();
requestModel.setBody(documents);
PushDocumentsResponseModel responseModel = client.pushDocuments(tableName, pkField, requestModel);
String responseBody = responseModel.getBody();
System.out.println("result:" + responseBody);
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}
结构展示:
[
{
"cmd":"delete",
"fields":{
"id":"3"
}
}
]