Digital Developer Conference: a FREE half-day online conference focused on AI & Cloud – North America: Nov 2 – India: Nov 9 – Europe: Nov 14 – Asia Nov 23 Register now

Close outline
  • United States
IBM?
  • Site map
IBM?
  • Marketplace

  • Close
    Search
  • Sign in
    • Sign in
    • Register
  • IBM Navigation
IBM Developer Answers
  • Spaces
    • Blockchain
    • IBM Cloud platform
    • Internet of Things
    • Predictive Analytics
    • Watson
    • See all spaces
  • Tags
  • Users
  • Badges
  • FAQ
  • Help
Close

Name

Community

  • Learn
  • Develop
  • Connect

Discover IBM

  • ConnectMarketplace
  • Products
  • Services
  • Industries
  • Careers
  • Partners
  • Support
10.190.13.195

Refine your search by using the following advanced search options.

Criteria Usage
Questions with keyword1 or keyword2 keyword1 keyword2
Questions with a mandatory word, e.g. keyword2 keyword1 +keyword2
Questions excluding a word, e.g. keyword2 keyword1 -keyword2
Questions with keyword(s) and a specific tag keyword1 [tag1]
Questions with keyword(s) and either of two or more specific tags keyword1 [tag1] [tag2]
To search for all posts by a user or all posts with a specific tag, start typing and choose from the suggestion list. Do not use a plus or minus sign with a tag, e.g., +[tag1].
  • Ask a question

Adding Html or Docx file into Watson Discovery by using the Java SDK

310000K7CT gravatar image
Question by GhulamSarwar  (1) | Mar 28, 2017 at 09:12 AM watsondiscoverywatson-discoveryjava-sdk

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?

People who like this

  0
Comment
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster

2 answers

  • Sort: 
120000N2N7 gravatar image

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

 }
Comment

People who like this

  0   Share
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
100000GP4U gravatar image

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

Comment

People who like this

  0   Share
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster

Follow this question

116 people are following this question.

Answers

Answers & comments

Related questions

When I used java code to get aggregation from discovery service it shows me aggregation list as null. 0 Answers

Adding Word Documents to Watson Discovery using Java? 1 Answer

Watson Discovery - getEnvironments missing 1 Answer

Is the Data Crawler available for Discovery Service Lite plans ? 2 Answers

Pricing changes: Element Classification Add-On 1 Answer

  • Contact
  • Privacy
  • IBM Developer Terms of use
  • Accessibility
  • Report Abuse
  • Cookie Preferences

Powered by AnswerHub

Authentication check. Please ignore.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • API Connect
  • Analytic Hybrid Cloud Core
  • Application Performance Management
  • Appsecdev
  • BPM
  • Blockchain
  • Business Transaction Intelligence
  • CAPI
  • CAPI SNAP
  • CICS
  • Cloud Analytics
  • Cloud Automation
  • Cloud Object Storage
  • Cloud marketplace
  • Collaboration
  • Content Services (ECM)
  • Continuous Testing
  • Courses
  • Customer Experience Analytics
  • DB2 LUW
  • Data and AI
  • DataPower
  • Decision Optimization
  • DevOps Build
  • DevOps Services
  • Developers IBM MX
  • Digital Commerce
  • Digital Experience
  • Finance
  • Global Entrepreneur Program
  • Hadoop
  • Hybrid Cloud Core
  • Hyper Protect
  • IBM Cloud platform
  • IBM Design
  • IBM Forms Experience Builder
  • IBM Maximo Developer
  • IBM StoredIQ
  • IBM StoredIQ-Cartridges
  • IIDR
  • ITOA
  • InformationServer
  • Integration Bus
  • Internet of Things
  • Kenexa
  • Linux on Power
  • LinuxONE
  • MDM
  • Mainframe
  • Messaging
  • Node.js
  • ODM
  • Open
  • PartnerWorld Developer Support
  • PowerAI
  • PowerVC
  • Predictive Analytics
  • Product Insights
  • PureData for Analytics
  • Push
  • QRadar App Development
  • Run Book Automation
  • Search Insights
  • Security Core
  • Storage
  • Storage Core
  • Streamsdev
  • Supply Chain Business Network
  • Supply Chain Insights
  • Swift
  • UBX Capture
  • Universal Behavior Exchange
  • UrbanCode
  • WASdev
  • WSRR
  • Watson
  • Watson Campaign Automation
  • Watson Content Hub
  • Watson Marketing Insights
  • dW Answers Help
  • dW Premium
  • developerWorks Sandbox
  • developerWorks Team
  • Watson Health
  • More
  • Tags
  • Questions
  • Users
  • Badges