pythonsaprfcunwrapsap-bwpyrfc

Get SAP BW infocube data with pyrfc RSDRI_INFOPROV_READ_RFC


I am trying to get data from an SAP BW infocube using Python with pyrfc. I am currently using the ABAP function RSDRI_INFOPROV_READ_RFC, but when I print the result I am unable to get something consistent to exploit.

Below my python code :

import pyrfc
import pandas as pd
import pprint

from pyrfc import Connection, ABAPApplicationError, ABAPRuntimeError, LogonError, CommunicationError

ASHOST = "myashost"
SYSNR = "xx" 
CLIENT = 'xx'
USER = "my_user"
PASSWD = "mymdp"
lang= 'EN'

i_th_sfc = [
  {"CHANM": "/CPMB/WVDU6SJ",
   "CHAALIAS": "AUDITTRAIL",
   },
   {"CHANM": "/CPMB/WVD9OS5",
   "CHAALIAS": "TIME",
   },
   {"CHANM": "/CPMB/WVD9X7E",
   "CHAALIAS": "CONTRIBUTOR",
   },
    {"CHANM": "/CPMB/WVDYXDT",
   "CHAALIAS": "RUBRIC",
   }

] 

i_th_sfk = [
  {"KYFNM": "/CPMB/SDATA",
   "KYFALIAS" : "KPI",
   "AGGR" : "SUM"
},
]
]

conn = pyrfc.Connection(ashost=ASHOST, sysnr=SYSNR, client=CLIENT, user=USER, passwd=PASSWD, lang= lang)

result = conn.call("RSDRI_INFOPROV_READ_RFC",#RFC_READ_TABLE", 
  I_INFOPROV = "/CPMB/WVIKBIP"#"cube name
  ,I_T_SFC = i_th_sfc #: dimensions
  ,I_T_SFK = i_th_sfk #: measures
  , I_MAXROWS = 2
)
pprint.pprint(result, width=100, compact=True)

And I get this result :

{'E_AGGREGATE': '/CPMB/WVIKBIP',
 'E_END_OF_DATA': 'X',
 'E_RFCDATA_UC': b'',
 'E_SPLIT_OCCURRED': '',
 'E_STEPUID': '8N9KNHSSFKVVE28GD2LCKFCT0',
 'E_T_RFCDATA': [{'CONT': 'X',
                  'LINE': '000000000000000                              '
                          'IMPORT_CONSO_BFC                HL001'},
                 {'CONT': 'X',
                  'LINE': '                                                                                                                                              '
                          'RC11030                                                                                         '
                          '2023.06'},
                 {'CONT': 'X', 'LINE': ''},
                 {'CONT': 'X', 'LINE': '                                           000'},
                 {'CONT': 'X', 'LINE': ''}, {'CONT': 'X', 'LINE': ''},
                 {'CONT': '',
                  'LINE': '                                                                              '
                          '\x00\x00\x00\x00\x00ఀ                              \x00\x01'},
                 {'CONT': 'X',
                  'LINE': '000000000000000                              '
                          'IMPORT_STR_BFC                  HL001'},
                 {'CONT': 'X',
                  'LINE': '                                                                                                                                              '
                          'RC11030                                                                                         '
                          '2023.06'},
                 {'CONT': 'X', 'LINE': ''},
                 {'CONT': 'X', 'LINE': '                                           000'},
                 {'CONT': 'X', 'LINE': ''}, {'CONT': 'X', 'LINE': ''},
                 {'CONT': '',
                  'LINE': '                                                                              '
                          '\x00\x00\x00\x00\x00ఀ                              \x00\x01'}],
 'E_T_RFCDATAV': [],
 'I_T_RANGE': [],
 'I_T_REQUID': [],
 'I_T_RTIME': [],
 'I_T_SFC': [{'CHAALIAS': 'AUDITTRAIL', 'CHANM': '/CPMB/WVDU6SJ', 'ORDERBY': 0},
             {'CHAALIAS': 'TIME', 'CHANM': '/CPMB/WVD9OS5', 'ORDERBY': 0},
             {'CHAALIAS': 'CONTRIBUTOR', 'CHANM': '/CPMB/WVD9X7E', 'ORDERBY': 0},
             {'CHAALIAS': 'RUBRIC', 'CHANM': '/CPMB/WVDYXDT', 'ORDERBY': 0}],
 'I_T_SFK': [],
 'I_T_TABLESEL': []}

I can see the data that I want in 'E_T_RFCDATA' but they are printed in a weird manner. For example, I must have 5 columns : AUDITTRAIL, TIME , CONTRIBUTOR , RUBRIC and KPI and the first row must contains respectively : IMPORT_CONSO_BFC, 2023.06, RC11030, HL001 and an amount.

In the printing the fields are rendered in random order, I can't see any amount and I have weird characters.

I have tried to use this ABAP function in SAP and the result is presented correctly.

Anyone knows how to get data from an SAP BW infocube correctly in Python ?

EDIT :

After analysing the data in sap I can see that another function is used to structure the data correctly which is RSDRI_DATA_UNWRAP, unfortunately, I can't use it because it's not an RFC function. Is there any equivalent that I can use in Python ?


Solution

  • I've found how to get the data correctly. I've just added the parameter I_RESULTTYPE that I've set to 'V' and the data is encoded very well.

    I've found the solution in this forum : https://www.sapboard.ru/forum/viewtopic.php?p=371006