全部产品

Java SDK

更新时间:2020-10-12 17:23:54

准备工作

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

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

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>videosearch20200225</artifactId>
    4. <version>1.0.0</version>
    5. </dependency>

完整代码示例

创建视频入库任务

  1. // 设置Region以北京为例,其它Region请按实际情况填写。
  2. String region = "cn-beijing";
  3. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建Config
  7. Config config = new Config();
  8. config.regionId = "<yourRegionId>";
  9. config.accessKeyId = "<yourAccessKeyId>";
  10. config.accessKeySecret = "<yourAccessKeySecret>";
  11. config.endpoint="multisearch.<regionId>.aliyuncs.com"
  12. try {
  13. Client client = new Client(config);
  14. AddStorageVideoTaskRequest request = new AddStorageVideoTaskRequest();
  15. // 实例id
  16. request.setInstanceId("<yourInstanceId>");
  17. // 视频url
  18. request.setVideoUrl("<yourVideoUrl>");
  19. // 视频id(业务key)
  20. request.setVideoId("<yourVideoId>");
  21. // 视频描述
  22. request.setDescription("<yourDescription>");
  23. // 视频标签
  24. request.setVideoTags("<yourVideoTag>");
  25. // 入库完成的回调
  26. request.setCallbackUrl("<yourCallbackUrl>");
  27. AddStorageVideoTaskResponse response = client.addStorageVideoTask(request);
  28. System.out.println("requestId : " + response.getRequestId());
  29. System.out.println("Data : " + JSON.toJSONString(response.getData()));
  30. } catch (TeaException e) {
  31. System.out.println(JSON.toJSONString(e.getData()));
  32. System.out.println(e.getCode());
  33. System.out.println(e.getMessage());
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }

创建视频检索任务

  1. // 设置Region以北京为例,其它Region请按实际情况填写。
  2. String region = "cn-beijing";
  3. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建Config
  7. Config config = new Config();
  8. config.regionId = "<yourRegionId>";
  9. config.accessKeyId = "<yourAccessKeyId>";
  10. config.accessKeySecret = "<yourAccessKeySecret>";
  11. config.endpoint="multisearch.<regionId>.aliyuncs.com"
  12. try {
  13. Client client = new Client(config);
  14. AddSearchVideoTaskRequest request = new AddSearchVideoTaskRequest();
  15. // 实例id
  16. request.setInstanceId("<yourInstanceId>");
  17. // 检索类型(1. 视频搜视频 2. 图片搜视频)
  18. request.setSearchType(1);
  19. // 视频/图片的url地址
  20. request.setVideoUrl("<yourVideoUrl>");
  21. // 视频/图片的描述
  22. request.setDescription("<yourDescription>");
  23. // 检索标签
  24. request.setQueryTags("<yourQueryTags>");
  25. // 返回结果数
  26. request.setReturnVideoNumber(20);
  27. // 查询入库类型,当检索类型为1时生效(1. 直接入库 2. 去重入库 3. 不入库)
  28. request.setStorageType(3);
  29. // 视频id,当searchType为1且storageType为1,2时生效
  30. request.setVideoId("<yourVideoId>");
  31. // 视频标签,当searchType为1且storageType为1,2时生效
  32. request.setVideoTags("<yourVideoTags>");
  33. // 去重入库阈值,searchType为1且storageType为2时生效
  34. request.setReplaceStorageThreshold(0.75f);
  35. // 检索完成的回调
  36. request.setCallbackUrl("<yourCallbackUrl>");
  37. AddSearchVideoTaskResponse response = client.addSearchVideoTask(request);
  38. System.out.println("requestId : " + response.getRequestId());
  39. System.out.println("Data : " + JSON.toJSONString(response.getData()));
  40. } catch (TeaException e) {
  41. System.out.println(JSON.toJSONString(e.getData()));
  42. System.out.println(e.getCode());
  43. System.out.println(e.getMessage());
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }

查询任务状态

  1. // 设置Region以北京为例,其它Region请按实际情况填写。
  2. String region = "cn-beijing";
  3. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建Config
  7. Config config = new Config();
  8. config.regionId = "<yourRegionId>";
  9. config.accessKeyId = "<yourAccessKeyId>";
  10. config.accessKeySecret = "<yourAccessKeySecret>";
  11. config.endpoint="multisearch.<regionId>.aliyuncs.com"
  12. try {
  13. Client client = new Client(config);
  14. GetTaskStatusRequest request = new GetTaskStatusRequest();
  15. // 实例id
  16. request.setInstanceId("<yourInstanceId>");
  17. // 任务id
  18. request.setTaskId("<yourTaskId>");
  19. GetTaskStatusResponse response = client.getTaskStatus(request);
  20. System.out.println("requestId : " + response.getRequestId());
  21. System.out.println("Data : " + JSON.toJSONString(response.getData()));
  22. System.out.println("TaskInfo : " + JSON.toJSONString(response.getTaskInfo()));
  23. } catch (TeaException e) {
  24. System.out.println(JSON.toJSONString(e.getData()));
  25. System.out.println(e.getCode());
  26. System.out.println(e.getMessage());
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }

查询入库历史

  1. // 设置Region以北京为例,其它Region请按实际情况填写。
  2. String region = "cn-beijing";
  3. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建Config
  7. Config config = new Config();
  8. config.regionId = "<yourRegionId>";
  9. config.accessKeyId = "<yourAccessKeyId>";
  10. config.accessKeySecret = "<yourAccessKeySecret>";
  11. config.endpoint="multisearch.<regionId>.aliyuncs.com"
  12. try {
  13. Client client = new Client(config);
  14. GetStorageHistoryRequest request = new GetStorageHistoryRequest();
  15. // 实例id
  16. request.setInstanceId("<yourInstanceId>");
  17. // 视频id
  18. request.setVideoId("<yourVideoId>");
  19. // 当前页数
  20. request.setPageNumber(1);
  21. // 分页大小
  22. request.setPageSize(10);
  23. GetStorageHistoryResponse response = client.getStorageHistory(request);
  24. System.out.println("requestId : " + response.requestId);
  25. System.out.println("Data : " + JSON.toJSONString(response.getData()));
  26. } catch (TeaException e) {
  27. System.out.println(JSON.toJSONString(e.getData()));
  28. System.out.println(e.getCode());
  29. System.out.println(e.getMessage());
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }

删除视频

  1. // 设置Region以北京为例,其它Region请按实际情况填写。
  2. String region = "cn-beijing";
  3. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建Config
  7. Config config = new Config();
  8. config.regionId = "<yourRegionId>";
  9. config.accessKeyId = "<yourAccessKeyId>";
  10. config.accessKeySecret = "<yourAccessKeySecret>";
  11. config.endpoint="multisearch.<regionId>.aliyuncs.com"
  12. try {
  13. Client client = new Client(config);
  14. AddDeletionVideoTaskRequest request = new AddDeletionVideoTaskRequest();
  15. // 实例id
  16. request.setInstanceId("<yourInstanceId>");
  17. // 视频id
  18. request.setVideoId("<yourVideoId>");
  19. AddDeletionVideoTaskResponse response = client.addDeletionVideoTask(request);
  20. System.out.println("requestId : " + response.requestId);
  21. System.out.println("Data : " + response.getData());
  22. } catch (TeaException e) {
  23. System.out.println(JSON.toJSONString(e.getData()));
  24. System.out.println(e.getCode());
  25. System.out.println(e.getMessage());
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }