Hi,
I am trying to upload the documents(html or docx file) in discovery by using the Java API code available under the API reference section. I am using the same code which is provided under the Java example of API. Also used the same html file(sample1.html) that is given under the 'Add a document' section. But still no luck. Here is the exception that I am getting.
"Exception in thread "main" java.lang.NoSuchMethodError: com.ibm.watson.developer_cloud.http.InputStreamRequestBody.create(Lokhttp3/MediaType;Ljava/io/InputStream;)Lokhttp3/RequestBody;"
The exception is caused by line 365 of Discovery.class
**RequestBody file = InputStreamRequestBody.create(mediaType, createRequest.getFile());**
I tried to use the UTF-8 encoding as well but the error remains the same.
Here is my routine to add the document.
*public void adddocuments(String document) throws UnsupportedEncodingException
{
byte ptext[] = document.getBytes(ISO_8859_1);
String value = new String(ptext, UTF_8);
InputStream documentStream = new ByteArrayInputStream(value.getBytes());
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(EnvironmentId, CollectionId);
builder.inputStream(documentStream, HttpMediaType.TEXT_HTML);
CreateDocumentResponse createResponse = service.createDocument(builder.build()).execute();
}*
Does anyone have any idea how to solve this error?
Answer by SIB (41) | Mar 29, 2017 at 09:32 AM
For your info, here is my piece of code to load a 'big' Json document (split into small json structures) to a Discovery collection. Don't know if this helps .....
public void uploadDocs(String environmentId, String collectionId, String filePath) throws Exception {
String documentJson;
try {
documentJson = new String(Files.readAllBytes(Paths.get(filePath)));
JsonParser parser = new JsonParser();
JsonElement root = parser.parse(documentJson);
JsonArray array = root.getAsJsonObject().getAsJsonArray("documents") ;
int docNb = 1 ;
for (JsonElement elm : array) {
JsonObject elmObj = elm.getAsJsonObject();
log("Doc nb "+ docNb + ": " + elmObj.toString());
InputStream documentStream = new ByteArrayInputStream(elmObj.toString().getBytes());
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId);
builder.inputStream(documentStream, HttpMediaType.APPLICATION_JSON);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
log(""+createResponse) ;
docNb++;
}
log("End processing documents") ;
} catch (Exception e) {
e.printStackTrace();
}
}
Answer by MJonker (537) | May 17, 2018 at 09:13 AM
To addDocuments to a collection I use the AddDocumentOptions and the addDocument method on the Discovery Service, see also : https://developer.ibm.com/answers/questions/448302/adding-word-documents-to-watson-discovery-using-ja.html?childToView=448346#answer-448346