python获取sap数据_使用python使用pyrfc将数据插入到SAP中
python获取sap数据
Today we are seeing the daily increase of Machine Learning (ML) development and the largest development tool is Python and each day more and more people are trying to return the results of the ML models into their Enterprise Resource Planning systems (ERPs).
今天,我们看到机器学习(ML)开发的每天都在增加,最大的开发工具是Python,并且每天都有越来越多的人试图将ML模型的结果返回到其企业资源计划系统(ERP)中。
SAP is one of the most used ERPs systems and to insert the results of an ML model into it we have two options, manually in a custom SAP transaction or using a Remote Function Call (RFC) which is the standard SAP interface for communication between SAP systems and other systems, so RFC calls a function to be executed in a remote system.
SAP是最常用的ERP系统之一,要在其中插入ML模型的结果,我们有两个选择,可以在自定义SAP事务中手动进行,也可以使用远程功能调用(RFC),后者是SAP之间进行通信的标准SAP接口系统和其他系统,因此RFC调用要在远程系统中执行的功能。
Let’s say that you can automate the upload of the result file in the transaction with a Robotic Process Automation system (RPA), so then which one we should choose?
假设您可以使用Robotic Process Automation系统(RPA)在事务中自动上传结果文件,那么我们应该选择哪一个呢?
Well, if I had to choose one of them I would pick the second option because it tends to vary less and have fewer updates than the first one, so you probably would have fewer problems with unnoticed updates.
好吧,如果我必须选择其中一个,那么我会选择第二个选项,因为它与第一个选项相比变化较小且更新较少,因此,您可能会注意到较少的更新问题。
One important thing to point is that RFC has a lot of goals and ways to be developed and the one we want to use is to insert data into tables
重要的一点是,RFC有很多目标和开发方法,而我们要使用的目标是将数据插入表中
We can do that with Python using the PyRFC package, but the first use of it is not so simple, so to help with that I’ve created some functions and documentations.
我们可以使用PyRFC软件包在Python中完成此操作,但是第一次使用并不是那么简单,因此为了帮助我创建了一些函数和文档。
Let’s begin!
让我们开始!
装置 (Installations)
-第一步:下载SAP NW RFC SDK (- First Step: Download SAP NW RFC SDK)
SAP NW RFC SDK is the abbreviation for SAP NetWeaver Remote Function Call System Development Kit, this lets you manage the RFCs.
SAP NW RFC SDK是SAP NetWeaver远程功能调用系统开发工具包的缩写,它使您可以管理RFC。
I’ve found a really good manual from Informatica of how to download it, but it’s for an older version and some icons are quite different now, so I’ve written the steps and reprinted the images to have it up to date.
我发现Informatica的一本非常好的手册 如何下载它,但是它是针对较旧的版本的,并且有些图标现在已经大不相同了,因此我编写了步骤并重新打印了图像以使其具有最新性。
If you want to see the manual of Informatica, it is linked at the end of the article as a Source.
如果要查看Informatica手册,则将其作为源链接在文章末尾。
One difficult thing about downloading this content is that you need an SAP account to do it, so try to get one before starting this journey.
下载此内容的一件困难事是您需要一个SAP帐户才能执行此操作,因此在开始此旅程之前,请尝试获取一个。
Coming back, the download steps:
返回,下载步骤:
Go to the SAP Service Marketplace: http://service.sap.com/support
转到SAP Service Marketplace: http : //service.sap.com/support
- Enter your SAP Service Marketplace user name and password. The SAP Support Portal page appears.输入您的SAP Service Marketplace用户名和密码。 出现“ SAP支持门户”页面。
- Click the Download Software icon.单击下载软件图标。
Observation, once you are logged in the user icon changes its color to blue
观察,一旦登录,用户图标的颜色就会变为蓝色
- Click the Support Packages & Patches tab.单击支持包和补丁选项卡。
- Click By Category from the list.从列表中单击按类别。
- Click Additional Components from the list.从列表中单击其他组件。
- Click SAP NW RFC SDK from the list.从列表中单击SAP NW RFC SDK。
- Click on the most recent version of SAP NW RFC SDK.单击最新版本的SAP NW RFC SDK。
- Select the operating system for which you want to download选择要下载的操作系统
- Click the hyperlinked .zip file to download libraries.单击超链接的.zip文件以下载库。
- Extract the files and folders that are inside the .zip to your SAP folder将.zip内的文件和文件夹提取到您的SAP文件夹中
The highlighted folders and files were what was inside of the .zip.
突出显示的文件夹和文件就是.zip文件的内容。
Once you’ve done that everything is set from the SAP side.
完成后,一切都从SAP端设置。
-第二步:安装软件包 (- Second Step: Install packages)
It’s a little bit easier than the last step, just write and run, separately, these two lines on your Python terminal.
这比最后一步要容易一些,只需在Python终端上分别编写和运行这两行即可。
pip install pyrfcpip install pynwrfc
I think you might only need to run the second one, but I’ve installed both of them and I prefer to say to you to install them too instead of you not being able to use the PyRFC package.
我认为您可能只需要运行第二个,但是我已经安装了它们两个,并且我想告诉您也安装它们,而不是不能使用PyRFC软件包。
编码 (Coding)
Cool, now you’ve reached the interesting part!
太酷了,现在您已经达到了有趣的部分!
Obs: If you have to turn on your Virtual Private Network (VPN) to access SAP you will also need to have it turned on when using these functions.
观察:如果必须打开虚拟专用网络(VPN)才能访问SAP,则在使用这些功能时也需要将其打开。
For this development, we’ll have two files: constants.py and sap_rfc_connection.py.
对于此开发,我们将有两个文件: 常量 .py和sap_rfc_connection .py。
Inside constants.py we store the three things, the dictionary with SAP logon credentials, RFC function and table names, and the number of rows you are going to insert per RFC call.
在常量 .py中,我们存储了三件事:带有SAP登录凭证的字典,RFC函数和表名以及每个RFC调用要插入的行数。
Now, in sap_rfc_connection.py we have two main functions:
现在,在sap_rfc_connection .py中,我们有两个主要功能:
- rfc_func_descrfc_func_desc
def rfc_func_desc(dict_sap_con, func_name):
'''consult the RFC description and needed input fields
Parameters
----------
dict_sap_con : dict
key to create connection with SAP, must contain: user, passwd, ashost, sysnr, client
func_name : str
name of the function that you want to verify
Returns
-------
funct_desc : pyrfc.pyrfc.FunctionDescription
RFC functional description object
'''
print(f'{time.ctime()}, Start getting function description from RFC')
print(f'{time.ctime()}, Start SAP connection')
# create connection with SAP based on data inside the dict
with Connection(**dict_sap_con) as conn:
print(f'{time.ctime()}, SAP connection stablished')
# get data from the desired RFC function
funct_desc = conn.get_function_description(func_name)
# display the information about the RFC to user
display(funct_desc.parameters[0],funct_desc.parameters[0]['type_description'].fields)
# return it as a variable
return funct_desc
# how the whole command is inside the 'with' when it ends the connection with SAP is closed
print(f'{time.ctime()}, SAP connection closed')
print(f'{time.ctime()}, End getting function description from RFC')
Now an example of how it works, the main goal of this function is to let you get the whole RFC description and stores it into a variable so you can consult it later (and you also can check PyDrive documentation to see all sort of things you can do with it), and display to you what is needed to use the RFC like fields names and data types.
现在是其工作方式的一个示例,此功能的主要目标是让您获得完整的RFC描述并将其存储到变量中,以便以后可以查阅(并且您还可以查看PyDrive文档以查看各种内容)。可以使用它),并向您显示使用RFC所需的内容,例如字段名称和数据类型。
This is useful because with them we know the structure/columns names of your data frame and the data type you must use in each field to not get an error.
这很有用,因为有了它们,我们知道了数据框的结构/列名称以及必须在每个字段中使用的数据类型,以免出错。
In my case, I’ve created this data frame bellow to test.
就我而言,我已经创建了这个数据框进行测试。
After the data frame creation, we can continue to the second function.
创建数据框后,我们可以继续第二个功能。
- df_to_sap_rfcdf_to_sap_rfc
def df_to_sap_rfc(df, dict_sap_con, func_name, rfc_table):
'''ingest data that is in a data frame in SAP using a defined RFC, checking if the dataframe has the same size, column names and data types
Parameters
----------
df : pandas.DataFrame
dataframe that is going to be used to insert data to SAP
dict_sap_con : dict
dictionary with SAP logon credentials (user, passwd, ashost, sysnr, client)
func_name : string
name of the RFC function
rfc_table : string
name of the rfc table you are going to populate
Returns
-------
None
'''
# get needed parameters from RFC
lst_param = get_rfc_parameters(dict_sap_con, func_name)
# check dataframe input
check_input_format(df, lst_param)
# insert data
lst_res = insert_df_in_sap_rfc(
df, dict_sap_con, func_name, rfc_table)
This function wraps another three methods inside of it, but to not waste too much time on them the first two are to check if the data frame follows the RFC requested format (column names and data types), and if doesn’t have it stops the execution to not insert wrong data into SAP.
此函数在其中封装了另外三个方法,但是为了不浪费太多时间,前两个方法是检查数据帧是否遵循RFC请求的格式(列名和数据类型),以及是否没有停止不会将错误数据插入SAP的执行。
The real “magic” happens inside the third one, the insert_df_in_sap_rfc function:
真正的“魔术”发生在第三个内部,insert_df_in_sap_rfc函数:
def insert_df_in_sap_rfc(df, dict_sap_con, func_name, rfc_table):
'''Ingest data that is in a data frame in SAP using a defined RFC
Parameters
----------
df : pandas.DataFrame
dataframe that is going to be used to insert data to SAP
dict_sap_con : dict
dictionary with SAP logon credentials (user, passwd, ashost, sysnr, client)
func_name : string
name of the function that you want to remotelly call
rfc_table : string
name of the table which your RFC populates
Returns
-------
lst_res : list
list of dictionaries with field names and data types used in RFC
'''
print(f'{time.ctime()}, Start data ingestion to SAP process')
# create an empty list that is going to recive the result
lst_res = []
# get the quantity of rows of the dataframe
rows_qty = len(df)
# define the number of execution, getting the entire part of the division and
# adding 1 to it, to execute the last rows that don't achieve the quantity of
# an extra execution
iter_qty = (rows_qty // c.rows_per_exec) + 1
print(f'{time.ctime()}, Start SAP connection')
# create connection with SAP based on data inside the dict
with Connection(**dict_sap_con) as conn:
print(f'{time.ctime()}, SAP connection stablished')
# for each iteration
for i in range(iter_qty):
# define the first and last row for this execution
f_r = i*c.rows_per_exec
l_r = min((i+1)*c.rows_per_exec, rows_qty)
# define an auxiliar dataframe with only the rows of this iteration
df_aux = df.iloc[f_r:l_r]
print(f'{time.ctime()}, Rows defined')
# convert this dataframe to a json format, oriented by records
# this is the needed format to do a multirow input with a RFC
# by last all the json data must be inside of a list
lst_dicts_rows = eval(df_aux.to_json(orient='records'))
# once we have the desired rows well formatted we must tell for
# which table we are going to insert it
dict_insert = {rfc_table: lst_dicts_rows}
print(f'{time.ctime()}, RFC input format applied')
print(f'{time.ctime()}, Start sending rows {f_r} to {l_r-1}')
# with everything set just call the RFC by its name
# and pass the connection dict
try:
result = conn.call(func_name, **dict_insert)
exec_ind = True
except:
result = None
exec_ind = False
print(f'{time.ctime()}, Rows {f_r} to {l_r-1} sent')
# save the row's numbers, execution indicator and the result of the call in the list
# as a dict
lst_res.append({'row':f'{f_r}_{l_r-1}', 'exec_ind':exec_ind, 'rfc_result':result})
# how the whole command is inside the 'with' when it ends the connection with SAP is closed
print(f'{time.ctime()}, SAP connection closed')
print(f'{time.ctime()}, End data ingestion to SAP process')
return lst_res
Long story short, this piece of code gets the number of rows per execution that you define in constants.py and calculate the number of iterations to fully insert your data frame into SAP.
长话短说,这段代码获取了您在constants.py中定义的每次执行的行数,并计算了将数据帧完全插入SAP的迭代数。
After that, it is going to split your data frame into slices, convert them to a .json oriented by records format and put all inside a list, and to finish, it is stored inside of a dictionary whose key is the RFC table name.
之后,它将把您的数据帧分割成片,将它们转换为以记录格式为导向的.json并将其全部放入列表中,最后,将其存储在以RFC表名为键的字典中。
This come and go might be a little confusing, but it creates a dictionary that lets us insert multiple rows at once to an SAP table.
这种来去去去可能有点令人困惑,但是它创建了一个字典,使我们可以一次将多个行插入到SAP表中。
In the end, if everything was set well you will have something like this:
最后,如果一切设置正确,您将获得以下内容:
If you got interested in these files you can see them in my Git Hub with the link below:
如果您对这些文件感兴趣,可以在我的Git Hub中通过以下链接查看它们:
If you are thinking to use PyRFC in a process that you must repeat it periodically, perhaps this article bellow may help you to schedule it.
如果您打算在必须定期重复的过程中使用PyRFC,那么下面的文章也许可以帮助您安排它。
就是这样! (And this is it!)
Well, with this I think you can now use Python and RFC to insert data into SAP with a little more ease!
好吧,我想您现在可以使用Python和RFC轻松地将数据插入SAP!
I hope this article has helped you!
希望本文对您有所帮助!
Special thanks to Leonardo Ramos and Anderson Tessitori who put up with me while I cleared a million RFC questions with them
特别感谢Leonardo Ramos和Anderson Tessitori在我与他们共同解决了100万个RFC问题时表示支持
资料来源: (Sources:)
https://sap.github.io/PyRFC/client.html#examples
https://sap.github.io/PyRFC/client.html#examples
https://sap.github.io/PyRFC/install.html#sap-nw-rfc-sdk-installation
https://sap.github.io/PyRFC/install.html#sap-nw-rfc-sdk-installation
翻译自: https://towardsdatascience.com/using-python-to-insert-data-into-sap-with-pyrfc-5eb2ceeda79b
python获取sap数据
文章来自于网络,如果侵犯了您的权益,请联系站长删除!