SalesForce和Pandas的整合

pandasforce的Python项目详细描述


潘达斯福斯

这是SalesForce和Pandas for Python的集成。它使用SalesForce的bulkapi将数据从Pandas DataFrames加载到SalesForce,并将SalesForce的数据加载到Pandas DataFrames中。有一个由push()和pull()函数组成的高级API以及更低级的实现。在

如果要处理多行数据,则鼓励使用bulkapi。bulkapi经过优化,甚至可以处理大量数据。如果您当前使用SalesForce的restapi来传输大量数据,那么您应该可以看到性能的显著提高。在

安装

您可以使用pip安装PandasForce pip install pandasforce

但是,如果决定导入源代码,请确保安装了以下依赖项:

  • 请求
  • 熊猫

使用

以下进口按给定值计算:

import pandas as pd
import pandasforce as pf

高级API

为了与您的orgs数据云交互,您需要通过登录来创建一个活动会话。假设您的用户帐户是“john.doe@test.com”,密码是“Test12345”,安全令牌等于“Hello123”。使用login(用户名、密码、令牌)功能创建活动会话:

^{pr2}$

现在,您可以使用push()函数更改SalesForce内部的数据,也可以使用pull()从SalesForce获取数据。假设您创建了一个包含潜在客户信息的pandas数据框,并希望将这些线索插入SalesForce:

companies = ["Test Inc", "Doe AG", "Mustermann KG"]
lnames = ["Musterfrau", "Doe","Mustermann"]
fnames = ["Eva", "John", "Max"]
leads = pd.DataFrame({"Company": companies,
		      "LastName": lnames,
		      "FirstName": fnames})
leads_insert = pf.push(operation = "insert", sfobject = "lead", data = leads,
		       session = session)

这将把数据插入SalesForce。push()函数接受以下参数:

operation: str
    Either one of 'insert', 'update', or 'delete'.

sfobject: str
    The name of the SalesForce object you want to operate on.

data: pandas.DataFrame or FilePath
    The data you want to push into SalsForce. This can either
    be a Pandas DataFrame or the path to a csv file. Note
    that if you give a csv file path, the data will be
    loaded into a Pandas DataFrame for you.

session: Session
    An active instance of the Session class. Use the login()
    function to create one. This object holds your credentials.

batch_size: int
    Your input will be split into batches of this size. This
    is done to speed up upload time.

sep: str
    If you give a csv file path for the data argument, this
    is the field separator used in your csv file.

encoding: str
    If you give a csv file path for the data argument, this
    is the file's encoding.

verbose: Boolean
    If set to True, you will receive further information
    about your workload. Very useful for debugging.

结果将是一个Pandas数据帧,其中包含您已推入SalesForce的数据的结果和id。在

如果决定查询驻留在SalesForce数据云中的数据,可以使用pull()函数。以下是函数的参数:

query: str
    An SOQL query you would like to run on the SalesForce
    DataCloud.

sfobject: str
    The name of the SalesForce object you want to query.

session: Session
    An active instance of the Session class. Use the login()
    function to create one. This object holds your credentials.

chunk_size: int
    Your output will be split into batches of this size. This
    is done to speed up download time. The final result will
    be a single Pandas DataFrame holding the data from all
    chunks.

verbose: Boolean
    If set to True, you will receive further information
    about your workload. Very useful for debugging. Note that
    you will receive a final status report. If everything worked,
    all batches will be 'Finished' but one. One batch will show
    the status 'Not Processed'. This is your query trigger and 
    it is completely expected.

结果将是一个Pandas数据帧,其中包含SOQL查询的结果。请注意,您的查询必须是有效的SOQL。SOQL只支持普通SQL命令的一个子集。假设我们要提取所有潜在客户的公司名称、名字和姓氏。此外,我们将使用先前定义的会话:

leads = pf.pull(query = "SELECT Company,FirstName,LastName FROM Lead", sfobject = "lead", session = session)

leads将是一个包含我们所有线索的常规Pandas数据框。在

低级API

如果希望对操作有更多的控制权,可以选择使用更低级的API。注意push()和pull()函数是简单的包装器。在

创建会话与创建高级api相同。通过登录创建会话:

^{pr2}$

下一步将是创建一个作业。这项工作将是我们与SalesForce的BulkAPI交互的点。create_job()函数采用以下参数:

operation: str
    One of either 'insert', 'update', 'delete', or 'query'.

sfobject: str
    The name of the SalesForce object you want to operate on.

session: Session
    An active instance of the Session class. Use the login()
    function to create one. This object holds your credentials.

chunk_size: int
    If your operation is 'query', the results will be split
    into multiple batches of size equal to 'chunk_size'.
    This will increase performance for large chunks of data
    since it will be downloaded in separate chunks. Will be
    ignored, if operation is not 'query'.

假设您想在SalesForce中插入一些潜在客户。首先,我们需要在登录后创建作业:

job = pf.create_job(operation = "insert", sfobject = "lead", session = session)

接下来,我们需要将一些数据作为批插入到我们的作业中。这可以通过使用add_batch()方法来完成,该方法接受Pandas数据帧或csv文件的路径。在

df = pd.DataFrame({"Company": ["A", "B", "C"],
		   "FirstName": ["John", "Jack", "Sarah"],
		   "LastName": ["Doe", "Smith", "Miller"]}) 
job.add_batch(df)

请注意,我们鼓励将数据拆分为多个批次。这将加快处理速度。此外,您的批次不得超过10000个观察值。在

当所有批处理都添加到作业中时,必须使用close()方法关闭作业。在

job.close()

您可以通过运行get_status()方法来查找作业的进度。这将返回字典列表。每个字典表示一个批处理,包含当前状态以及已处理多少项的信息。如果您正在查询数据,请注意,始终有一个批次的状态为“未处理”,并且已处理的项目为0。这是创建所有其他批的初始批。在

job.get_status()

当所有批次处理成功时,可以通过调用get_results()方法获取结果。结果将是一个Pandas数据帧,其中包含插入、更新和删除操作的id。如果您的作业是一个查询,它将保存SOQL查询的结果。在

job.get_results()

参考文献

这个模块是使用SalesForce的官方Bulk API Documentation构建的。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Android同步不同页面上的按钮   java评测每个类收集的垃圾对象实例数   java(Spring MVC+Hibernate 4+Test 4)自动连线DAO返回NULL   java Android编辑文本和虚拟键盘   java Selenium与BrowserMobProxy   JAVAlang.NoClassDefFoundError:com/sun/jersey/spi/inject/Errors$关闭原因?   java为什么在我成功登录后仍然会出现“不正确的帐户或密码或用户类型”   安卓应用程序在重新启动java时崩溃。网UnknownHostException:无法解析主机   多线程在Java中同步共享静态对象的正确方法是什么?   未调用自定义注释的java类验证(约束类)   java如何将指定目录的存档文件放入所需位置?   java如何识别Selenium中的每个编辑文本字段,如果它们的Xpath都相同   使用gwtmockito/mockito的java简单单选按钮单元测试?