分享

Solr学习(五)DIH增量、定时导入并检索数据

本帖最后由 xioaxu790 于 2014-12-7 21:01 编辑
问题导读
1、如何增量导入MYSQL数据库中的数据?
2、如何设置定时导入数据来做?
3、我们怎样测试增量导入?




本文接上一篇:Solr学习(四)DIH全量导入并索引数据
(一)引言:
前面我的文章 DIH全量导入 中已经学会了如何全量导入Oralce和MySQL的数据,大家都知道全量导入在数据量大的时候代价非常大,一般来说都会适用增量的方式来导入数据,下面介绍如何增量导入MYSQL数据库中的数据,以及如何设置 定时来做。
下面介绍的所有操作都是基于前面已经完成的全量导入的基础上来做的。

(一)DIH增量从MYSQL数据库导入数据:
1、数据库表的更改:
前面已经创建好了一个UserInfo的表,这里为了能够进行增量导入,需要新增一个字段,类型为TIMESTAMP,默认值为CURRENT_TIMESTAMP。
1.png

有了这样一个字段,Solr才能判断增量导入的时候,哪些数据是新的。
因为Solr本身有一个默认值last_index_time,记录最后一次做full import或者是delta import(增量导入)的时间,这个值存储在文件conf目录的dataimport.properties文件中。

2、data-config.xml中必要属性的设置:       
<!--  transformer 格式转化:HTMLStripTransformer 索引中忽略HTML标签   --->   
<!--  query:查询数据库表符合记录数据   --->   
<!--  deltaQuery:增量索引查询主键ID    --->    注意这个只能返回ID字段   
<!--  deltaImportQuery:增量索引查询导入的数据  --->   
<!--  deletedPkQuery:增量索引删除主键ID查询  ---> 注意这个只能返回ID字段   
有关“query”,“deltaImportQuery”, “deltaQuery”的解释,引用官网说明,如下所示:

The query gives the data needed to populate fields of the Solr document in full-import
The deltaImportQuery gives the data needed to populate fields when running a delta-import
The deltaQuery gives the primary keys of the current entity which have changes since the last index time

最终针对步骤一中创建的UserInfo表,我们的data-config.xml文件的配置内容如下:
  1. <dataConfig>  
  2.     <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password="passok" />   
  3.     <document>  
  4.         <entity name="userInfo" pk="UserID"   
  5. query="SELECT * FROM userinfo"   
  6. deltaImportQuery="SELECT * FROM userinfo where UserID='${dih.delta.UserID}'"   
  7. deltaQuery="SELECT UserID FROM userinfo where UpdateTime > '${dataimporter.last_index_time}'">  
  8.             <field column="UserID" name="id"/>   
  9.             <field column="UserName" name="userName"/>   
  10.             <field column="UserAge" name="userAge"/>  
  11.             <field column="UpdateTime" name="updateTime"/>  
  12.         </entity>  
  13.     </document>  
  14. </dataConfig>  
复制代码


意思是首先按照query指定的SQL语句查询出符合条件的记录。
然后从这些数据中根据deltaQuery指定的SQL语句查询出所有需要增量导入的数据的ID号。
最后根据deltaImportQuery指定的SQL语句返回所有这些ID的数据,即为这次增量导入所要处理的数据。
核心思想是:通过内置变量“${dih.delta.id}”和 “${dataimporter.last_index_time}”来记录本次要索引的id和最近一次索引的时间。
注意:刚新加上的UpdateTime字段也要在field属性中配置,同时也要在schema.xml文件中配置:<field name="updateTime" type="date" indexed="true" stored="true" />

3、测试增量导入:
在浏览器中输入:http://localhost:8087/solr/dataimport?command=delta-import  然后到http://localhost:8087/solr/#/collection1/query检索一条不存在的数据,然后利用SQL语句插入一条数据:
  1. INSERT INTO `test`.`userinfo`  
  2. (`UserID`,  
  3. `UserName`,  
  4. `UserAge`)  
  5. VALUES  
  6. (6,  
  7. '季义钦增量数据测试',  
  8. 25);  
复制代码


再次在浏览器中数据刚才的连接,再次检索。

(二)设置增量导入为定时执行的任务:
很多人利用Windows计划任务,或者Linux的Cron来定期访问增量导入的连接来完成定时增量导入的功能,这其实也是可以的,而且应该没什么问题。
但是更方便,更加与Solr本身集成度高的是利用其自身的定时增量导入功能。
1、下载apache-solr-dataimportscheduler-1.0.jar放到Tomcat的webapps的solr目录的WEB-INF的lib目录下:
下载地址:http://code.google.com/p/solr-dataimport-scheduler/downloads/list
也可以到我的云盘下载:http://pan.baidu.com/s/1dDw0MRn

2、修改solr的WEB-INF目录下面的web.xml文件:
为<web-app>元素添加一个子元素
  1. <listener>   
  2.     <listener-class>   
  3.             org.apache.solr.handler.dataimport.scheduler.ApplicationListener   
  4.     </listener-class>   
  5.   </listener>   
复制代码

3、新建配置文件dataimport.properties:
在SOLR_HOME\solr目录下面新建一个目录conf(注意不是SOLR_HOME\solr\collection1下面的conf),然后用解压文件打开apache-solr-dataimportscheduler-1.0.jar文件,将里面的dataimport.properties文件拷贝过来,进行修改,下面是最终我的自动定时更新配置文件内容:
  1. #################################################  
  2. #                                               #  
  3. #       dataimport scheduler properties         #  
  4. #                                               #  
  5. #################################################  
  6.   
  7. #  to sync or not to sync  
  8. #  1 - active; anything else - inactive  
  9. syncEnabled=1  
  10.   
  11. #  which cores to schedule  
  12. #  in a multi-core environment you can decide which cores you want syncronized  
  13. #  leave empty or comment it out if using single-core deployment  
  14. # syncCores=game,resource #因为我的是single-core,所以注释掉了,默认就是collection1  
  15.   
  16. #  solr server name or IP address  
  17. #  [defaults to localhost if empty]  
  18. server=localhost  
  19.   
  20. #  solr server port  
  21. #  [defaults to 80 if empty]  
  22. port=8087  
  23.   
  24. #  application name/context  
  25. #  [defaults to current ServletContextListener's context (app) name]  
  26. webapp=solr  
  27.   
  28. #  URL params [mandatory]  
  29. #  remainder of URL  
  30. #  增量更新的请求参数  
  31. params=/dataimport?command=delta-import&clean=true&commit=true  
  32.   
  33. #  schedule interval  
  34. #  number of minutes between two runs  
  35. #  [defaults to 30 if empty]  
  36. #  这里配置的是2min一次  
  37. interval=2  
  38.   
  39. #  重做索引的时间间隔,单位分钟,默认7200,即5天;   
  40. #  为空,为0,或者注释掉:表示永不重做索引  
  41. reBuildIndexInterval=7200  
  42.   
  43. #  重做索引的参数  
  44. reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true  
  45.   
  46. #  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;  
  47. #  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期  
  48. reBuildIndexBeginTime=03:10:00
复制代码


至此就完成了定时增量更新的配置,启动tomcat服务器,不需要再浏览器请求增量导入了,可以看到已经开始定期增量更新了。
2.png
一般来说要在你的项目中引入Solr需要考虑以下几点:
1、数据更新频率:每天数据增量有多大,随时更新还是定时更新
2、数据总量:数据要保存多长时间
3、一致性要求:期望多长时间内看到更新的数据,最长允许多长时间延迟
4、数据特点:数据源包括哪些,平均单条记录大小
5、业务特点:有哪些排序要求,检索条件
6、资源复用:已有的硬件配置是怎样的,是否有升级计划


Solr学习多表导入
(一)导入两张表,不相关:
新建一张新表,同样要有UpdateTime字段:

在data-config.xml文件中增加这个表的entity配置:
  1. <dataConfig>  
  2.     <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password="passok" />   
  3.     <document>  
  4.         <entity name="userInfo" pk="UserID"   
  5. query="SELECT * FROM userinfo"   
  6. deltaImportQuery="SELECT * FROM userinfo where UserID='${dih.delta.UserID}'"   
  7. deltaQuery="SELECT UserID FROM userinfo where UpdateTime > '${dataimporter.last_index_time}'">  
  8.             <field column="UserID" name="id"/>   
  9.             <field column="UserName" name="userName"/>   
  10.             <field column="UserAge" name="userAge"/>  
  11.             <field column="UpdateTime" name="updateTime"/>  
  12.         </entity>  
  13.                 <!-- 新加的entity -->  
  14.         <entity name="myArticle" pk="AID"   
  15. query="SELECT * FROM article"   
  16. deltaImportQuery="SELECT * FROM article where AID='${dih.delta.AID}'"   
  17. deltaQuery="SELECT AID FROM article where UpdateTime > '${dataimporter.last_index_time}'">  
  18.             <field column="AID" name="id"/>   
  19.             <field column="ArTitle" name="arTitle"/>   
  20.             <field column="UpdateTime" name="updateTime"/>  
  21.         </entity>  
  22.     </document>  
  23. </dataConfig>  
复制代码

在schema,xml文件中增加想要索引的列的配置:
  1. <!-- ===========jiyiqin add====================== -->  
  2. <field name="userName" type="text_general" indexed="true" stored="true" />   
  3. <field name="userAge" type="int" indexed="true" stored="true" />  
  4. <field name="updateTime" type="date" indexed="true" stored="true" />  
  5. <field name="arTitle" type="text_general" indexed="true" stored="true" />
复制代码

就这样启动tomcat服务器,就会自动增量导入Article这个表的数据并为ArTitle这列建立索引了。



已有(3)人评论

跳转到指定楼层
lihy114 发表于 2015-10-10 11:41:08
楼主您好,想咨询一下您,我按照您说的这种方式设置了solr的增量数据同步,从命令行来看,是执行成功的,结果与您 上传的截图一样的;但是查询看并没有数据同步过去;将同步过程中产生的url连接拷贝到浏览器,就可以成功 执行了;
想问下您遇到过这种情况吗?是什么地方设置的有问题吗?
我用的jar包是solr-dataimportscheduler-1.1.jar   多谢
回复

使用道具 举报

魏航 发表于 2018-1-13 15:28:44
请问楼主,你做过关联表的增量更新吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条