Skip to Content
avatar image
Former Member

Iterate through all the records in a table using Java API

Hi All,

What is the easiest way to iterate through all the records in a given table using Java API? I cannot find any methods that will return all records in a table and the only way I can use is to perform a free form search with a condition that is always true. The code works but is pretty ugly. Is there an alternative to this approach?

Thanks!

Kenny

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Best Answer
    May 16, 2007 at 08:30 PM

    Try this

    RetrieveLimitedRecordsCommand retrieveRecordsCmd = new RetrieveLimitedRecordsCommand(ConnectionPoolFactory.getInstance(<pass your parameter>));
    
    retrieveRecordsCmd.setSearch(new Search(new TableId(<pass your table code>)));
    
    ResultDefinition resultDefinition = new ResultDefinition(<define your resultsetdefinition>);
    
    retrieveRecordsCmd.setResultDefinition(resultDefinition);
    retrieveRecordsCmd.setSession(<set user session>);
    retrieveRecordsCmd.execute();

    now get your resultset and iterate through all the records...

    -Y

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for your reply. I am using MDM4J. Your codes are using another set of APIs. Is MDM4J obsoleted?

  • avatar image
    Former Member
    May 17, 2007 at 02:32 AM

    MDM4J is deprecated with SP05, no further development will be provided for MDM4J.

    It's replaced by the MDM JAVA API, introduced with SP03.

    Regards Klaus

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      So where can I find the latest documentations and examples on the new Java APIs? A lot of existing documentations are only for MDM4J.

      Thanks again!

      Kenny

  • avatar image
    Former Member
    May 17, 2007 at 03:01 PM

    Hi Kenny,

    You can construct a new Search object with your table's code name, a new ResultSetDefinition object for your table and just execute this search using the GetResultSet method of CatalogData.

    Please look at the following code:

    Search search = new Search(<code name of your table>);
    ResultSetDefinition rsd = new ResultSetDefinition(<code name of your table>);
    rsd.AddField<code name of a field>);
    rsd.AddField(<code name of a field>);
    .
    .
    .
    String sortField = <code name of your sort field>;
    boolean sortAscending = true;
    int page = 0; //page number
    A2iResultSet rs = <your CatalogData object>.GetResultSet(search, rsd, sortField, sortAscending, page);
    
    for (int i = 0; i < rs.GetRecordCount(); i++)
    {
        Value fieldValue = rs.GetValueAt(i, <code name of a field>);
        .
        .
        .
    }

    Hope this helps,

    Nir

    PS - I really recommend you to start using the new API, as it is much more efficient and straight-forward.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 17, 2007 at 03:08 PM

    Hi David,

    Will the new MDM Java API released with SP05 be able to access Image and PDF data stored in the repository?

    Best Regards,

    Mark

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 17, 2007 at 05:48 PM

    Under http://service.sap.com/installMDM you can find the current documentation for MDM. In the "Operations"-section there's a link to the JAVA API documentation and as well a tutorial.

    Additional coding samples come in the package com.sap.mdm.examples in the online documentation.

    Regards Klaus

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 23, 2007 at 12:35 PM

    Hello all,

    I'm using the new JavaAPI on SP04 and did a "search" on a lookup table to retrieve all records.

    ...

    retrieveLimitedRecordsCommand.execute();

    result = retrieveLimitedRecordsCommand.getRecords();

    But it turns out, that just exactly 1000 out of the almost 5000 records are returned as a result..! Everything works fine, so there is no error message or something like that. I also did not limit the search and connect with the Admin user (no limitations).

    Anybody worked with "mass data"...!? Any help is appreciated!

    Thanks,

    Andreas

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Please take a look at the Javadoc for the command RetrieveLimitedRecordsCommand. It has two methods setPageSize and setPageIndex.

      In general, you should page through your 5000 records, perhaps 1000 records at a time. Depending on the data you are retrieving, you will need to have enough memory to hold the data. This is why paging is the way to go.

  • avatar image
    Former Member
    Oct 09, 2007 at 02:09 PM

    Hi,

    I have same problem. I have 1 million products table and need to get information for all.

    Currently, I can get only 1000 records with RetrieveLimitedRecordsCommand.

    In the second iteration of this loop, metaData has null value. Is this normal? Metadata is returned only on first iteration?

    I haven't tested what value records array will have in the second iteration.

    for (int pageIndex = 0; pageIndex < MAX_PAGE_SIZE; pageIndex++) {

    retrieve.setPageIndex(pageIndex);

    RecordResultSet resultSet = retrieve.getRecords();

    MetadataItem metaData = resultSet.getMetadata();

    //here metaData has null value in the second iteration of above for loop

    Record[] records = resultSet.getRecords();

    }

    Thanks

    Erol Akarsu

    Add comment
    10|10000 characters needed characters exceeded