Is it possible to integrate Apache Solr with Spring Batch?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 15:39:01

We have done a spring batch based application that do indexing on solr cloud which is equivalent to Solr "Data Import Request Handler".

Step 1: Read from database

 <bean id="itemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">
        <property name="dataSource" ref="mysqlDataSource"/>
        <property name="queryProvider">
            <bean class="org.springframework.batch.item.database.support.MySqlPagingQueryProvider">
            </bean>
        </property>
        <property name="rowMapper">
         <bean class="implement org.springframework.jdbc.core.RowMapper">
         <!-- public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        return SolrInputDocument }
        -->
            </bean>
        </property>
    </bean>

Step 2: process the read data

   <bean id="intermediateProcessor" class="our.own.intermediate.Processer" scope="step">
    </bean> 

Step 3: write/post data to apache solr

<bean id="solrItemWriter" class="our.own.writer.SolrItemWriter" scope="step">
    <property name="solrServer" ref="solrServer"/>
</bean> 

<bean id="solrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer" destroy-method="shutdown" >
    <constructor-arg index="0"><value>solr cloud zookeeper host</value></constructor-arg>
    <constructor-arg index="1"><value>false</value></constructor-arg>
</bean> 

    import org.apache.solr.client.solrj.SolrServer;
    import org.apache.solr.client.solrj.response.UpdateResponse;
    import org.apache.solr.common.SolrInputDocument;    
    public class SolrItemWriter implements ItemWriter {
        @Override
        public void write(List items) throws Exception {
            for (Object object : items) {
                SolrInputDocument solrInputDocument = (SolrInputDocument) object;
                UpdateResponse response = solrServer.add(solrInputDocument);
            }
            UpdateResponse commitResponse = solrServer.commit();

        }
    }

I am not sure my answer will help you as your question is not very clear about the requirements. why you want read data from apache solr and write it to mysql database

I don't really know Apache Solr, but from its website description :

Solr is a standalone enterprise search server with a REST-like API. You put documents in it (called "indexing") via JSON, XML, CSV or binary over HTTP. You query it via HTTP GET and receive JSON, XML, CSV or binary results.

So, a possible solution would be to write a custom ItemWriter which queries via HTTP GET your Solr server and returns the result as a POJO (or a simple String). You can then use a classic "chunk" with your custom reader, a processor (optionnal) and a JBDC Writer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!