是否可以将Apache Solr与Spring Batch集成?

时间:2015-10-04 09:05:50

标签: spring solr spring-batch

我读到了Apache Solr和Spring Batch。 Apache Solr是强大的搜索技术。现在,我们想要从Apache Solr读取数据,然后Spring Batch将处理该数据并将写入数据库。

我搜索了很多,但我无法获得关于这种整合的演示。 是否可以将Apache Solr与Spring Batch集成?

2 个答案:

答案 0 :(得分:1)

我们已经完成了一个基于Spring批处理的应用程序,它在solr cloud上进行索引,相当于Solr“数据导入请求处理程序”。

第1步:从数据库中读取

 <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>

第2步:处理读取数据

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

第3步:将数据写入/发布到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();

        }
    }

我不确定我的回答会对您有所帮助,因为您的问题不是很清楚。为什么要从apache solr读取数据并将其写入mysql数据库

答案 1 :(得分:0)

我真的不了解Apache Solr,但是从其网站描述:

  

Solr是一个独立的企业搜索服务器,具有类似REST的API。   您通过JSON,XML,CSV或者将文档放入其中(称为“索引”)   通过HTTP二进制。您通过HTTP GET查询它并接收JSON,XML,CSV   或二进制结果。

因此,一个可能的解决方案是编写一个自定义ItemWriter,它通过HTTP GET查询您的Solr服务器并将结果作为POJO(或简单字符串)返回。然后,您可以使用经典的“块”与自定义阅读器,处理器(optionnal)和JBDC Writer。

相关问题