Advertisement
Guest User

Untitled

a guest
Feb 4th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. at com.sforce.async.BulkConnection.parseAndThrowException(BulkConnection.java:190)
  2. at com.sforce.async.BulkConnection.doHttpGet(BulkConnection.java:747)
  3. at com.sforce.async.BulkConnection.getBatchInfo(BulkConnection.java:557)
  4. at com.sforce.async.BulkConnection.getBatchInfo(BulkConnection.java:550)
  5.  
  6. import java.io.*;
  7. import java.util.*;
  8. import com.sforce.async.*;
  9. import com.sforce.soap.partner.PartnerConnection;
  10. import com.sforce.ws.ConnectionException;
  11. import com.sforce.ws.ConnectorConfig;
  12.  
  13.  
  14. public class BulkQueryPerformanceObject {
  15.  
  16. static int finalBatchCount=0;
  17. static JobInfo job = new JobInfo();
  18. static BulkConnection bulkConnection = null;
  19. BatchInfo[] bListInfo = null;
  20. int numberOfBatchesForQueryExtract = 0;
  21. int numberOfRecordsExtracted = 0;
  22. long startTime = System.nanoTime();
  23.  
  24.  
  25. public static void main(String[] args)
  26. throws AsyncApiException, ConnectionException, IOException {
  27. BulkQueryPerformanceObject exampleQuery = new BulkQueryPerformanceObject();
  28. System.setProperty("https.protocols", "TLSv1.2");
  29. bulkConnection = exampleQuery.login();
  30. bulkConnection.addHeader("Sforce-Enable-PKChunking","chunkSize=10000");
  31. exampleQuery.doBulkQuery(bulkConnection);
  32. }
  33.  
  34.  
  35. public BulkConnection login() {
  36.  
  37. String userName = "testing1@ibm.com";
  38. String passWord = "ibmsalesVInDXaqvHnNEQgqTDIC24AAn";
  39. String url = "https://login.salesforce.com/services/Soap/u/39.0";
  40. BulkConnection _bulkConnection = null;
  41. try {
  42.  
  43. ConnectorConfig partnerConfig = new ConnectorConfig();
  44. partnerConfig.setUsername(userName);
  45. partnerConfig.setPassword(passWord);
  46. partnerConfig.setAuthEndpoint(url);
  47. new PartnerConnection(partnerConfig);
  48. ConnectorConfig config = new ConnectorConfig();
  49. config.setSessionId(partnerConfig.getSessionId());
  50. String soapEndpoint = partnerConfig.getServiceEndpoint();
  51. String apiVersion = "37.0";
  52. String restEndpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("Soap/"))
  53. + "async/" + apiVersion;
  54. config.setRestEndpoint(restEndpoint);
  55. config.setCompression(true);
  56. config.setTraceMessage(false);
  57. _bulkConnection = new BulkConnection(config);
  58. } catch (AsyncApiException aae) {
  59. aae.printStackTrace();
  60. } catch (ConnectionException ce) {
  61. ce.printStackTrace();
  62. }
  63. return _bulkConnection;
  64. }
  65.  
  66.  
  67.  
  68. public void doBulkQuery(BulkConnection bulkConnection) {
  69.  
  70. try {
  71. job.setObject("PerformanceObject__c");
  72. job.setOperation(OperationEnum.query);
  73. job.setConcurrencyMode(ConcurrencyMode.Parallel);
  74. job.setContentType(ContentType.CSV);
  75.  
  76. job = bulkConnection.createJob(job);
  77. assert job.getId() != null;
  78. System.out.println("Job id is " + job.getId());
  79.  
  80.  
  81. String query = "Select Id, Name, OwnerId From PerformanceObject__c";
  82. ByteArrayInputStream bout = new ByteArrayInputStream(query.getBytes());
  83. bulkConnection.createBatchFromStream(job, bout);
  84.  
  85. bListInfo = bulkConnection.getBatchInfoList(job.getId()).getBatchInfo();
  86.  
  87. while(bListInfo.length < 2)
  88. bListInfo = bulkConnection.getBatchInfoList(job.getId()).getBatchInfo();
  89.  
  90. finalBatchCount++;
  91. BatchInfo info = bListInfo[finalBatchCount];
  92. numberOfBatchesForQueryExtract++;
  93.  
  94. String[] queryResults = null;
  95.  
  96. for(int i=0; i<10000; i++)
  97. {
  98. info = bulkConnection.getBatchInfo(job.getId(), info.getId());
  99.  
  100. if (info.getState() == BatchStateEnum.Completed)
  101. {
  102. QueryResultList list = bulkConnection.getQueryResultList(job.getId(),info.getId());
  103. queryResults = list.getResult();
  104. break;
  105. }
  106. else if (info.getState() == BatchStateEnum.Failed)
  107. {
  108. System.out.println("-------------- failed ----------" + info);
  109. break;
  110. }
  111. else
  112. {
  113. System.out.println("-------------- waiting ----------" + info);
  114. }
  115. }
  116.  
  117. if (queryResults != null)
  118. {
  119. for (String resultId : queryResults)
  120. {
  121. InputStream resultStream = bulkConnection.getQueryResultStream(job.getId(), info.getId(), resultId);
  122. BufferedReader lineReader = new BufferedReader(new InputStreamReader(resultStream,"UTF8"));
  123. String lineString = null;
  124. lineString = lineReader.readLine();
  125. while((lineString = lineReader.readLine()) != null)
  126. {
  127. System.out.println("lineString : " + lineString);
  128. numberOfRecordsExtracted++;
  129. }
  130. }
  131. }
  132.  
  133. } catch (AsyncApiException aae) {
  134. aae.printStackTrace();
  135. } catch (UnsupportedEncodingException e) {
  136. e.printStackTrace();
  137. } catch (IOException e) {
  138. e.printStackTrace();
  139. }
  140.  
  141. while(!getMoreData())
  142. {}
  143.  
  144. System.out.println("Number of batches created with chunkSize 2000 is " + numberOfBatchesForQueryExtract);
  145. System.out.println("Number of total records extracted is " + numberOfRecordsExtracted);
  146. System.out.println("Final Total Batch Count is " + finalBatchCount);
  147. System.out.println("Job state is " + job.getState());
  148. long finishTime = System.nanoTime();
  149. long elapsedTime = (finishTime - startTime)/1000000;
  150. int seconds = (int)(elapsedTime / 1000) % 60 ;
  151. int minutes = (int)((elapsedTime / (1000*60)) % 60);
  152. int hours = (int)((elapsedTime / (1000*60*60)) % 24);
  153. System.out.println("Time for processing is "+ hours + ":" + minutes + ":" + seconds);
  154. }
  155.  
  156. public boolean isQueryDone()
  157. {
  158. try {
  159. if(finalBatchCount < (bulkConnection.getBatchInfoList(job.getId()).getBatchInfo().length -1))
  160. {
  161. return false;
  162. }
  163. else
  164. {
  165. bulkConnection.closeJob(job.getId());
  166. return true;
  167. }
  168. } catch (AsyncApiException e) {
  169. // TODO Auto-generated catch block
  170. e.printStackTrace();
  171. }
  172. return true;
  173. }
  174.  
  175.  
  176. public boolean getMoreData()
  177. {
  178. try{
  179. if ( isQueryDone() )
  180. {
  181. return true;
  182. }
  183. else
  184. {
  185. bListInfo = bulkConnection.getBatchInfoList(job.getId()).getBatchInfo();
  186. finalBatchCount++;
  187. BatchInfo info = bListInfo[finalBatchCount];
  188. numberOfBatchesForQueryExtract++;
  189.  
  190. String[] queryResults = null;
  191.  
  192. for(int i=0; i<10000; i++)
  193. {
  194. info = bulkConnection.getBatchInfo(job.getId(), info.getId());
  195.  
  196. if (info.getState() == BatchStateEnum.Completed)
  197. {
  198. QueryResultList list = bulkConnection.getQueryResultList(job.getId(),info.getId());
  199. queryResults = list.getResult();
  200. break;
  201. }
  202. else if (info.getState() == BatchStateEnum.Failed)
  203. {
  204. System.out.println("-------------- failed ----------" + info);
  205. break;
  206. }
  207. else
  208. {
  209. System.out.println("-------------- waiting ----------" + info);
  210. }
  211. }
  212.  
  213. if (queryResults != null)
  214. {
  215. for (String resultId : queryResults)
  216. {
  217. InputStream resultStream = bulkConnection.getQueryResultStream(job.getId(), info.getId(), resultId);
  218. BufferedReader lineReader = new BufferedReader(new InputStreamReader(resultStream,"UTF8"));
  219. String lineString = null;
  220. lineReader.readLine();
  221. while((lineString = lineReader.readLine()) != null)
  222. {
  223. System.out.println("lineString : " + lineString);
  224. numberOfRecordsExtracted++;
  225. }
  226. }
  227. }
  228. return false;
  229. }
  230. } catch (AsyncApiException aae) {
  231. aae.printStackTrace();
  232. } catch (UnsupportedEncodingException e) {
  233. e.printStackTrace();
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. return false;
  238. }
  239.  
  240. public String convertInputStreamToString(InputStream inputStream)
  241. {
  242. StringBuilder result = new StringBuilder();
  243. String line;
  244. String newLine = System.getProperty("line.separator");
  245. boolean flag = false;
  246. try {
  247. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF8"));
  248.  
  249. while ((line = reader.readLine()) != null) {
  250. result.append(flag? newLine: "").append(line);
  251. flag = true;
  252. }
  253. } catch (IOException e) {
  254. e.printStackTrace();
  255. }
  256. return result.toString();
  257. }
  258.  
  259.  
  260. public static int countLines(String filename)
  261. {
  262. InputStream is = null;
  263. try {
  264. is = new BufferedInputStream(new FileInputStream(filename));
  265.  
  266. byte[] c = new byte[1024];
  267. int count = 0;
  268. int readChars = 0;
  269. boolean empty = true;
  270. while ((readChars = is.read(c)) != -1) {
  271. empty = false;
  272. for (int i = 0; i < readChars; ++i) {
  273. if (c[i] == 'n') {
  274. ++count;
  275. }
  276. }
  277. }
  278. return (count == 0 && !empty) ? 1 : count;
  279. } catch (IOException e) {
  280. // TODO Auto-generated catch block
  281. e.printStackTrace();
  282. } finally {
  283. try {
  284. is.close();
  285. } catch (IOException e) {
  286. e.printStackTrace();
  287. }
  288. }
  289. return 0;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement