Created
August 7, 2023 09:49
-
-
Save A-pZ/fe8420b3e9f8388931eab4def39c1fe5 to your computer and use it in GitHub Desktop.
Bulk API 2.0で登録したクエリの結果を取得する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.repository; | |
import com.fasterxml.jackson.databind.MappingIterator; | |
import com.fasterxml.jackson.dataformat.csv.CsvMapper; | |
import com.fasterxml.jackson.dataformat.csv.CsvSchema; | |
import com.github.apz.salesforcesample.config.SalesforceProperties; | |
import com.github.apz.salesforcesample.model.AuthenticationResult; | |
import com.github.apz.salesforcesample.model.Company; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.http.MediaType; | |
import org.springframework.stereotype.Repository; | |
import org.springframework.web.reactive.function.client.WebClient; | |
import reactor.core.publisher.Mono; | |
import java.io.IOException; | |
import java.util.List; | |
@Repository | |
@AllArgsConstructor | |
@Slf4j | |
public class CompanyQueryRepository { | |
SalesforceProperties salesforceProperties; | |
WebClient salesforceWebClient; | |
public List<Company> bulkQueryExecute(String queryId, AuthenticationResult authenticationResult) { | |
String queryExecutionUrl = String.format("/jobs/query/%s/results?maxRecords=%d", queryId, 1000); | |
String result = salesforceWebClient.get() | |
.uri( salesforceProperties.getApplicationPath() + queryExecutionUrl) | |
.header("Authorization", authenticationResult.bearerToken()) | |
.retrieve() | |
.bodyToMono(String.class) | |
.block(); | |
log.info("result :{}", result); | |
return csvData(result); | |
} | |
List<Company> csvData(String csvData) { | |
CsvMapper csvMapper = new CsvMapper(); | |
CsvSchema csvSchema = csvMapper | |
.schemaFor(Company.class) | |
.withHeader(); | |
try { | |
MappingIterator<Company> objectMappingIterator = | |
csvMapper.readerFor(Company.class) | |
.with(csvSchema) | |
.readValues(csvData); | |
return objectMappingIterator.readAll(); | |
} catch (IOException e) { | |
throw new RuntimeException("CSVMapperでエラーが発生しました", e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.repository | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.test.context.SpringBootTest | |
import spock.lang.Specification | |
@SpringBootTest | |
class SalesforceBulkQueryJobRegisterTest extends Specification{ | |
@Autowired | |
SalesforceAuthentication authentication | |
@Autowired | |
CompanyQueryRepository repository | |
def "クエリジョブ作成"() { | |
when: | |
def authenticationResult = authentication.authentication() | |
String id = repository.createBulkQuery(authenticationResult) | |
then: | |
noExceptionThrown() | |
id | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INFO 26936 --- [ Test worker] c.g.a.s.r.CompanyQueryRepository : result : | |
"Id","Name","RecordTypeId" | |
"0010l00001UHnvBAAT","株式会社テスト","0120o000001JawLAAS" | |
"0010l00001UHnvGAAT","テスト施設","0120o000001TahCAAS" | |
"0010l00001akR7FAAU","株式会社テスト","0120o000001TahMAAS" | |
"0010l00001akR7eAAE","株式会社テスト","0120o000001JawLAAS" | |
"0010l00001akR7yAAE","テストパーク","0120o000001TahCAAS" | |
"0010l00001cxusZAAQ","テストコードから登録した会社","0120o000001TahMAAS" | |
"0010l00001cxvwjAAA","テストコードから登録した会社","0120o000001TahMAAS" | |
"0010l00001cxwy3AAA","テストコードから登録した会社","0120o000001TahMAAS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment