文档

管控SDK Demo

更新时间:

提供一些管控操作的方法调用参考。

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();
        }
    }

}
  • 本页导读 (0)