java - Reading data from an API -


i have written function read data external api. function , calls api while reading file disk. want optimize code large size of file (35000 records). please suggest me on this.

following code.

public void readcsvfile() {      try {          br = new bufferedreader(new filereader(getfilename()));          while ((line = br.readline()) != null) {               string[] splitline = line.split(cvssplitby);              string campaign = splitline[0];             string adgroup =  splitline[1];             string url = splitline[2];                           long searchcount = getsearchcount(url);                           stringbuilder sb = new stringbuilder();             sb.append(campaign + ",");             sb.append(adgroup + ",");                            sb.append(searchcount + ",");                            writetofile(sb, getnewfilename());          }      } catch (exception e) {         e.printstacktrace();     } }  private long getsearchcount(string url) {     long recordcount = 0;     try {          defaulthttpclient httpclient = new defaulthttpclient();          httpget getrequest = new httpget(                 "api.com/querysearch?q="                         + url);         getrequest.addheader("accept", "application/json");          httpresponse response = httpclient.execute(getrequest);          if (response.getstatusline().getstatuscode() != 200) {             throw new runtimeexception("failed : http error code : "                     + response.getstatusline().getstatuscode());         }          bufferedreader br = new bufferedreader(new inputstreamreader(                 (response.getentity().getcontent())));          string output;          while ((output = br.readline()) != null) {             try {                  jsonobject json = (jsonobject) new jsonparser()                         .parse(output);                 jsonobject result = (jsonobject) json.get("result");                 recordcount = (long) result.get("count");                 system.out.println(url + "=" + recordcount);              } catch (exception e) {                 system.out.println(e.getmessage());             }          }          httpclient.getconnectionmanager().shutdown();      } catch (exception e) {         e.getstacktrace();     }     return recordcount;  } 

since remote calls slower local disk access, you'll want in way parallelize or batch remote calls. if can't make batch calls remote api, allows multiple concurrent reads, perhaps want use thread pool make remote calls:

public void readcsvfile() {     // exception handling ignored space     br = new bufferedreader(new filereader(getfilename()));     list<future<string>> futures = new arraylist<future<string>>();     executorservice pool = executors.newfixedthreadpool(5);      while ((line = br.readline()) != null) {         final string[] splitline = line.split(cvssplitby);         futures.add(pool.submit(new callable<string> {             public string call() {                 long searchcount = getsearchcount(splitline[2]);                 return new stringbuilder()                     .append(splitline[0]+ ",")                     .append(splitline[1]+ ",")                     .append(searchcount + ",")                     .tostring();             }         }));     }      (future<string> fs: futures) {         writetofile(fs.get(), getnewfilename());     }      pool.shutdown(); } 

ideally, though, you'd want make single batch read remote api if @ possible.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -