簡體   English   中英

SolrJ與Spring Batch的集成

[英]Integration of SolrJ with Spring Batch

我正在使用Spring Batch.Following是jobContext.xml文件, JdbcCursorItemReader正在從MySQL數據庫讀取數據。

<?xml version="1.0" encoding="UTF-8"?>
<beans>

    <import resource="infrastructureContext.xml"/>

    <batch:job id="readProcessWriteProducts">
        <batch:step id="readWriteProducts">
            <tasklet>
                <chunk reader="reader" processor="processer" writer="writer"  commit-interval="5"> </chunk>
            </tasklet>          
        </batch:step>
    </batch:job>  

    <bean id="reader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
        <property name="dataSource" ref="dataSource"></property>
        <property name="sql" value="SELECT id, name, description, price FROM product"></property>
        <property name="rowMapper" ref="productItemReader"></property>      
    </bean>

    <bean id="productItemReader" class="com.itl.readprocesswrite.reader.ProductItemReader"></bean>

    <bean id="processer" class="com.itl.readprocesswrite.processor.ProductItemProcessor">
        <constructor-arg ref="jdbcTemplate"></constructor-arg>
    </bean>

    <bean id="writer" class="com.itl.readprocesswrite.writer.ProductJdbcItemWriter">
        <constructor-arg ref="jdbcTemplate"></constructor-arg>
    </bean>
</beans>

現在,我想從Apache Solr中讀取數據。 我嘗試使用以下代碼從Apache Solr讀取數據。

public class SolrJDrive {
    public static void main(String[] args) throws MalformedURLException, SolrServerException {
         System.out.println("SolrJDrive::main");
         SolrServer solr = new CommonsHttpSolrServer("http://localhost:8983/solr");
         ModifiableSolrParams params = new ModifiableSolrParams();
         params.set("qt", "/select");
         params.set("q", "*:*");
         params.set("spellcheck", "on");
         params.set("spellcheck.build", "true");
         QueryResponse response = solr.query(params);
         SolrDocumentList results = response.getResults();
         for (int i = 0; i < results.size(); ++i) {
           System.out.println(results.get(i));
         }
    }//end of method main
}//end of class SolrJDrive

現在如何將其與Spring Batch集成在一起?

除了我在另一個問題( 是否可以將Apache Solr與Spring Batch集成? )中所說的以外,這是Solr自定義ItemReader的示例:

public class SolrReader implements ItemReader<SolrDocumentList> {

    @Override
    public SolrDocumentList read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {


        SolrServer solr = new CommonsHttpSolrServer("http://localhost:8983/solr");
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("qt", "/select");
        params.set("q", "*:*");
        params.set("spellcheck", "on");
        params.set("spellcheck.build", "true");
        QueryResponse response = solr.query(params);
        SolrDocumentList results = response.getResults();

        return results;
    }
}

然后,您將需要一個ItemProcessor來將您的SolrDocumentList轉換為可以使用的東西(即POJO)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM