简体   繁体   中英

How do I use JdbcPagingItemReader in Spring Batch when my primary key is a string?

I've got some SQL data that I need to access with an ItemReader in Spring Batch. I thought that JdbcPagingItemReader / PagingQueryProvider would be the perfect fit.

The table I'm selecting from has a primary key that is a composite of three columns: INTEGER, VARCHAR, and VARCHAR. And actually, for this use case, the first two columns will be identical for every record in a given job. Thus, effectively it's as though the primary key column is a VARCHAR.

The interface definition requires this:

@Override
public String generateJumpToItemQuery(int itemIndex, int pageSize) {

}

I have to confess, I don't find the documentation to be particularly helpful in this case. It seems rather terse and makes a lot of assumptions. But here's what it says about that particular method:

Generate the query that will provide the jump to item query. The itemIndex provided could be in the middle of the page and together with the page size it will be used to calculate the last index of the preceding page to be able to retrieve the sort key for this row.

I don't like that the documentation assumes I know what a "jump to item query" is without defining that. Because... I don't! What's a "jump to item query"? I'm assuming that this is operating from the paradigm that there the table is ordered with a numeric ID? Or does this relate to the skip value in the LIMIT clause?

Any guidance you can offer is appreciated.

It's pretty clear from looking at the provided implementations. Here's PostgresPagingQueryProvider :

@Override
public String generateJumpToItemQuery(int itemIndex, int pageSize) {
    int page = itemIndex / pageSize;
    int offset = (page * pageSize) - 1;
    offset = offset<0 ? 0 : offset;
    String limitClause = new StringBuilder().append("LIMIT 1 OFFSET ").append(offset).toString();
    return SqlPagingQueryUtils.generateLimitJumpToQuery(this, limitClause);
}

So you're right, it relates to the skip value in the LIMIT clause.

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