简体   繁体   English

当我的主键是字符串时,如何在Spring Batch中使用JdbcPagingItemReader?

[英]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. 我有一些SQL数据,我需要使用Spring Batch中的ItemReader访问。 I thought that JdbcPagingItemReader / PagingQueryProvider would be the perfect fit. 我认为JdbcPagingItemReader / PagingQueryProvider将是完美的选择。

The table I'm selecting from has a primary key that is a composite of three columns: INTEGER, VARCHAR, and VARCHAR. 我从中选择的表具有一个主键,该键由三列组成:INTEGER,VARCHAR和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. 因此,有效地好像主键列是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. 提供的itemIndex可能位于页面的中间,并且与页面大小一起将用于计算前一页的最后一个索引,以便能够检索此行的排序键。

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? 我假设这是从范式中进行的,即该表以数字ID排序? Or does this relate to the skip value in the LIMIT clause? 还是这与LIMIT子句中的skip值有关?

Any guidance you can offer is appreciated. 您可以提供的任何指导表示赞赏。

It's pretty clear from looking at the provided implementations. 从所提供的实现中可以很明显地看出来。 Here's PostgresPagingQueryProvider : 这是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. 没错,它与LIMIT子句中的skip值有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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