简体   繁体   中英

Perl module sapnwrfc to retrieve data from a huge SAP table with RFC_READ_TABLE

I'd like to use the Perl module sapnwrfc to retrieve data from a big SAP table (several million entries) to export it to a CSV file.

The idea was to use the function module RFC_READ_TABLE as following:

# Connect to SAP system
# [...]
my $rd = $conn->function_lookup("RFC_READ_TABLE");
my $rc = $rd->create_function_call;
$rc->QUERY_TABLE("/PLMB/AUTH_OBSID");
$rc->DELIMITER("@");
$rc->FIELDS([ {'FIELDNAME' => 'OBJECT_ID'}, {'FIELDNAME' => 'SID'} ]);
$rc->OPTIONS([{'TEXT' => 'OBJ_TYPE = \'PLM_DIR\''}]);  
$rc->invoke;

# Iterate over $rc-DATA and export it to CSV file
# [...]
$conn->disconnect;

The problem is that the script terminates with an out of memory error because the retrieved data exceeds the existing memory.

Is there a possibility to avoid this problem like a paging mechanism or something like that?

Based on a Python code snippet on SAP SCN I found a solution for my problem.

With the import parameters ROWSKIPS and ROWCOUNT of the function module RFC_READ_MODULE I can fetch data with chunks of rows:

# Meaning of ROWSKIPS and ROWCOUNT as parameters of function module RFC_READ_TABLE:
#
# For example, ROWSKIPS = 0, ROWCOUNT = 500 fetches first 500 records, 
# then ROWSKIPS = 501, ROWCOUNT = 500 gets next 500 records, and so on. 
# If left at 0, then no chunking is implemented. The maximum value to either of these fields is 999999.
my $RecordsCounter = 1;
my $Iteration = 0;
my $FetchSize = 1000;
my $RowSkips = 0;
my $RowCount = 1000;

# Open RFC connection
my $conn = SAPNW::Rfc->rfc_connect;

# Reference to function module call
my $rd = $conn->function_lookup("RFC_READ_TABLE");

# Reference to later function module call
my $rc;

# Loop to get data out of table in several chunks
while ($RecordsCounter > 0){

    # Calculate the already retrieved rows that need to be skipped
    $RowSkips = $Iteration * $FetchSize;

    # Reference to function module call
    $rc = $rd->create_function_call;

    # Table where data needs to be extracted
    $rc->QUERY_TABLE("/PLMB/AUTH_OBSID");

    # Delimeter between columns
    $rc->DELIMITER("@");

    # Columns to be retrieved
    $rc->FIELDS([ {'FIELDNAME' => 'OBJECT_ID'}, {'FIELDNAME' => 'SID'} ]);

    # SELECT criteria
    $rc->OPTIONS([{'TEXT' => 'OBJ_TYPE = \'PLM_DIR\''}]);

    # Define number of data to be retrieved
    $rc->ROWCOUNT($RowCount);

    # Define number of rows to be skipped that have been retrieved in the previous fetch
    $rc->ROWSKIPS($RowSkips);

    # Function call
    $rc->invoke;

    $Iteration++;

    # Data retrieved        
    if(defined $rc->DATA->[0]){ 

      print "Fetch $Iteration\n";

      foreach my $TableLine ( @{ $rc->DATA } ) {
        print "$TableLine->{WA}\n";
      }

  }

  # No more data to retrieve
  else{

    # Leave loop
    $RecordsCounter = 0;
  }

}

# Disconnect RFC connection
$conn->disconnect;

This is not what RFC_READ_TABLE is intended for. You will have to resort to some other extraction methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM