Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. public class BulkQueryPKChunking {
  2.  
  3.  
  4. public static void main(String[] args)
  5. throws AsyncApiException, ConnectionException, IOException {
  6. BulkQueryPKChunking exampleQuery = new BulkQueryPKChunking();
  7. System.setProperty("https.protocols", "TLSv1.2");
  8. BulkConnection bulkConnection = exampleQuery.login();
  9. bulkConnection.addHeader("Sforce-Enable-PKChunking","chunkSize=2000");
  10. exampleQuery.doBulkQuery(bulkConnection);
  11. }
  12.  
  13.  
  14. public BulkConnection login() {
  15.  
  16. String userName = "deepnaik@ibm.com";
  17. String passWord = "DN2006ascPBmF0sqBPkVVCBp1ZmOhxhXQF";
  18. String url = "https://login.salesforce.com/services/Soap/u/32.0";
  19. BulkConnection _bulkConnection = null;
  20. try {
  21.  
  22. ConnectorConfig partnerConfig = new ConnectorConfig();
  23. partnerConfig.setUsername(userName);
  24. partnerConfig.setPassword(passWord);
  25. partnerConfig.setAuthEndpoint(url);
  26. new PartnerConnection(partnerConfig);
  27. ConnectorConfig config = new ConnectorConfig();
  28. config.setSessionId(partnerConfig.getSessionId());
  29. String soapEndpoint = partnerConfig.getServiceEndpoint();
  30. String apiVersion = "32.0";
  31. String restEndpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("Soap/"))
  32. + "async/" + apiVersion;
  33. config.setRestEndpoint(restEndpoint);
  34. config.setCompression(true);
  35. config.setTraceMessage(false);
  36. _bulkConnection = new BulkConnection(config);
  37. } catch (AsyncApiException aae) {
  38. aae.printStackTrace();
  39. } catch (ConnectionException ce) {
  40. ce.printStackTrace();
  41. }
  42. return _bulkConnection;
  43. }
  44.  
  45.  
  46.  
  47. public void doBulkQuery(BulkConnection bulkConnection) {
  48.  
  49. try {
  50. JobInfo job = new JobInfo();
  51. job.setObject("Account");
  52.  
  53. job.setOperation(OperationEnum.query);
  54. job.setConcurrencyMode(ConcurrencyMode.Parallel);
  55. job.setContentType(ContentType.CSV);
  56.  
  57. job = bulkConnection.createJob(job);
  58. assert job.getId() != null;
  59. System.out.println("Job id is " + job.getId());
  60.  
  61. job = bulkConnection.getJobStatus(job.getId());
  62.  
  63. String query = "SELECT Name, Id FROM Account";
  64. ByteArrayInputStream bout = new ByteArrayInputStream(query.getBytes());
  65. bulkConnection.createBatchFromStream(job, bout);
  66.  
  67. BatchInfo[] bListInfo = bulkConnection.getBatchInfoList(job.getId()).getBatchInfo();
  68. System.out.println("Numbe of batches is " + bListInfo.length);
  69. int numberOfBatchesForQueryExtract = 0;
  70.  
  71. for(int ib=1; ib < bListInfo.length; ib++)
  72. {
  73. BatchInfo info = bListInfo[ib];
  74. numberOfBatchesForQueryExtract++;
  75.  
  76. String[] queryResults = null;
  77.  
  78. for(int i=0; i<10000; i++)
  79. {
  80. Thread.sleep(30000); //30 sec
  81. info = bulkConnection.getBatchInfo(job.getId(), info.getId());
  82.  
  83. if (info.getState() == BatchStateEnum.Completed)
  84. {
  85. QueryResultList list = bulkConnection.getQueryResultList(job.getId(),info.getId());
  86. queryResults = list.getResult();
  87. break;
  88. }
  89. else if (info.getState() == BatchStateEnum.Failed)
  90. {
  91. System.out.println("-------------- failed ----------" + info);
  92. break;
  93. }
  94. else
  95. {
  96. System.out.println("-------------- waiting ----------" + info);
  97. }
  98. }
  99.  
  100. if (queryResults != null)
  101. {
  102. for (String resultId : queryResults)
  103. {
  104. InputStream resultStream = bulkConnection.getQueryResultStream(job.getId(), info.getId(), resultId);
  105. String fileNameCreated = createFileName(job.getId(), info.getId(),convertInputStreamToString(resultStream));
  106. //System.out.println("Data extracted successfully into CVS file");
  107. System.out.println("Filename " + fileNameCreated + ", No of rows in the file is " + countLines(fileNameCreated));
  108. }
  109. }
  110. }
  111. System.out.println("Number of batches created with chunkSize 2000 is" + numberOfBatchesForQueryExtract);
  112. } catch (AsyncApiException aae) {
  113. aae.printStackTrace();
  114. } catch (InterruptedException ie) {
  115. ie.printStackTrace();
  116. }
  117. }
  118.  
  119. public String convertInputStreamToString(InputStream inputStream)
  120. {
  121. StringBuilder result = new StringBuilder();
  122. String line;
  123. String newLine = System.getProperty("line.separator");
  124. boolean flag = false;
  125. try {
  126. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF8"));
  127.  
  128. while ((line = reader.readLine()) != null) {
  129. result.append(flag? newLine: "").append(line);
  130. flag = true;
  131. }
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. return result.toString();
  136. }
  137.  
  138. public String createFileName(String jobId, String batchId, String dataAsString)
  139. {
  140. String namePrefix = "SFBulkExtract";
  141. String nameSuffix = ".csv";
  142. String fileSeperator = "/";
  143. String fFileName = namePrefix + "_" + jobId + "_" + batchId + nameSuffix;
  144. createFile(dataAsString,fFileName);
  145. return fFileName;
  146. }
  147.  
  148. //Create the CVS file
  149. private void createFile(String dataAsString, String fFileName)
  150. {
  151. String fFileNameString = fFileName;
  152. OutputStream outFile = null;
  153. try {
  154. File oFile = new File(fFileNameString);
  155. if (oFile.exists())
  156. {
  157. boolean deleteFileSucceeded = oFile.delete();
  158. if (! deleteFileSucceeded)
  159. {
  160. throw new IOException("Unable to delete file");
  161. }
  162. }
  163. outFile = new BufferedOutputStream(new FileOutputStream(fFileNameString));
  164. outFile.write(dataAsString.getBytes());
  165. outFile.flush();
  166. }catch(FileNotFoundException ex){
  167. ex.printStackTrace();
  168. }catch(IOException ex){
  169. ex.printStackTrace();
  170. }finally {
  171. if(outFile != null)
  172. try {
  173. outFile.close();
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. }
  177. }
  178. }
  179.  
  180. public static int countLines(String filename)
  181. {
  182. InputStream is = null;
  183. try {
  184. is = new BufferedInputStream(new FileInputStream(filename));
  185.  
  186. byte[] c = new byte[1024];
  187. int count = 0;
  188. int readChars = 0;
  189. boolean empty = true;
  190. while ((readChars = is.read(c)) != -1) {
  191. empty = false;
  192. for (int i = 0; i < readChars; ++i) {
  193. if (c[i] == 'n') {
  194. ++count;
  195. }
  196. }
  197. }
  198. return (count == 0 && !empty) ? 1 : count;
  199. } catch (IOException e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. } finally {
  203. try {
  204. is.close();
  205. } catch (IOException e) {
  206. e.printStackTrace();
  207. }
  208. }
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement