一括クエリの使用
一括クエリジョブにバッチを追加するときに、要求のヘッダーの Content-Type には、ジョブの作成時に指定されたコンテンツタイプに応じて、text/csv、application/xml、または application/json のいずれかを指定する必要があります。バッチに使用される実際の SOQL ステートメントは、平文テキスト形式で表されます。
- URI
- https://instance_name—api.salesforce.com/services/async/APIversion/job/jobid/batch
- 一括クエリの要求
-
1POST baseURI/job/750x00000000014/batch 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... 3Content-Type: [either text/csv, application/xml, or application/json depending on job] 4 5[plain text SOQL statement]
要求および応答
一括クエリの要求と応答の例を次に示します。
- 一括クエリバッチ作成の HTTP 要求
-
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 - 一括クエリバッチ作成の HTTP レスポンスボディ
-
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 <numberRecordsFailed>0</numberRecordsFailed> 11 <totalProcessingTime>0</totalProcessingTime> 12 <apiActiveProcessingTime>0</apiActiveProcessingTime> 13 <apexProcessingTime>0</apexProcessingTime> 14</batchInfo> - ジョブの HTTP 要求内のすべてのバッチに関する情報の取得 (PK Chunking が有効な場合に使用)
-
1GET baseURI/job/750x00000000014/batch 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... - ジョブの HTTP レスポンスボディ内のすべてのバッチに関する情報の取得
-
1<?xml version="1.0" encoding="UTF-8"?><batchInfoList 2 xmlns="http://www.force.com/2009/06/asyncapi/dataload"> 3 <batchInfo> 4 <id>751D00000004YjwIAE</id> 5 <jobId>750D00000004T5OIAU</jobId> 6 <state>NotProcessed</state> 7 <createdDate>2011-03-10T00:59:47.000Z</createdDate> 8 <systemModstamp>2011-03-10T01:00:19.000Z</systemModstamp> 9 <numberRecordsProcessed>0</numberRecordsProcessed> 10 <numberRecordsFailed>0</numberRecordsFailed> 11 <totalProcessingTime>0</totalProcessingTime> 12 <apiActiveProcessingTime>0</apiActiveProcessingTime> 13 <apexProcessingTime>0</apexProcessingTime> 14 </batchInfo> 15 <batchInfo> 16 <id>751D00000004Yk1IAE</id> 17 <jobId>750D00000004T5OIAU</jobId> 18 <state>Completed</state> 19 <createdDate>2011-03-10T00:59:47.000Z</createdDate> 20 <systemModstamp>2011-03-10T01:00:19.000Z</systemModstamp> 21 <numberRecordsProcessed>100000</numberRecordsProcessed> 22 <numberRecordsFailed>0</numberRecordsFailed> 23 <totalProcessingTime>1000</totalProcessingTime> 24 <apiActiveProcessingTime>1000</apiActiveProcessingTime> 25 <apexProcessingTime>0</apexProcessingTime> 26 </batchInfo> 27 <batchInfo> 28 <id>751D00000004Yk2IAE</id> 29 <jobId>750D00000004T5OIAU</jobId> 30 <state>Completed</state> 31 <createdDate>2011-03-10T00:59:47.000Z</createdDate> 32 <systemModstamp>2011-03-10T01:00:19.000Z</systemModstamp> 33 <numberRecordsProcessed>100000</numberRecordsProcessed> 34 <numberRecordsFailed>0</numberRecordsFailed> 35 <totalProcessingTime>1000</totalProcessingTime> 36 <apiActiveProcessingTime>1000</apiActiveProcessingTime> 37 <apexProcessingTime>0</apexProcessingTime> 38 </batchInfo> 39 <batchInfo> 40 <id>751D00000004Yk6IAE</id> 41 <jobId>750D00000004T5OIAU</jobId> 42 <state>Completed</state> 43 <createdDate>2011-03-10T00:59:47.000Z</createdDate> 44 <systemModstamp>2011-03-10T01:00:19.000Z</systemModstamp> 45 <numberRecordsProcessed>100000</numberRecordsProcessed> 46 <numberRecordsFailed>0</numberRecordsFailed> 47 <totalProcessingTime>1000</totalProcessingTime> 48 <apiActiveProcessingTime>1000</apiActiveProcessingTime> 49 <apexProcessingTime>0</apexProcessingTime> 50 </batchInfo> 51 <batchInfo> 52 <id>751D00000004Yk7IAE</id> 53 <jobId>750D00000004T5OIAU</jobId> 54 <state>Completed</state> 55 <createdDate>2011-03-10T00:59:47.000Z</createdDate> 56 <systemModstamp>2011-03-10T01:00:19.000Z</systemModstamp> 57 <numberRecordsProcessed>50000</numberRecordsProcessed> 58 <numberRecordsFailed>0</numberRecordsFailed> 59 <totalProcessingTime>500</totalProcessingTime> 60 <apiActiveProcessingTime>500</apiActiveProcessingTime> 61 <apexProcessingTime>0</apexProcessingTime> 62 </batchInfo> 63</batchInfoList> - バッチ結果取得の HTTP 要求
-
1GET baseURI/job/750x00000000014/batch/751x00000000030/result 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... - バッチ結果取得の HTTP レスポンスボディ
-
1<result-list xmlns="http://www.force.com/2009/06/asyncapi/dataload"><result>752x000000000F1</result></result-list> - 一括クエリ結果取得の HTTP 要求
-
1GET baseURI/job/750x00000000014/batch/751x00000000030/result/752x000000000F1 2X-SFDC-Session: 4f1a00D30000000K7zB!ARUAQDqAHcM... - 一括クエリ結果取得の HTTP レス���ンスボディ
-
1"Name", "Description__c" 2"Merchandise1", "Description for merchandise 1" 3"Merchandise2", "Description for merchandise 2"
WSC を使用した Java の例
次の例では、Partner API を使用して組織にログインし、Partner API ログインからサービスエンドポイントを使用して BulkConnection オブジェクトをインスタンス化します。
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 return;
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(30000); //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 }