IBM Watson:将文档添加到集合时出现未知 API 错误
IBM Watson: Unknown API error when adding documents to the collection
我正在尝试通过 Java 中的 IBM Watson API 将文档添加到我的 IBM Bluemix Discovery 实例的集合中。对于给定的文件,我执行如下操作:
Discovery discovery = new Discovery("2017-08-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
discovery.setUsernameAndPassword("{username}", "{password}");
String environmentId = "{environment_id}";
String collectionId = "{collection_id}";
File f = new File("path/to/file");
String path = f.getPath();
String ext = FilenameUtils.getExtension(f.getName());
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId).documentId(path).file(f);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
System.out.println(createResponse.toString());
但是,当我执行我的程序时,我收到错误 Exception in thread "main" com.ibm.watson.developer_cloud.service.exception.NotFoundException: Unknown API: POST [...]
,它来自初始化 createResponse
的行。
谢谢。
您的 documentId 有误。
正确的文档 ID 类似于“49a82bd4-xxxx-xxxx-xxxx-900b7378ff35”,但您将路径作为参数。
如果要添加文档,默认不需要指定documentId的值。
Discovery discovery = new Discovery("2017-08-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
discovery.setUsernameAndPassword("feeb4a95-xxxx-xxxx-xxxx-4d94c98b2b24", "KQc6G5nK6HrD");
String environmentId = "59d0fa32-xxxx-xxxx-xxxx-cf98a8cdc52e";
String collectionId = "49a82bd4-xxxx-xxxx-xxxx-900b7378ff35";
File f = new File("/Users/xxx/Desktop/myDoc.json");
Path path = Paths.get(f.getPath());
InputStream documentStream = new ByteArrayInputStream(Files.readAllBytes(path));
//Create
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId);
builder.inputStream(documentStream, HttpMediaType.APPLICATION_JSON);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
//Retrieve (You can get documentId here)
QueryRequest.Builder queryBuilder = new QueryRequest.Builder(environmentId, collectionId);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
System.out.println(queryResponse.toString());
//Delete
String documentId = "d1059598-xxxx-xxxx-xxxx-b65a1f3334d8";
DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest.Builder(environmentId, collectionId, documentId).build();
DeleteDocumentResponse deleteResponse = discovery.deleteDocument(deleteRequest).execute();
我正在尝试通过 Java 中的 IBM Watson API 将文档添加到我的 IBM Bluemix Discovery 实例的集合中。对于给定的文件,我执行如下操作:
Discovery discovery = new Discovery("2017-08-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
discovery.setUsernameAndPassword("{username}", "{password}");
String environmentId = "{environment_id}";
String collectionId = "{collection_id}";
File f = new File("path/to/file");
String path = f.getPath();
String ext = FilenameUtils.getExtension(f.getName());
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId).documentId(path).file(f);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
System.out.println(createResponse.toString());
但是,当我执行我的程序时,我收到错误 Exception in thread "main" com.ibm.watson.developer_cloud.service.exception.NotFoundException: Unknown API: POST [...]
,它来自初始化 createResponse
的行。
谢谢。
您的 documentId 有误。
正确的文档 ID 类似于“49a82bd4-xxxx-xxxx-xxxx-900b7378ff35”,但您将路径作为参数。
如果要添加文档,默认不需要指定documentId的值。
Discovery discovery = new Discovery("2017-08-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
discovery.setUsernameAndPassword("feeb4a95-xxxx-xxxx-xxxx-4d94c98b2b24", "KQc6G5nK6HrD");
String environmentId = "59d0fa32-xxxx-xxxx-xxxx-cf98a8cdc52e";
String collectionId = "49a82bd4-xxxx-xxxx-xxxx-900b7378ff35";
File f = new File("/Users/xxx/Desktop/myDoc.json");
Path path = Paths.get(f.getPath());
InputStream documentStream = new ByteArrayInputStream(Files.readAllBytes(path));
//Create
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId);
builder.inputStream(documentStream, HttpMediaType.APPLICATION_JSON);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
//Retrieve (You can get documentId here)
QueryRequest.Builder queryBuilder = new QueryRequest.Builder(environmentId, collectionId);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
System.out.println(queryResponse.toString());
//Delete
String documentId = "d1059598-xxxx-xxxx-xxxx-b65a1f3334d8";
DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest.Builder(environmentId, collectionId, documentId).build();
DeleteDocumentResponse deleteResponse = discovery.deleteDocument(deleteRequest).execute();