科帮网-Java论坛、Java社区、JavaWeb毕业设计

登录/注册
您现在的位置:论坛 资料库 开源社区 > solr实现MySQL数据全量索引和增量索引
总共48085条微博

动态微博

查看: 2399|回复: 1

solr实现MySQL数据全量索引和增量索引

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2016-03-15 13:11:48 |只看该作者 |倒序浏览
    solr.home  solr实例目录 D:solr\home

    solr.war      solr服务目录 D:solr\server\solr

    参考文档

    IBM developerWorks 文档库  Apache Solr 的新特性
    Solr中国 http://www.solr.cc/blog/
    官方文档 http://wiki.apache.org/solr/Data ... elta-import_command
    全量索引与增量索引

    索引导入分全量索引与增量索引。Solr支持多种创建索引的方式。这里通过solr官方提供的一个工具的—Data Import Handler,来做数据库建立索引,DIH支持增量索引。Solr/example中有example-DIH的项目,用了hsqldb作为数据库演示了DIH的使用,可以看下

    1、添加jar包  (mysql的jar包与dataimporthandler的jar包)

    将解压后的solr-4.10.0文件夹dist目录下的:

    solr-dataimporthandler-4.10.0.jar
    solr-dataimporthandler-extras-4.10.0.jar
    mysql-connector-java-5.1.13-bin.jar   (这个需根据使用的数据库不同添加不同的驱动包,所以dist目录下是没有的)
    拷贝到solr.war服务的lib目录下,对应本机D:\solr\server\solr\WEB-INF\lib

    2、配置一个 SolrRequestHandler将 DIH 和 Solr 关联起来

    在每个核心中的solrconfig.xml中配置dataimport,对应本机D:solr\home\core1\conf\solrconfig.xml
    1. <!-- 全量索引与增量索引 -->
    2. <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    3.         <lst name="defaults">
    4.                 <str name="config">data-config.xml</str>
    5.         </lst>
    6. </requestHandler>
    复制代码
    3、全量索引与增量索引配置文件,同目录下新增data-config.xml
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <dataConfig>
    3.     <dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
    4.         url="jdbc:mysql://ip:port/db_name?characterEncoding=UTF-8" user="root" password="root"/>
    5.     <document name="TestCore">
    6.         <entity name="test_table" pk="testId"
    7.                 query="select * from test_table"
    8.                 deltaImportQuery="select * from test_table where test_id='${dih.delta.testId}'"
    9.                 deltaQuery="select Test_Id as testId from test_table where create_time > '${dih.last_index_time}'"
    10.                 transformer="RegexTransformer">
    11.                 <field column="Test_Id" name="testId"/>
    12.                 <field column="Test_Name" name="testName"/>
    13.                 <field column="Create_Time" name="createTime"/>
    14.         </entity>
    15.     </document>
    16. </dataConfig>
    复制代码

    entity:需要从数据库中取出的数据,name为数据库表名,支持sql语句,支持多表查询。注意pk="id" 不能随便改 ,需要和schema.xml中的<uniqueKey>id</uniqueKey>匹配,否则会报错注[1] ,且<uniqueKey>id</uniqueKey>中的主键必须为String类型
    field:接受到的数据。列名column为数据库字段名(必须与数据库字段相同,区分大小写),name为索引库(必须和schema.xml)中field定义的name名字相同

    query属性为全量索引时查询用,其他的为增量索引用


    Note the variable ${dataimporter.last_index_time} The DataImportHandler exposes a variable called last_index_time which is a timestamp value denoting the last time full-import 'or' delta-import was run。

    内置变量${dataimporter.last_index_time}用来记录最后一次索引的时间(包括全量与增量),并保存在core~/conf/dataimport.properties文件中,该文件为自动创建(如果solr服务启动后请求索引后,未在相应核下找到该文件,说明DIH配置有误,可以通过solr后台logging查看异常日志)。这里dataimport.properties内容如下:
    1. #Mon Mar 15 16:10:05 CST 2015
    2. test_table.last_index_time=2015-03-15 16\:10\:04
    3. last_index_time=2015-03-15 16\:10\:04
    复制代码

    内置变量 “${dataimporter.request.length}”和 “${dataimporter.request.offset}”用来设置一个批次索引的数据表记录数
    至此,DIH配置完成,在浏览器中输入请求:


    全量索引
    1. http://localhost:8080/solr/core1/dataimport?command=data-import&clean=false&commit=true
    复制代码
    增量索引
    1. http://localhost:8080/solr/core1/dataimport?command=delta-import&clean=false&commit=true
    复制代码
    查看导入状态
    1. http://localhost:8080/solr/core1/dataimport?command=status
    复制代码

    clean:选择是否要在索引开始构建之前删除之前的索引,默认为true
    commit:选择是否在索引完成之后提交。默认为true
    offset=0&length=100000  0到100000的数据创建索引,全量索引分批次索引用于数据量较大时,如果数据量较小,可以直接全部索引(未调通)
    当然也可以通过 http://localhost:8080/solr

    进入solr管理页面中相应索引库(如core1)中找到dataimport 点击执行,这样好处是还可以通过日志logging查看异常
    定时重做索引

    定时自动重建索引可以通过自己写程序实现,也可以通过solr-dataimportscheduler-1.0.jar包完成此功能,具体如下:

    1、将dataimporter.properties(网上找的)放到solr.home/conf目录下,对应本机D:\solr\home\conf(conf文件夹是没有的,需要新建)注[3]


    dataimporter.properties (重要)(不同于core/conf下的dataimport.properties,那个dataimport.properties会自动生成,用于记录最近一次更新的时间)
    1. #################################################
    2. #                                               #
    3. #       dataimport scheduler properties         #
    4. #                                               #
    5. #################################################

    6. #  to sync or not to sync
    7. #  1 - active; anything else - inactive
    8. syncEnabled=1

    9. #  which cores to schedule
    10. #  in a multi-core environment you can decide which cores you want syncronized
    11. #  leave empty or comment it out if using single-core deployment
    12. syncCores=core1,core2

    13. #  solr server name or IP address
    14. #  [defaults to localhost if empty]
    15. server=localhost

    16. #  solr server port
    17. #  [defaults to 80 if empty]
    18. port=8080

    19. #  application name/context
    20. #  [defaults to current ServletContextListener's context (app) name]
    21. webapp=solr

    22. #  增量索引的参数
    23. #  URL params [mandatory]
    24. #  remainder of URL
    25. params=/dataimport?command=delta-import&clean=false&commit=true

    26. #  重做增量索引的时间间隔
    27. #  schedule interval
    28. #  number of minutes between two runs
    29. #  [defaults to 30 if empty]
    30. interval=1

    31. #  重做全量索引的时间间隔,单位分钟,默认7200,即5天;
    32. #  为空,为0,或者注释掉:表示永不重做索引
    33. #reBuildIndexInterval=7200

    34. #  重做索引的参数
    35. reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true

    36. #  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
    37. #  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期
    38. reBuildIndexBeginTime=03:10:00
    复制代码

    2、修改solr.war中WEB-INF/web.xml, 对应本机D:\solr\server\solr\WEB-INF\web.xml,在servlet节点前增加:


    这个有点坑爹!这个类并不存在solr几个包中,需要额外导入solr-dataimportscheduler-1.1.jar(官方还不支持定期重建索引,所以只能通过修改过的第三方jar,参考这里)
    1. <listener>
    2.         <listener-class>
    3.                 org.apache.solr.handler.dataimport.scheduler.ApplicationListener
    4.         </listener-class>
    5. </listener>
    复制代码
    看别人的资料说上面的dataimporter.properties位置可以随意放,只需要在solr.war中WEB-INF/web.xml中指定位置(还未测试,不清楚说的对不对)
    1. <context-param>
    2.     <param-name>autoDeltaImportConfPath</param-name>
    3.     <param-value>/yourconfpath</param-value>
    4. </context-param>
    复制代码

    常见异常

    org.apache.solr.common.SolrException: Document is missing mandatory uniqueKey field: testId
    参见注[1]
    java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Could not load driver: com.mysql.jdbc.Driver Processing Document # 1
    请加入数据库驱动jar包
    org.apache.solr.common.SolrException: [doc=454a2d2f-c199-483a-a8d2-7c49c1adbfe1] missing required field: xxxx
    xxxx这个字段对应的column的字段名与数据库中的字段不一致(包括大小写)
    Exception while processing: test_table document : SolrInputDocument(fields: [])rg.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from test_table limit  offset  Processing Document # 1
            at org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow(DataImportHandlerException.java:71)
            at org.apache.solr.handler.dataimport.JdbcDataSource$ResultSetIterator.<init>(JdbcDataSource.java:283)
    全量索引异常:data-config.xml中配置了批次索引,但创建索引的时候参数offset&length出现问题,我也未调通

           
    严重: Exception sending context initialized event to listener instance of class org.apache.solr.handler.dataimport.scheduler.ApplicationListener
    java.lang.NullPointerException
            at org.apache.solr.handler.dataimport.scheduler.ApplicationListener.contextInitialized(ApplicationListener.java:93)
            at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
            at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)

    出现这种错误是因为dataimporter.properties配置文件的存放位置不对


    科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、科帮网-Java论坛、Java社区、JavaWeb毕业设计管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    4

    主题

    0

    听众

    302

    金钱

    四袋长老

    该用户从未签到

    沙发
    发表于 2016-09-27 14:12:20 |只看该作者
    涨姿势了                                             
    回复

    使用道具 举报

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

       

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表