问题
I read about Apache Solr and Spring Batch. Apache Solr is powerful search technology. Now, we want to read data from Apache Solr and then Spring Batch will process that data and will write to database.
I searched a lot, but I could not get demo about this integrartion. Is it possible to integrate Apache Solr with Spring Batch?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/32931814/is-it-possible-to-integrate-apache-solr-with-spring-batch