SOLR研究第五弹:SOLR与数据库(ORACLE,MYSQL)关联做增量更新 
配置solr下的solrconfig.xml文件,加入:- <span style="background-color: white;"><font color="#000000"><!--数据源-->
 
 - <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
 
 - <lst name="defaults">
 
 - <str name="config">/opt/solr/core0/conf/data-config.xml</str>
 
 - </lst>
 
 - </requestHandler></font></span>
 
  复制代码 其中涉及到文件data-config.xmlmysql的配置如下: 
- <span style="background-color: white;"><font color="#000000"><dataConfig>
 
 - <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&char
 
 - acterEncoding=utf8" user="数据库帐号" password="数据库密码" />
 
 - <document name="documents">
 
 - <entity name="doc" pk="id"
 
 - query="select * from 表格"
 
 - deltaImportQuery="select * from 表格 where id='${dataimporter.delta.id}'"
 
 - deltaQuery="select id from 表格 where 最后更新字段如last_index_time > '${dataimporter.last_index_time}'"
 
 - >
 
 - <field column="id" name="id" />
 
 - <field column="name" name="name" />
 
 - </entity>
 
 - </document>
 
 - </dataConfig></font></span>
 
  复制代码 oracle的配置如下:- <span style="background-color: white;"><font color="#000000"><dataConfig>
 
 - <dataSource type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:端口号:库名" u
 
 - ser="数据库帐号" password="数据库密码"/>
 
 - <document name="messages">
 
 - <entity name="message" pk="ID" transformer="ClobTransformer"
 
 - query="select * from 表格 WHERE ISDELETED=0"
 
 - deletedPkQuery="select * from 表格 where ISDELETED=1"
 
 - deltaQuery="select id from 表格 where 数据更新字段如last_index_time > '${dataimporter.last_index_time}'"
 
 - >
 
 - <field column="ID" name="ID"/>
 
 - <field column="NAME" name="NAME"/>
 
 - </entity>
 
 - </document>
 
 - </dataConfig></font></span>
 
  复制代码 增量部分为其中的: 
- <span style="background-color: white;"><font color="#000000"><entity name="message" pk="ID" transformer="ClobTransformer"
 
 - query="select * from 表格 WHERE ISDELETED=0"
 
 - deletedPkQuery="select * from 表格 where ISDELETED=1"
 
 - deltaQuery="select id from 表格 where 数据更新字段如last_index_time > '${dataimporter.last_index_time}'"
 
 - ></font></span>
 
  复制代码query:获取数据元,读取正常状态(isdeleted=0)的数据 deletedpkquery:增量删除,数据库标记isdeleted=1后,更新solr删除文档 deltaQuery语句是用于增量导入(Delta Import)中获取符合增量导入标准的数据的主键的SQL,供deltaImportQuery查询使用。它将数据表中每一笔记录执行写操作的时候更新字段last_index_time为当前系统时间,这句sql是从数据表中取出更新字段比solr上次更新时间晚的记录,加入或更新到solr索引。 deltaImportQuery语句增量导入(Delta Import)中获取需要增量索引数据(document)的字段(field) 其中执行完成后会在本地conf目录下生成文件:dataimport.properties,授予该文件写权限,每次solr执行索引后会更新记录,如内容: - <span style="background-color: white;"><font color="#000000">#Tue Jul 17 12:36:41 CST 2012
 
 - last_index_time=2012-07-17 12\:17\:27
 
 - message.last_index_time=2012-07-17 12\:17\:27</font></span>
 
  复制代码其中last_index_time为上次索引时间。 关于执行地址DataImportHandler:批量导入(full-import): http://localhost:8080/solr/dataimport?command=full-import&commit=true  增量导入(delta-import): 
http://localhost:8080/solr/dataimport?command=delta-import&commit=true 导入状态查询(status): 
http://localhost:8080/solr/dataimport  重新装载配置文件(reload-config): 
http://localhost:8080/solr/dataimport?command=reload-config  终止导入(abort): 
http://localhost:8080/solr/dataimport?command=abort  
出处:http://blog.duteba.com/technology/article/70.htm 
SOLR研究第五弹:SOLR与数据库(ORACLE,MYSQL)关联做增量更新 
 |