首页 > 解决方案 > 将 Salesforce 批量 api 数据解析成块进行处理

问题描述

我有下面的代码,我从 Bulk APi 查询作业中获取结果,其中记录数量巨大(60,000 到 80,000)。我将其转换为 JSON 并将插入到数据库中。任何人都可以建议处理大量数据的最佳方法以及如何分块处理它。

request.get( options, function ( error, response, body ) {
        if ( error )
        {

        } else
        {
            csvJson()
                .fromString( response.body )
                .then( ( jsonObj ) => {
                    var a = JSON.stringify( jsonObj );
                } )
        }
    } );

标签: javascriptnode.jssalesforce-lightning

解决方案


async function* readApi() {
  let page = 1;
  while (page != null) {
    const r = await fetch(`http://target-api.com/stuff?page=${page}`)
    const d = await r.json()
    page = yield d
  }
}

const it = readApi()

it.next() // Init fn with first next call it will get the first page
it.next(2) // Gets the second page, process the data and go to next call
// ..
// ..
it.next(null) // When you are done with getting data call with null

推荐阅读