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

Issue retrieving multiple Watson Log pages using Java

550000DBX6 gravatar image
Question by marsh1jh  (3) | Aug 21 at 10:00 AM watson-assistantconversationassistantjava api

I am trying to create a Java script to retrieve all of the logs from a particular workspace for a given date range and running into an issue where when trying to retrieve the additional pages, it seems that I get stuck in an infinite loop. Can someone help guide me in what I am doing wrong? FYI, I am using the “ibm-watson-7.2.0-jar-with-dependencies.jar” library

Here is the code that I have trying to get this working for the logs on 2019-08-08 (Aug 8, 2019)

Any and all help would be appreciated.

    import com.ibm.watson.assistant.v1.Assistant;
     import com.ibm.cloud.sdk.core.service.security.IamOptions;
     import com.ibm.watson.assistant.v1.model.ListLogsOptions;
     import com.ibm.watson.assistant.v1.model.LogCollection;
     import org.json.JSONArray;
     import org.json.JSONException;
     import org.json.JSONObject;
      
 public class Main {
     private static String apikey="<apikey>"; 
     private static String version="2019-02-28";
     private static String url="https://gateway.watsonplatform.net/assistant/api";
     private static String workspaceId = "<workspaceid>";
     private static int pagelimit=500;
     private static LogCollection response;
     private static String next_url,next_cursor;
     private static String filter = "(response_timestamp>2019-08-07,response_timestamp<2019-08-09)";
 
     public static void main(String[] args) {
         // Connecting
         IamOptions iamOptions = new IamOptions.Builder().apiKey(apikey).build();
         Assistant service = new Assistant(version, iamOptions);
         service.setEndPoint(url);
         
         // Get first log set
         ListLogsOptions options = new ListLogsOptions.Builder(workspaceId)
                 .pageLimit(pagelimit)
                 .filter(filter)
                 .build();
         response = service.listLogs(options).execute().getResult();
 
         next_url=get_nexturl(response);
         next_cursor=get_nextcursor(response);
         
         // while next_cursor has a value, keep looping
         while (next_cursor!="") {
             options = new ListLogsOptions.Builder(workspaceId).
                     cursor(next_cursor).
                     pageLimit(pagelimit).
                     filter(filter).
                     build();
             response = service.listLogs(options).execute().getResult();
             next_url=get_nexturl(response);
             next_cursor=get_nextcursor(response);
         } //end while
     } //end main
 
     private static String get_nexturl(LogCollection r) {
         String p, nu;
         // Get Pagination and next_url
         p=return_key(r.toString(),"pagination");
         nu=return_key(p,"next_url");
         return nu;
     } // end get_nexturl
     
     private static String get_nextcursor(LogCollection r) {
         String p, nc;
         // Get Pagination and next_cursor
         p=return_key(r.toString(),"pagination");
         nc=return_key(p,"next_cursor");
         return nc;
     } // end get_nextcursor
     
     // returns the value of a JSON key, with JSON being passed as a string surrounded by "{ }"
     private static String return_key(String r, String key) {
         String p;
         try {
             // Get the pagination
             JSONObject jo = new JSONObject(r);
             p=jo.get(key).toString();
             return p;
         } catch (JSONException e) {
             return "";
         }
     } // end return_key
 } // end class



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

1 reply

  • Sort: 
110000A6B0 gravatar image
Accepted answer

Answer by mkistler (201) | Aug 21 at 05:02 PM

Not sure if this will solve your problem, but you don't need the functions for parsing through the result to get the nexturl and next cursor -- the SDK gives you accessors for these.

 next_url = response.getPagination().getNextUrl();
 next_cursor = response.getPagination().getNextCursor();
Comment

People who like this

  0   Show 1   Share
10 |3000 characters needed characters left characters exceeded
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
550000DBX6 gravatar image marsh1jh (3)   Aug 22 at 08:03 AM 0
Share

Thanks, that did indeed help. Using the built in functions, I was able to change my while loop condition to

 while (next_cursor!=null) vs while (next_cursor!="")

and it is now working reliably

Thanks for the assist!

Follow this question

193 people are following this question.

Answers

Answers & comments

Related questions

Watson Assistant: Updating the response to user dynamically 2 Answers

Watson Assistant workspace training 1 Answer

Check if user input isn't in the context array 1 Answer

Watson Assistant Log Error "Rate limit exceeded" 1 Answer

Remove entity training in Watson Assistant 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