Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何让Salesforce REST API分页?_Python 3.x_Salesforce_Simple Salesforce - Fatal编程技术网

Python 3.x 如何让Salesforce REST API分页?

Python 3.x 如何让Salesforce REST API分页?,python-3.x,salesforce,simple-salesforce,Python 3.x,Salesforce,Simple Salesforce,我正在为salesforce REST API使用simple\u salesforcepython包装器。我们有数十万条记录,我想将salesforce数据的提取分开,这样就不会同时提取所有记录 我尝试传递一个查询,如: results = salesforce_connection.query_all("SELECT my_field FROM my_model limit 2000 offset 50000") 查看记录50K到52K,但收到一个错误,即偏移量只能用于前2000条

我正在为salesforce REST API使用
simple\u salesforce
python包装器。我们有数十万条记录,我想将salesforce数据的提取分开,这样就不会同时提取所有记录

我尝试传递一个查询,如:

    results = salesforce_connection.query_all("SELECT my_field FROM my_model limit 2000 offset 50000")

查看记录50K到52K,但收到一个错误,即偏移量只能用于前2000条记录。我怎样才能使用分页,这样我就不需要一次拉取所有记录了?

限制和偏移量并不是真的要这样使用的,如果有人在较早的位置插入或删除一条记录会怎么样(更不用说这里没有ORDER BY了)。SF将为您打开一个合适的,使用它

用于“查询”的文档说,您可以调用
query
,然后
query\u more
,也可以转到
query\u all
query\u all
将循环并继续调用
query\u more
,直到您耗尽光标-但这很容易消耗内存


或者看看批量查询的内容,API中有一些魔力,但我不知道它是否适合您的用例。这将是异步调用,可能不会在库中实现。它叫。除非你有数百万条记录,否则我不会打扰你。

限制和偏移量并不是真的要这样使用的,如果有人在较早的位置插入或删除一条记录会怎么样(更不用说你没有ORDER BY了)。SF将为您打开一个合适的,使用它

用于“查询”的文档说,您可以调用
query
,然后
query\u more
,也可以转到
query\u all
query\u all
将循环并继续调用
query\u more
,直到您耗尽光标-但这很容易消耗内存


或者看看批量查询的内容,API中有一些魔力,但我不知道它是否适合您的用例。这将是异步调用,可能不会在库中实现。它叫。除非你有数百万条记录,否则我不会打扰你。

你希望使用salesforce\u connection.query(query=SOQL)和.query\u more(nextRecordsUrl,True)

因为.query()只返回2000条需要使用的记录。查询\u更多以获取下一页的结果

SOQL查询通过以下方式完成:

如果由于一个特别大的结果,Salesforce在您的查询结果中添加了一个nextRecordsUrl,例如“nextRecordsUrl”:“/services/data/v26.0/query/01gD0000002HU6KIAW-2000”,您可以使用ID或完整URL(如果使用完整URL,则必须将“True”作为第二个参数传递)

下面是一个使用此方法的示例

 data = [] # list to hold all the records

 SOQL = "SELECT my_field FROM my_model"

 results = sf.query(query=SOQL) # api call

 ## loop through the results and add the records
 for rec in results['records']:
     rec.pop('attributes', None) # remove extra data
     data.append(rec) # add the record to the list


 ## check the 'done' attrubite in the response to see if there are more records
 ## While 'done' == False (more records to fetch) get the next page of records
 while(results['done'] == False):
     ## attribute 'nextRecordsUrl' holds the url to the next page of records
     results = sf.query_more(results['nextRecordsUrl', True])

     ## repeat the loop of adding the records
     for rec in results['records']:
         rec.pop('attributes', None)
         data.append(rec)
循环浏览记录并使用数据

 ## loop through the records and get their attribute values
 for rec in data:
     # the attribute name will always be the same as the salesforce api name for that value
     print(rec['my_field'])
正如另一个答案所说,这可能会开始消耗大量资源。但如果你想实现页面国家,这就是你想要的


也许可以创建一个更集中的SOQL语句,以便在特定时刻仅获取用例所需的记录。

您希望使用salesforce\u connection.query(query=SOQL)和.query\u more(nextRecordsUrl,True)

因为.query()只返回2000条需要使用的记录。查询\u更多以获取下一页的结果

SOQL查询通过以下方式完成:

如果由于一个特别大的结果,Salesforce在您的查询结果中添加了一个nextRecordsUrl,例如“nextRecordsUrl”:“/services/data/v26.0/query/01gD0000002HU6KIAW-2000”,您可以使用ID或完整URL(如果使用完整URL,则必须将“True”作为第二个参数传递)

下面是一个使用此方法的示例

 data = [] # list to hold all the records

 SOQL = "SELECT my_field FROM my_model"

 results = sf.query(query=SOQL) # api call

 ## loop through the results and add the records
 for rec in results['records']:
     rec.pop('attributes', None) # remove extra data
     data.append(rec) # add the record to the list


 ## check the 'done' attrubite in the response to see if there are more records
 ## While 'done' == False (more records to fetch) get the next page of records
 while(results['done'] == False):
     ## attribute 'nextRecordsUrl' holds the url to the next page of records
     results = sf.query_more(results['nextRecordsUrl', True])

     ## repeat the loop of adding the records
     for rec in results['records']:
         rec.pop('attributes', None)
         data.append(rec)
循环浏览记录并使用数据

 ## loop through the records and get their attribute values
 for rec in data:
     # the attribute name will always be the same as the salesforce api name for that value
     print(rec['my_field'])
正如另一个答案所说,这可能会开始消耗大量资源。但如果你想实现页面国家,这就是你想要的

也许可以创建一个更集中的SOQL语句,以便在特定时刻仅获取用例所需的记录