Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
vodela
Active Participant
 

Python has grown in popularity and the number of use cases has rapidly increased from Finance, big data and machine learning.   For those of us in the SAP it will be helpful to use the power of processing provided by Python at the same time retain the benefits and advantages of working in SAP.

In this blog, we will see how to use python and query odata from the ABAP developer edition using IntellJ PY Charm and show case the power of python in data analysis using a simple example  I There are many blogs that describe how to set up python and IntelliJ for development. One such blog is Python with Intellij.

To query odata you need to install pyodata from GitHub.  This is available from SAP Pyodata package.  Install the library as mentioned

It is assumed that you have SAP ABAP developer edition up and running. I am using Developer edition 7.52.

Once your setup is complete you can create new Python project in Py Charm and add a python file name it appy.py.  The  following code shows how to query ABAP system odata service.


# Import the requirements


import pyodata 
import pandas as pd
import requests



# Create a PyOData client instance

service_url = "http://vhcalnplci.dummy.nodomain:8000//sap/opu/odata/sap/EPM_REF_APPS_SHOP_SRV"
#we are querying Shopping service to analyse the review information


session = requests.Session()
session.auth = ('Developer', 'Down1oad')
#username and password for ABAP developer edition
client = pyodata.Client(service_url, session)




# Get the Products entity set and its entity type

entity_sets = client.entity_sets
products_entity_set=""
scalar_properties = set()
for es in client.schema.entity_sets:
if es.name == "Reviews":
proprties = es.entity_type.proprties()

for prop in proprties:
if prop.name == 'ProductId' or prop.name == 'Rating' or prop.name == 'HelpfulCount':
scalar_properties.add(prop.name)
print(scalar_properties)
reviews = client.entity_sets.Reviews.get_entities().execute()

# Create an empty list to store the dictionaries for each reviews
review_list = []

# Loop through each review entity and create a dictionary
for review in reviews:
review_dict = {}
for property_name in scalar_properties:
review_dict[property_name] = getattr(review, property_name)
review_list.append(review_dict)

# Convert the list of dictionaries to a pandas DataFrame
df = pd.DataFrame(review_list)
df=df.sort_index()
df2= df.groupby('ProductId').sum()


print(df2)



When you run the program you the output shown below




The above output shows the power of python for data analysis using a simple example. If we had to write an ABAP to do the same we would have to loop thru the output and sum reviews by product.


It will help if readers can comment and provide feedback and suggestions for future blogs.  If you are interested in ABAP connectivity please follow the ABAP Connectivity environment topic page (https://community.sap.com/topics/abap-connectivity),  Post and answer questions (https://answers.sap.com/tags/266264953119842772207986043063520), and read other posts on the topic (https://blogs.sap.com/tags/266264953119842772207986043063520/)


2 Comments