Advertisement
Guest User

BulkExample.java

a guest
Jan 16th, 2014
2,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. /*
  2. * @Author John M. Miller
  3. * @Date: 1/17/14
  4. * @Contact: johnm22987@gmail.com
  5. *
  6. * @param:
  7. * Connects to salesforce production org and pulls down tables based on user input found in the input.txt file or via the command line.
  8. * input.txt must be formatted as:
  9. *
  10. * Object
  11. * Query
  12. * Object
  13. * Query
  14. *
  15. * Running the jar will execute all of these in succession and results will be stored in csv files named from the objects called.
  16. * Query must be a full valid query. DO NOT include the Id field with any query, it will be done automatically to ensure data integrity
  17. */
  18. import java.io.*;
  19. import java.util.*;
  20.  
  21. import com.sforce.async.*;
  22. import com.sforce.soap.partner.DescribeSObjectResult;
  23. import com.sforce.soap.partner.Field;
  24. import com.sforce.soap.partner.PartnerConnection;
  25. import com.sforce.ws.ConnectionException;
  26. import com.sforce.ws.ConnectorConfig;
  27.  
  28.  
  29. public class BulkExample {
  30.  
  31. private BulkConnection bulkConnection = null;
  32. private static String object = "";
  33. private static String query = "";
  34. private String userId = "USERNAME"; //change to alter login
  35. private String passwd = "PASSWORD";
  36.  
  37. public static void main(String[] args)
  38. throws AsyncApiException, ConnectionException, IOException {
  39. //for loop to take in input from text file
  40. BulkExample example = new BulkExample();
  41. InputHandler input = new InputHandler();
  42. ArrayList<String>[] jobList= input.getObjectsandQueries();
  43. for(int i = 0; i < jobList[0].size();i++){
  44. object = jobList[0].get(i);
  45. query = jobList[1].get(i);
  46. int numRecords = example.doBulkQuery();
  47. if(numRecords == 0){
  48. //bulk api will also throw more extensive log on what was wrong
  49. System.out.println("Query was: "+query);
  50. System.out.println("Object was: "+object);
  51. throw new IllegalArgumentException("Malformed Query or no Data found. Please check console output.");
  52. }
  53. System.out.println(numRecords+" results returned.");
  54. }
  55. }
  56.  
  57. public boolean login() {
  58. boolean success = false;
  59. //change from login to test to move to sandbox and last number is API version being used
  60. String soapAuthEndPoint = "https://test.salesforce.com/services/Soap/u/29.0";
  61. //first part is the header on the url of the org you are pointing this tool at
  62. String bulkAuthEndPoint = "https://cs17-api.salesforce.com/services/async/29.0";
  63. try {
  64. ConnectorConfig config = new ConnectorConfig();
  65. config.setUsername(userId);
  66. config.setPassword(passwd);
  67. config.setAuthEndpoint(soapAuthEndPoint);
  68. config.setCompression(true);
  69. config.setTraceFile("traceLogs.txt");
  70. config.setTraceMessage(false);
  71. config.setPrettyPrintXml(true);
  72. config.setRestEndpoint(bulkAuthEndPoint);
  73. System.out.println("AuthEndpoint: " +
  74. config.getRestEndpoint());
  75. PartnerConnection connection = new PartnerConnection(config);
  76. System.out.println("SessionID: " + config.getSessionId());
  77. bulkConnection = new BulkConnection(config);
  78. success = true;
  79.  
  80. //generates query from all objects, if query generation requirements change, uncomment and it will run
  81. //DescribeSObjectResult info = connection.describeSObject(object);
  82. //Field[] fields = info.getFields();
  83. //QueryHandler qh = new QueryHandler();
  84. //query = qh.generateQuery(fields, object);
  85.  
  86. } catch (AsyncApiException aae) {
  87. aae.printStackTrace();
  88. } catch (ConnectionException ce) {
  89. ce.printStackTrace();
  90. } catch (FileNotFoundException fnfe) {
  91. fnfe.printStackTrace();
  92. }
  93. return success;
  94. }
  95.  
  96. public int doBulkQuery() throws IOException{
  97. if ( ! login() ) {
  98. throw new IllegalArgumentException("Invalid Login");
  99. }
  100. try {
  101. JobInfo job = new JobInfo();
  102. job.setObject(object);
  103.  
  104. job.setOperation(OperationEnum.query);
  105. job.setConcurrencyMode(ConcurrencyMode.Parallel);
  106. job.setContentType(ContentType.CSV);
  107.  
  108. job = bulkConnection.createJob(job);
  109. assert job.getId() != null;
  110.  
  111. job = bulkConnection.getJobStatus(job.getId());
  112.  
  113. Calendar time = Calendar.getInstance();
  114. System.out.println("Query started on object "+object+" at time: "+time.get(Calendar.HOUR_OF_DAY)
  115. + ":" + time.get(Calendar.MINUTE)+":"+time.getGreatestMinimum(Calendar.SECOND)+".");
  116.  
  117. BatchInfo info = null;
  118. ByteArrayInputStream bout =
  119. new ByteArrayInputStream(query.getBytes());
  120. info = bulkConnection.createBatchFromStream(job, bout);
  121.  
  122. String[] queryResults = null;
  123.  
  124. for(int i=0; i<10000; i++) {
  125. Thread.sleep(i==0 ? 30 * 1000 : 30 * 1000); //30 sec
  126. info = bulkConnection.getBatchInfo(job.getId(),
  127. info.getId());
  128.  
  129. if (info.getState() == BatchStateEnum.Completed) {
  130. QueryResultList list =
  131. bulkConnection.getQueryResultList(job.getId(),
  132. info.getId());
  133. queryResults = list.getResult();
  134. break;
  135. } else if (info.getState() == BatchStateEnum.Failed) {
  136. System.out.println("-------------- failed ----------"
  137. + info);
  138. bulkConnection.closeJob(job.getId());
  139. break;
  140. } else {
  141. System.out.println("-------------- waiting ----------"
  142. + info);
  143. }
  144. }
  145.  
  146. if (queryResults != null) {
  147. for (String resultId : queryResults) {
  148. //grabs result stream and passes it to csv writer
  149. FileHandler.writeCSVFromStream(bulkConnection.getQueryResultStream(job.getId(),
  150. info.getId(), resultId),object);
  151. //grabs results to ensure integrity
  152. bulkConnection.getQueryResultList(job.getId(), info.getId()).getResult();
  153. }
  154. //notify user of job complete
  155. System.out.println("Output complete on object "+object+" at time: "+time.get(Calendar.HOUR_OF_DAY)
  156. + ":" + time.get(Calendar.MINUTE)+":"+time.getGreatestMinimum(Calendar.SECOND)+", processung results.");
  157. //return number of records complete for data check and close job
  158. int out = info.getNumberRecordsProcessed();
  159. bulkConnection.closeJob(job.getId());
  160. return out;
  161. }
  162. } catch (AsyncApiException aae) {
  163. aae.printStackTrace();
  164. } catch (InterruptedException ie) {
  165. ie.printStackTrace();
  166. }
  167. //something went wrong here, return 0 to catch an error back in main
  168. return 0;
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement