No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Using Bulk Query
When adding a batch to a bulk query job, the Content-Type in the header for the request must be either text/csv or application/xml, depending on the content type specified when the job was created. The actual SOQL statement supplied for the batch will be in plain text format.
For queries on very large tables, use the PK Chunking header to split the query results into smaller chunks.
- URI
- https://instance_name—api.salesforce.com/services/async/APIversion/job/jobid/batch
- Bulk Query Request
-
1POST baseURI/job/750x00000000014/batch 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... 3Sforce-Enable-PK-Chunking: TRUE 4Content-Type: [either text/csv or application/xml depending on job] 5 6[plain text SOQL statement]
SOAP Headers, Requests, and Responses
The following are example Bulk Query requests and responses.
- Create Bulk Query Batch HTTP Request
-
1POST baseURI/job/750x00000000014/batch 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... 3Content-Type: text/csv; charset=UTF-8 4 5SELECT Name, Description__c FROM Merchandise__c - Create Bulk Query Batch HTTP Response Body
-
1<?xmlversion="1.0" encoding="UTF-8"?> 2<batchInfo 3 xmlns="http://www.force.com/2009/06/asyncapi/dataload"> 4 <id>751x00000000079AAA</id> 5 <jobId>750x00000000014</jobId> 6 <state>Queued</state> 7 <createdDate>2009-09-01T17:44:45.000Z</createdDate> 8 <systemModstamp>2009-09-01T17:44:45.000Z</systemModstamp> 9 <numberRecordsProcessed>0</numberRecordsProcessed> 10</batchInfo> - Get Batch Results HTTP Request
-
1GET baseURI/job/750x00000000014/batch/751x00000000030/result 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... - Get Batch Results HTTP Response Body
-
1<result-list xmlns="http://www.force.com/2009/06/asyncapi/dataload"><result>752x000000000F1</result></result-list> - Get Bulk Query Results HTTP Request
-
1GET baseURI/job/750x00000000014/batch/751x00000000030/result/752x000000000F1 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... - Get Bulk Query Results HTTP Response Body
-
1"Name", "Description__c" 2"Merchandise1", "Description for merchandise 1" 3"Merchandise2", "Description for merchandise 2"
Java Example Using WSC
The following example logs into an organization using the Partner API, then instantiates a BulkConnection object using the service endpoint from the Partner API login.
1public boolean login() {
2 boolean success = false;
3
4 String userId = getUserInput("UserID: ");
5 String passwd = getUserInput("Password: ");
6 String soapAuthEndPoint = "https://" + loginHost + soapService;
7 String bulkAuthEndPoint = "https://" + loginHost + bulkService;
8 try {
9 ConnectorConfig config = new ConnectorConfig();
10 config.setUsername(userId);
11 config.setPassword(passwd);
12 config.setAuthEndpoint(soapAuthEndPoint);
13 config.setCompression(true);
14 config.setTraceFile("traceLogs.txt");
15 config.setTraceMessage(true);
16 config.setPrettyPrintXml(true);
17 System.out.println("AuthEndpoint: " +
18 config.getRestEndpoint());
19 PartnerConnection connection = new PartnerConnection(config);
20 System.out.println("SessionID: " + config.getSessionId());
21 config.setRestEndpoint(bulkAuthEndPoint);
22 bulkConnection = new BulkConnection(config);
23 success = true;
24 } catch (AsyncApiException aae) {
25 aae.printStackTrace();
26 } catch (ConnectionException ce) {
27 ce.printStackTrace();
28 } catch (FileNotFoundException fnfe) {
29 fnfe.printStackTrace();
30 }
31 return success;
32 }
33
34 public void doBulkQuery() {
35 if ( ! login() ) {
36 retirn;
37 }
38 try {
39 JobInfo job = new JobInfo();
40 job.setObject("Merchandise__c");
41
42 job.setOperation(OperationEnum.query);
43 job.setConcurrencyMode(ConcurrencyMode.Parallel);
44 job.setContentType(ContentType.CSV);
45
46 job = bulkConnection.createJob(job);
47 assert job.getId() != null;
48
49 job = bulkConnection.getJobStatus(job.getId());
50
51 String query =
52 "SELECT Name, Id, Description__c FROM Merchandise__c";
53
54 long start = System.currentTimeMillis();
55
56 BatchInfo info = null;
57 ByteArrayInputStream bout =
58 new ByteArrayInputStream(query.getBytes());
59 info = bulkConnection.createBatchFromStream(job, bout);
60
61 String[] queryResults = null;
62
63 for(int i=0; i<10000; i++) {
64 Thread.sleep(i==0 ? 30 * 1000 : 30 * 1000); //30 sec
65 info = bulkConnection.getBatchInfo(job.getId(),
66 info.getId());
67
68 if (info.getState() == BatchStateEnum.Completed) {
69 QueryResultList list =
70 bulkConnection.getQueryResultList(job.getId(),
71 info.getId());
72 queryResults = list.getResult();
73 break;
74 } else if (info.getState() == BatchStateEnum.Failed) {
75 System.out.println("-------------- failed ----------"
76 + info);
77 break;
78 } else {
79 System.out.println("-------------- waiting ----------"
80 + info);
81 }
82 }
83
84 if (queryResults != null) {
85 for (String resultId : queryResults) {
86 bulkConnection.getQueryResultStream(job.getId(),
87 info.getId(), resultId);
88 }
89 }
90 } catch (AsyncApiException aae) {
91 aae.printStackTrace();
92 } catch (InterruptedException ie) {
93 ie.printStackTrace();
94 }
95 }