全部产品

Java SDK

更新时间:2021-05-08 10:22:12

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参考创建AccessKey

  2. 将 SDK添加到项目中引入 Java SDK 依赖,通过 maven 依赖的方式将 MultiSearch 的SDK加入到自己的项目中。

    <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>videosearch20200225</artifactId>
    <version>1.1.2</version>
    </dependency>

完整代码示例

创建视频入库任务

//  设置Region以北京为例,其它Region请按实际情况填写。
String  region  ="cn-beijing";
//  阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录  https://ram.console.aliyun.com  创建RAM账号。
String  accessKeyId  ="<yourAccessKeyId>";
String  accessKeySecret  ="<yourAccessKeySecret>";

// 创建Config
Config config =newConfig();
config.regionId ="<yourRegionId>";
config.accessKeyId ="<yourAccessKeyId>";
config.accessKeySecret ="<yourAccessKeySecret>";
config.endpoint="multisearch.<regionId>.aliyuncs.com"

try{
Client client =newClient(config);

AddStorageVideoTaskRequest request =newAddStorageVideoTaskRequest();
// 实例id
        request.setInstanceId("<yourInstanceId>");
// 视频url
        request.setVideoUrl("<yourVideoUrl>");
// 视频id(业务key)
        request.setVideoId("<yourVideoId>");
// 视频描述
        request.setDescription("<yourDescription>");
// 视频标签
        request.setVideoTags("<yourVideoTag>");
// 入库完成的回调
        request.setCallbackUrl("<yourCallbackUrl>");

AddStorageVideoTaskResponse response = client.addStorageVideoTask(request);

System.out.println("requestId : "+ response.getRequestId());
System.out.println("Data : "+ JSON.toJSONString(response.getData()));
}catch(TeaException e){
System.out.println(JSON.toJSONString(e.getData()));
System.out.println(e.getCode());
System.out.println(e.getMessage());
}catch(Exception e){
        e.printStackTrace();
}

创建视频检索任务

//  设置Region以北京为例,其它Region请按实际情况填写。
String  region  ="cn-beijing";
//  阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录  https://ram.console.aliyun.com  创建RAM账号。
String  accessKeyId  ="<yourAccessKeyId>";
String  accessKeySecret  ="<yourAccessKeySecret>";

// 创建Config
Config config =newConfig();
config.regionId ="<yourRegionId>";
config.accessKeyId ="<yourAccessKeyId>";
config.accessKeySecret ="<yourAccessKeySecret>";
config.endpoint="multisearch.<regionId>.aliyuncs.com"

try{
Client client =newClient(config);


AddSearchVideoTaskRequest request =newAddSearchVideoTaskRequest();

// 实例id
        request.setInstanceId("<yourInstanceId>");
// 检索类型(1. 视频搜视频   2. 图片搜视频)
        request.setSearchType(1);
// 视频/图片的url地址
//如果需要使用 本地文件上传的功能,则用xxxFileObject参数并且使用 AddSearchXXXTaskAdvanceRequest对象
        request.setVideoUrl("<yourVideoUrl>");
// 视频/图片的描述
        request.setDescription("<yourDescription>");
// 检索标签
        request.setQueryTags("<yourQueryTags>");
// 返回结果数
        request.setReturnVideoNumber(20);
// 查询入库类型,当检索类型为1时生效(1. 直接入库  2. 去重入库  3. 不入库)
        request.setStorageType(3);
// 视频id,当searchType为1且storageType为1,2时生效
        request.setVideoId("<yourVideoId>");
// 视频标签,当searchType为1且storageType为1,2时生效
        request.setVideoTags("<yourVideoTags>");
// 去重入库阈值,searchType为1且storageType为2时生效
        request.setReplaceStorageThreshold(0.75f);
// 检索完成的回调
        request.setCallbackUrl("<yourCallbackUrl>");

AddSearchVideoTaskResponse response = client.addSearchVideoTask(request);

System.out.println("requestId : "+ response.getRequestId());
System.out.println("Data : "+ JSON.toJSONString(response.getData()));
}catch(TeaException e){
System.out.println(JSON.toJSONString(e.getData()));
System.out.println(e.getCode());
System.out.println(e.getMessage());
}catch(Exception e){
        e.printStackTrace();
}

查询任务状态

//  设置Region以北京为例,其它Region请按实际情况填写。
String  region  ="cn-beijing";
//  阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录  https://ram.console.aliyun.com  创建RAM账号。
String  accessKeyId  ="<yourAccessKeyId>";
String  accessKeySecret  ="<yourAccessKeySecret>";

// 创建Config
Config config =newConfig();
config.regionId ="<yourRegionId>";
config.accessKeyId ="<yourAccessKeyId>";
config.accessKeySecret ="<yourAccessKeySecret>";
config.endpoint="multisearch.<regionId>.aliyuncs.com"

try{
Client client =newClient(config);

GetTaskStatusRequest request =newGetTaskStatusRequest();
// 实例id
        request.setInstanceId("<yourInstanceId>");
// 任务id
        request.setTaskId("<yourTaskId>");

GetTaskStatusResponse response = client.getTaskStatus(request);

System.out.println("requestId : "+ response.getRequestId());
System.out.println("Data : "+ JSON.toJSONString(response.getData()));
System.out.println("TaskInfo : "+ JSON.toJSONString(response.getTaskInfo()));
}catch(TeaException e){
System.out.println(JSON.toJSONString(e.getData()));
System.out.println(e.getCode());
System.out.println(e.getMessage());
}catch(Exception e){
        e.printStackTrace();
}

查询入库历史

//  设置Region以北京为例,其它Region请按实际情况填写。
String  region  ="cn-beijing";
//  阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录  https://ram.console.aliyun.com  创建RAM账号。
String  accessKeyId  ="<yourAccessKeyId>";
String  accessKeySecret  ="<yourAccessKeySecret>";


// 创建Config
Config config =newConfig();
config.regionId ="<yourRegionId>";
config.accessKeyId ="<yourAccessKeyId>";
config.accessKeySecret ="<yourAccessKeySecret>";
config.endpoint="multisearch.<regionId>.aliyuncs.com"

try{
Client client =newClient(config);

GetStorageHistoryRequest request =newGetStorageHistoryRequest();
// 实例id
        request.setInstanceId("<yourInstanceId>");
// 视频id
        request.setVideoId("<yourVideoId>");
// 当前页数
        request.setPageNumber(1);
// 分页大小
        request.setPageSize(10);

GetStorageHistoryResponse response = client.getStorageHistory(request);

System.out.println("requestId : "+ response.requestId);
System.out.println("Data : "+ JSON.toJSONString(response.getData()));
}catch(TeaException e){
System.out.println(JSON.toJSONString(e.getData()));
System.out.println(e.getCode());
System.out.println(e.getMessage());
}catch(Exception e){
        e.printStackTrace();
}

删除视频

//  设置Region以北京为例,其它Region请按实际情况填写。
String  region  ="cn-beijing";
//  阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录  https://ram.console.aliyun.com  创建RAM账号。
String  accessKeyId  ="<yourAccessKeyId>";
String  accessKeySecret  ="<yourAccessKeySecret>";


// 创建Config
Config config =newConfig();
config.regionId ="<yourRegionId>";
config.accessKeyId ="<yourAccessKeyId>";
config.accessKeySecret ="<yourAccessKeySecret>";
config.endpoint="multisearch.<regionId>.aliyuncs.com"

try{
Client client =newClient(config);

AddDeletionVideoTaskRequest request =newAddDeletionVideoTaskRequest();
// 实例id
        request.setInstanceId("<yourInstanceId>");
// 视频id
        request.setVideoId("<yourVideoId>");

AddDeletionVideoTaskResponse response = client.addDeletionVideoTask(request);

System.out.println("requestId : "+ response.requestId);
System.out.println("Data : "+ response.getData());
}catch(TeaException e){
System.out.println(JSON.toJSONString(e.getData()));
System.out.println(e.getCode());
System.out.println(e.getMessage());
}catch(Exception e){
        e.printStackTrace();
}

v1.1.2 更新

1.账号,Config,Client 均为标准格式,下面不做重复说明,均已setUp()为说明

2.参数及返回值详细说明请参考HTTP调用接口文档

3.音频与视频接口调用方式基本相同,例如:视频任务检索为videoSearchRequest+videoSearchResponse,则音频接口为audioSearchRequest+audioSearchResponse, list等接口也一样,详细出入参可查看HTTP调用接口文档

public Clinet setUp() {

   //  设置Region以北京为例,其它Region请按实际情况填写。 
   String  region  ="cn-beijing";
   //  阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录  https://ram.console.aliyun.com  创建RAM账号。
   String  accessKeyId  ="<yourAccessKeyId>";
   String  accessKeySecret  ="<yourAccessKeySecret>";

   // 创建Config
   Config config =newConfig();
   config.regionId ="<yourRegionId>";
   config.accessKeyId ="<yourAccessKeyId>";
   config.accessKeySecret ="<yourAccessKeySecret>";
   config.endpoint="multisearch.<regionId>.aliyuncs.com";

   return new Client(config);
 
}

创建视频检索任务(文件方式)

public String addsearchVideoTaskForFile(AddsearchTaskParams addsearchTaskParams) throws Exception {

        Client client = setUp();

        AddSearchVideoTaskAdvanceRequest request = new AddSearchVideoTaskAdvanceRequest();
        // 视频oss url
        request.setVideoUrl(addsearchTaskParams.getVideoUrl());
        // 处理任务的实例id
        request.setInstanceId(instanceId);
        // 视频的业务key
        request.setVideoId(addsearchTaskParams.getVideoId());
        // 视频的业务标签
        request.setVideoTags(addsearchTaskParams.getVideoTags());
        // 返回视频数量
        request.setReturnVideoNumber(addsearchTaskParams.getReturnVideoNumber());
        // 查询标签
        request.setQueryTags(addsearchTaskParams.getQueryTags());
        // 检索入库类型 1为视频搜索
        request.setStorageType(addsearchTaskParams.getStorageType());
        // 检索回调
        request.setCallbackUrl(addsearchTaskParams.getCallbackUrl());
        // 覆盖入库的阈值,当searchStorageType为2(覆盖入库)时生效
        request.setReplaceStorageThreshold(addsearchTaskParams.getReplaceStorageThreshold());
        //视频描述
        request.setDescription(addsearchTaskParams.getDescription());
        // 检索类型(1. 视频搜视频   2. 图片搜视频)
        request.setSearchType(addsearchTaskParams.getSearchType());
        //上传文件流
        request.setVideoFileObject(addsearchTaskParams.getVideoFileObject());
        //设置优先级,(0.低 1.中 2.高 3.紧急)
        request.setSort(addsearchTaskParams.getSort());
        //是否需要特征文件 0.不需要 1.需要
        request.setNeedFeatureFile(addsearchTaskParams.getNeedFeatureFile());

        // 发起请求并处理应答或异常
        System.out.println(JSON.toJSON(request).toString());
        AddSearchVideoTaskResponse response = client.addSearchVideoTaskAdvance(request,runtimeOptions);
        //System.out.println(JSON.toJSONString(response.data));
        System.out.println("requestId : " + response.getRequestId());
        System.out.println("Data : " + JSON.toJSONString(response.getData()));
        return response.getData().getTaskId();
    }

创建视频入库任务

   public String addStorageVideoTaskForFile(AddStorageVideoTaskParams addStorageVideoTaskParams)throws Exception{

        Client client = setUp();
        AddStorageVideoTaskAdvanceRequest request = new AddStorageVideoTaskAdvanceRequest();
        // 处理任务的实例id
        request.setInstanceId(instanceId);
        // 视频oss url
        request.setVideoUrl(addStorageVideoTaskParams.getVideoUrl());
        // 视频文件流
        request.setVideoFileObject(addStorageVideoTaskParams.getVideoFileObject());
        // 视频的业务key
        request.setVideoId(addStorageVideoTaskParams.getVideoId());
        // 视频的业务标签
        request.setVideoTags(addStorageVideoTaskParams.getVideoTags());
        // 入库回调地址
        request.setCallbackUrl(addStorageVideoTaskParams.getCallbackUrl());

        request.setDescription(addStorageVideoTaskParams.getDescription());

        request.setSort(addStorageVideoTaskParams.getSort());
        System.out.println(JSON.toJSON(request).toString());
        // 发起请求并处理应答
        AddStorageVideoTaskResponse response = client.addStorageVideoTaskAdvance(request,runtimeOptions);
        // 获取任务id
        System.out.println("TaskId : " + response.getData().getTaskId());
        //System.out.println("RequestId : " + response.getRequestId());
        return response.getData().getTaskId();
    }

获取任务状态

    public Map<String, String> getTaskStatus(String taskId, String Id) throws TeaException,Exception  {
        Client client = setUp();
        GetTaskStatusRequest request = new GetTaskStatusRequest();
        // 任务id
        request.setTaskId(taskId);
        // 实例Id
        request.setInstanceId(Id);
        GetTaskStatusResponse response = client.getTaskStatus(request,runtimeOptions);
        System.out.println("requestId: "+response.requestId);
        System.out.println("Data : " + JSON.toJSONString(response.getData())+", ResultOss:"+response.getTaskInfo().getProcessResultOss());
        System.out.println("TaskInfo : " + JSON.toJSONString(response.getTaskInfo()));
        Map<String,String> map = new HashMap();
        map.put("Data",response.getData().toString());
        map.put("taskId",String.valueOf(response.getTaskInfo().getTaskId()));
        map.put("ResultOSS",response.getTaskInfo().getProcessResultOss());
        return map;
    }

获取实例列表

    public String listInstaces(ListInstacesParams listInstacesParams)throws Exception{
        Client client = setUp();

        ListInstancesRequest request = new ListInstancesRequest();
        //实例名称
        request.setInstanceName(listInstacesParams.getInstanceName());
        //实例类型
        request.setInstanceType(listInstacesParams.getInstanceType());
        request.setPageNumber(listInstacesParams.getPageNumber());
        request.setPageSize(listInstacesParams.getPageSize());
        request.setStatus(listInstacesParams.getStatus());
        request.setTags(listInstacesParams.getTags());
        System.out.println(JSON.toJSON(request).toString());
        ListInstancesResponse response = client.listInstancesSimply(request);
        System.out.println(JSON.toJSONString(response.getData()));
        return  JSON.toJSONString(response.getData());
    }

更改优先级

    public Boolean modifyPriority(ModifyPriorityParams modifyPriorityParams) throws Exception{
        Client client = setUp();

        ModifyPriorityRequest request = new ModifyPriorityRequest();
        request.setSort(modifyPriorityParams.getSort());
        request.setTaskId(modifyPriorityParams.getTaskId());

        System.out.println(JSON.toJSON(request).toString());
        ModifyPriorityResponse response = client.modifyPriority(request,runtimeOptions);

        System.out.println("taskId:"+response.getRequestId());
        System.out.println("修改结果:"+response.data);
        return response.getData();
    }

获取视频入库列表

    public String ListStorageVideoTasks(ListStorageVideoTasksParams params)throws Exception{
        Client client= setUp();
        ListStorageVideoTasksRequest  request = new ListStorageVideoTasksRequest();
        request.setDescription(params.getDescription());
        request.setVideoId(params.getVideoId());
        request.setInstanceId(instanceId);
        request.setPageNumber(params.getPageNumber());
        request.setPageSize(params.getPageSize());
        request.setStatusList(params.getStatusList());
        request.setSortList(params.getSortlist());
        request.setTaskId(params.getTaskId());
        request.setStorageInfoList(params.getStorageInfoList());
        //System.out.println(JSON.toJSON(request).toString());
        ListStorageVideoTasksResponse response = client.listStorageVideoTasks(request,runtimeOptions);
        System.out.println(JSON.toJSONString(response.data));
        return JSON.toJSONString(response.data);
    }

获取视频检索任务列表

    public String listSearchVideoTasks(ListSearchVideoTasksParams params) throws Exception{
        Client client= setUp();
        ListSearchVideoTasksRequest  request = new ListSearchVideoTasksRequest();
        request.setDescription(params.getDescription());
        request.setInstanceId(instanceId);
        request.setPageNumber(params.getPageNumber());
        request.setPageSize(params.getPageSize());
        request.setStatusList(params.getStatusList());
        request.setSortList(params.getSortList());
        request.setTaskId(params.getTaskId());
        request.setSearchTypeList(params.getSearchTypeList());
        ListSearchVideoTasksResponse response = client.listSearchVideoTasks(request,runtimeOptions);
        System.out.println(JSON.toJSONString(response.getData()));
        return JSON.toJSONString(response.data);
    }

创建批量任务(文件上传方式)

    public String listSearchVideoTasks(ListSearchVideoTasksParams params) throws Exception{
        Client client= setUp();
        ListSearchVideoTasksRequest  request = new ListSearchVideoTasksRequest();
        request.setDescription(params.getDescription());
        request.setInstanceId(instanceId);
        request.setPageNumber(params.getPageNumber());
        request.setPageSize(params.getPageSize());
        request.setStatusList(params.getStatusList());
        request.setSortList(params.getSortList());
        request.setTaskId(params.getTaskId());
        request.setSearchTypeList(params.getSearchTypeList());
        ListSearchVideoTasksResponse response = client.listSearchVideoTasks(request,runtimeOptions);
        System.out.println(JSON.toJSONString(response.getData()));
        return JSON.toJSONString(response.data);
    }

创建批量任务(OSS方式导入)

    public Long createBatchTaskForOss( CreateBatchTaskParams params)throws Exception{

        Client client = setUp();
        CreateBatchTaskRequest request = new CreateBatchTaskRequest();
        request.setBatchTaskType(params.getBatchTaskType());
        request.setCallbackUrl(searchCallback);
        request.setInstanceId(instanceId);
        request.setOssBucketName(params.getOssBucketName());
        request.setOssDataPath(params.getOssDataPath());
        request.setOssMetaFile(params.getOssMetaFile());
        request.setRoleArn(params.getRoleArn());
        System.out.println(JSON.toJSON(request).toString());
        CreateBatchTaskResponse response = client.createBatchTask(request,runtimeOptions);
        System.out.println("taskId:"+response.getTaskId());
        return  response.getTaskId();
    }

获取批量任务信息

    public Integer  getBatchTask (String instanceId, long batchTaskId) throws Exception{
        Client client = setUp();
        GetBatchTaskRequest request = new GetBatchTaskRequest();
        request.setBatchTaskId(batchTaskId);
        request.setInstanceId(instanceId);
        GetBatchTaskResponse response = client.getBatchTask(request,runtimeOptions);
        System.out.println("状态:"+response.status);
        System.out.println("子任务Oss地址:"+response.getSubTaskDetail());
        return  response.status;
    }

列举批量任务列表

    public String listBatchTask(ListBatchTaskParams params) throws Exception {
        Client client = setUp();
        ListBatchTaskRequest request = new ListBatchTaskRequest();
        request.setInstanceId(instanceId);
        request.setBatchTaskType(params.getBatchTaskType());
        request.setPageNumber(params.getPageNumber());
        request.setPageSize(params.getPageSize());
        request.setStatus(params.getStatus());
        request.setBucketName(params.getBucketName());
        request.setDataPath(params.getDataPath());
        System.out.println(JSON.toJSON(request).toString());
        ListBatchTaskResponse response = client.listBatchTask(request,runtimeOptions);

        System.out.println(JSON.toJSONString(response.data));
        return JSON.toJSONString(response.data);

    }