Java SDK

准备工作

  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>

完整代码示例

v1.1.2 更新

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

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

3.详细出入参可查看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 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();
    }

创建视频入库任务

try{
Client client =setUp();

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

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

        Client client = setUp();

        AddSearchVideoTaskAdvanceRequest request = new AddSearchVideoTaskAdvanceRequest();
        // 视频oss url
        request.setVideoUrl(<yourVideoUrl>);
        // 处理任务的实例id
        request.setInstanceId(<yourInstanceId>);
        // 视频的业务key
        request.setVideoId(<yourVIdeoId>);
        // 视频的业务标签
        request.setVideoTags(<yourVideoTags>);
        // 返回视频数量
        request.setReturnVideoNumber(<yourVideoNumber>);
        // 查询标签
        request.setQueryTags(<yourQueryTags>);
        // 检索入库类型 1为视频搜索
        request.setStorageType(<yourStorageType>);
        // 检索
        request.setCallbackUrl(<yourCallbackUr>);
        // 覆盖入库的阈值,当searchStorageType为2(覆盖入库)时生效
        request.setReplaceStorageThreshold(<yourStorageThreshold>);
        //视频描述
        request.setDescription(<yourDescription>);
        request.setSearchType(<yourSeacrchType>);
        //上传文件流
        request.setVideoFileObject(<yourFileObject>);
        //设置优先级,(0.低 1.中 2.高 3.紧急)
        request.setSort(<yourSort>);
        //是否需要特征文件 0.不需要 1.需要
        request.setNeedFeatureFile(<yourNeedFeatureFile>);

        // 发起请求并处理应答或异常
        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()));
    

创建视频检索任务

Client client =setUp();


AddSearchVideoTaskRequest request =newAddSearchVideoTaskRequest();

// 实例id
        request.setInstanceId("<yourInstanceId>");
// 参考详细接口文档
        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();
}

获取视频入库列表

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

二丶音频指纹

创建音频任务(文件方式)

          Client client = setUp();
        AddStorageAudioTaskAdvanceRequest request = new AddStorageAudioTaskAdvanceRequest();
        // 处理任务的实例id
        request.setInstanceId(<yourInstanceId>);
        // oss url
        request.setAudioUrl(<yourAudioUrl>);
        // 文件流
        request.setAudioFileObject(<yourFileObjcet>);
        // 业务key
        request.setAudioId(<yourAudioId>);
        // 业务标签
        request.setAudioTags(<yourAudioTags>);
        // 入库回调地址
        request.setCallbackUrl(<yourCallbackUrl>);

        request.setDescription(<yourDescription>);

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

创建音频入库任务

try{
Client client =setUp();

AddStorageAudioTaskRequest request =newAddStorageAudioTaskRequest();
// 实例id
        request.setInstanceId("<yourInstanceId>");
// url
        request.setAudioUrl("<yourAudioUrl>");
// id(业务key)
        request.setAudioId("<yourAudioId>");
//描述
        request.setDescription("<yourDescription>");
// 标签
        request.setAudioTags("<yourAudioTag>");
// 入库完成的回调
        request.setCallbackUrl("<yourCallbackUrl>");

AddStorageAudioTaskResponse response = client.addStorageAudioTask(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();
}

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

        Client client = setUp();

        AddSearchAudioTaskAdvanceRequest request = new AddSearchAudioTaskAdvanceRequest();
        // oss url
        request.setAudioUrl(<yourAudioUrl>);
        // 处理任务的实例id
        request.setInstanceId(<yourInstanceId>);
        request.setAudio(<yourAudioId>);
        request.setAudioTags(<yourAudioTags>);
        request.setReturnAudioNumber(<yourAudioNumber>);
        // 查询标签
        request.setQueryTags(<yourQueryTags>);
        request.setStorageType(<yourStorageType>);
        // 检索
        request.setCallbackUrl(<yourCallbackUr>);
        // 覆盖入库的阈值,当searchStorageType为2(覆盖入库)时生效
        request.setReplaceStorageThreshold(<yourStorageThreshold>);
        //描述
        request.setDescription(<yourDescription>);
        request.setSearchType(<yourSeacrchType>);
        //上传文件流
        request.setAudioFileObject(<yourFileObject>);
        //设置优先级,(0.低 1.中 2.高 3.紧急)
        request.setSort(<yourSort>);
        //是否需要特征文件 0.不需要 1.需要
        request.setNeedFeatureFile(<yourNeedFeatureFile>);

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

创建音频检索任务

Client client =setUp();


AddSearchAudioTaskRequest request =newAddSearchAudioTaskRequest();

// 实例id
        request.setInstanceId("<yourInstanceId>");
// 参考接口入参文档
        request.setSearchType(1);
        request.setAudioUrl("<yourAudioUrl>");
        request.setDescription("<yourDescription>");
// 检索标签
        request.setQueryTags("<yourQueryTags>");
// 返回结果数
        request.setReturnVideoNumber(20);
// 查询入库类型,当检索类型为1时生效(1. 直接入库  2. 去重入库  3. 不入库)
        request.setStorageType(3);
        request.setAudioId("<yourAudioId>");
        request.setAudioTags("<yourAudioTags>");
        request.setReplaceStorageThreshold(0.75f);
// 检索完成的回调
        request.setCallbackUrl("<yourCallbackUrl>");

AddSearchAudioTaskResponse response = client.addSearchAudioTask(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();
}

获取音频入库列表

           Client client= setUp();
        ListStorageAudioTasksRequest  request = new ListStorageAudioTasksRequest();
        request.setDescription(<yourDescription>);
        request.setAudioId(<yourAudioId>);
        request.setInstanceId(<yourInstanceId>);
        request.setPageNumber(<yourPageNumber>);
        request.setPageSize(<yourPageSize>);
        request.setStatusList(<yourStatusList>);
        request.setSortList(<yourSortList>);
        request.setTaskId(<yourTaskId>);
        request.setStorageInfoList(<yourStorageInfoList>);
        //System.out.println(JSON.toJSON(request).toString());
        ListStorageAudioTasksResponse response = client.listStorageAudioTasks(request,runtimeOptions);
        System.out.println(JSON.toJSONString(response.data));

获取音频检索任务列表

          Client client= setUp();
        ListSearchAudioTasksRequest  request = new ListSearchAudioTasksRequest();
        request.setDescription(<yourDescription>);
        request.setInstanceId(<yourInstanceId>);
        request.setPageNumber(<yourPageNumber>);
        request.setPageSize(<yourPageSize>);
        request.setStatusList(<yourStatusList>);
        request.setSortList(<yourSortList>);
        request.setTaskId(<yourTaskId>);
        ListSearchAudioTasksResponse response = client.listSearchAudioTasks(request,runtimeOptions);
        System.out.println(JSON.toJSONString(response.getData()));

三丶通用实例

获取任务状态

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

创建批量任务(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);

    }