提供一些管控操作的方法調用參考。
Maven依賴
<!--引入 searchengine sdk -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>searchengine20211025</artifactId>
<version>1.0.0</version>
</dependency>
<!--引入 aliyun 核心 sdk -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.20</version>
</dependency>
<!-- 召回引擎版 sdk -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-ha3engine</artifactId>
<version>1.1.0</version>
</dependency>
代碼示例
import java.util.HashMap;
import java.util.Map;
import com.aliyun.searchengine20211025.Client;
import com.aliyun.searchengine20211025.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class DataSourceService {
private Gson gson;
/**
* 阿里云原生client,優點是靈活
*/
private IAcsClient aliyunClient;
/**
* 召回引擎版client,暫時支持查詢操作
*/
private Client client;
/**
* 請填寫自己真實的實例ID
*/
private String instanceId = "ha-cn-xxxxx";
@Before
public void setUp() throws Exception {
gson = new GsonBuilder().setPrettyPrinting().create();
/**
* 初始化召回引擎版client
*/
Map<String, String> configMaps = new HashMap<String, String>(){{
put("accessKeyId", ""); //請輸入AK
put("accessKeySecret", ""); //請輸入secret
put("regionId", "cn-hangzhou"); // 請輸入區域信息
put("endpoint", "searchengine.cn-hangzhou.aliyuncs.com"); // 請輸入區域對應的域名
}};
client = new Client(Config.build(configMaps));
/**
* 初始化阿里云通用client
*/
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", // 請輸入區域信息
"", // 請輸入AK
""); // 請輸入secret
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<YOUR-REGION-ID>", // The region ID
"<YOUR-ACCESS-KEY-ID>", // The AccessKey ID of the RAM account
"<YOUR-ACCESS-KEY-SECRET>", // The AccessKey Secret of the RAM account
"<YOUR-STS-TOKEN>"); // STS Token
**/
aliyunClient = new DefaultAcsClient(profile);
}
@After
public void tearDown() {
}
@Test
public void ListClusters() throws Exception {
ListClustersResponse response = client.listClusters(instanceId);
System.out.println(gson.toJson(response.getBody().getResult()));
}
@Test
public void ListDataSources() throws Exception {
ListDataSourcesResponse response = client.listDataSources(instanceId);
System.out.println(gson.toJson(response.getBody().getResult()));
}
@Test
public void GetDataSourceByAliyun() {
String dataSourceName = instanceId + "_test_api1";
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.GET);
request.setSysDomain("searchengine.cn-hangzhou.aliyuncs.com"); // 請輸入區域對應的域名
request.setSysVersion("2021-10-25");
request.setSysUriPattern("/openapi/ha3/instances/"+instanceId+"/data-sources/"+dataSourceName);
request.putHeadParameter("Content-Type", "application/json");
try {
CommonResponse response = aliyunClient.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("errorCode:" + e.getErrCode() + " msg:" + e.getErrMsg());
e.printStackTrace();
}
}
/**
* 創建API數據源,數據源的名稱必須以 $instanceId_ 開頭
*/
@Test
public void CreateApiDataSource() {
String content = "{\"name\":\"" +instanceId+ "_test_api1\",\"type\":\"swift\"}";
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("searchengine.cn-hangzhou.aliyuncs.com"); // 請輸入區域對應的域名
request.setSysVersion("2021-10-25");
request.setSysUriPattern("/openapi/ha3/instances/"+instanceId+"/data-sources");
request.putHeadParameter("Content-Type", "application/json");
request.setHttpContent(content.getBytes(), "UTF-8", FormatType.JSON);
try {
CommonResponse response = aliyunClient.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("errorCode:" + e.getErrCode() + " msg:" + e.getErrMsg());
e.printStackTrace();
}
}
/**
* 創建ODPS數據源,數據源的名稱必須以 $instanceId_ 開頭
*/
@Test
public void CreateOdpsDataSource() {
String content = "{\"name\":\"" + instanceId + "_test_odps1\",\"type\":\"odps\",\"config\":{\"accessKey\":\"xxx\",\"accessSecret\":\"xxxs\",\"project\":\"xxxx\",\"table\":\"xxx\",\"partition\":\"ds=20211105194848\"},\"autoBuildIndex\":false}";
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("searchengine.cn-hangzhou.aliyuncs.com"); // 請輸入區域對應的域名
request.setSysVersion("2021-10-25");
request.setSysUriPattern("/openapi/ha3/instances/"+instanceId+"/data-sources");
request.putHeadParameter("Content-Type", "application/json");
request.setHttpContent(content.getBytes(), "UTF-8", FormatType.JSON);
try {
CommonResponse response = aliyunClient.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("errorCode:" + e.getErrCode() + " msg:" + e.getErrMsg());
e.printStackTrace();
}
}
}
文檔內容是否對您有幫助?