您的位置:首页 > 数据库 > MySQL

Solr之搭建Solr6.0服务并从Mysql上导入数据

2016-11-17 20:09 603 查看
目前百度上面关于solr对MYSQL的集成一般都是4.0左右。但是前solr的版本已经到了6.0,很多配置都和一起不一样了。所以我今天就和大家聊聊solr6.0如何进行MYSQL数据库的配置。
步骤1:
  在webapps中solrhome下新建一个文件夹名字叫做mynode(名字不固定,可以随便取,但是这个名字在后面的配置中会有所关联。)然后在mynode文件下新建一个名字叫做conf (这个文件夹一定要建)的文件夹(这个文件名字最后不要改。)然后把官网下下来的solr项目中solr-6.0.0\server\solr\configsets\data_driven_schema_configs\conf下的所有东西复制到conf中去。(注意不要复制错!)最后把solr-6.0\solr-6.0.0\example\example-DIH\solr\db\conf下的admin-extra.html, admin-extra.menu-bottom.html
,admin-extra.menu-top.html三个文件也复制到conf中去。(这个三个文件可以不用copy)
我的做法是:
1.自己建的solrhome 下的 configsets 文件夹下 data_driven_schema_configs 拷贝出来 放在和和configsets同目录下改名字为mynode(名字自己定)。
2.把mynode下 conf 的东西 移动到 mynode 目录下 ,但是 conf 目录一定保留,否则会有
Properties is not writable. Delta imports are supported by data config but will not work.错误, 导致不能创建dataimport.properties文件

步骤2:
  把mysql所需的jar包和solr-6.0\solr-6.0.0\dist下的solr-dataimporthandler-6.0.0.jar和solr-dataimporthandler-extras-6.0.0.jar都复制到项目WEB-INF\lib下。然后在solrconfig.xml文件中加入<lib dir="D:/编程工具/tomcat/apache-tomcat-8.0.32-windows-x64/apache-tomcat-8.0.32/webapps/solr/WEB-INF/lib/"
regex=".*\.jar" />(就是把WEB-INF\lib里面的jar包配置到项目中,我这里用的是绝对地址。这段代码大约在solrconfig.xml的70多行处,前面有一堆类似的代码。)
我的做法是不建议用绝对路径
<lib dir="${solr.install.dir:../..}/WEB-INF/lib/" regex=".*\.jar"/>

假如我的tomacat 部署的绝对路径是/usr/local/apache-tomcat-8.5.6-solr/
我的solr 部署的 apache-tomcat-8.5.6-solr/webapps/下。
solr home配置的如下:

<env-entry>

       <env-entry-name>solr/home</env-entry-name>

       <env-entry-value>/usr/local/apache-tomcat-8.5.6-solr/webapps/solr/solrhome</env-entry-value>

       <env-entry-type>java.lang.String</env-entry-type>

    </env-entry>

   
则:
${solr.install.dir:}代表的路径是 usr/local/apache-tomcat-8.5.6-solr/webapps/solr/solrhome/mynode

同时注意 ${solr.install.dir:../..}//WEB-INF/lib 这种目录写法,确保能找到刚才的
那个三个jar.
步骤3:
  在MYSQL中新建一张表。我这里用的是Navicat管理工具进行创建的。这里的表名是goods。字段如下图所示:



步骤4:
  在solrconfig.xml的  <requestHandler name="/select" class="solr.SearchHandler">之上添加

[html] view
plain copy

 





<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  

      <lst name="defaults">  

         <str name="config">data-config.xml</str>  

      </lst>  

 </requestHandler>  

然后在conf下新建data-config.xml文件。里面内容如下:

[html] view
plain copy

 





<?xml version="1.0" encoding="UTF-8"?>  

<dataConfig>  

    <dataSource name="source1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solrdata" user="root" password="220316" batchSize="-1" />  

  <document>  

        <entity name="goods" pk="id"  dataSource="source1"   

                query="select * from  goods"  

                 deltaImportQuery="select * from goods where id='${dih.delta.id}'"  

                deltaQuery="select id from goods where updateTime> '${dataimporter.last_index_time}'">  

  

         <field column="id" name="id"/>  

        <field column="name" name="name"/>  

            <field column="number" name="number"/>  

            <field column="updateTime" name="updateTime"/>  

     </entity>  

  </document>  

</dataConfig>  

说明:

  dataSource是数据库数据源。Entity就是一张表对应的实体,pk是主键,query是查询语句。Field对应一个字段,column是数据库里的column名,后面的name属性对应着Solr的Filed的名字。其中solrdata是数据库名,goods是表名。

 
其中deltaQuery是增量索引,原理是从数据库中根据deltaQuery指定的SQL语句查询出所有需要增量导入的数据的ID号。然后根据deltaImportQuery指定的SQL语句返回所有这些ID的数据,即为这次增量导入所要处理的数据。核心思想是:通过内置变量“${dih.delta.id}”和
“${dataimporter.last_index_time}”来记录本次要索引的id和最近一次索引的时间。

  最后在conf文件下的managed-schema配置field信息:

[html] view
plain copy

 





<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />  

<field name="name" type="string" indexed="true" stored="false"/>  

<field name="number" type="int" indexed="true" stored="false"/>  

<field name="updateTime" type="date" indexed="true" stored="true" />  

   <field name="_version_" type="long" indexed="true" stored="false"/>  

   <field name="_root_" type="string" indexed="true" stored="false" docValues="false" />  

   <field name="_text_" type="text_general" indexed="true" stored="false" multiValued="true"/>  

其中id,name,number,updateTime是上面提到的数据库字段。下面三个field是系统本身的不能删除,否则会运行错误。
步骤5:
启动tomcat,并在URL中输入http://127.0.0.1:8080/solr/index.html路径。选择Core admin 输入如下设置:



设置好之后,点击Add Core按钮,进行设置,设置成功后,再core Selector选择刚刚添加的core。



  选择刚刚添加的goods实体进行索引操作:我们这儿可以选择full-import或者delta-import(增量索引),选择增量索引需要把clean的勾给去掉,不然会清除之前的,增量的索引的初衷是对新增或者修改的记录重新索引,会追加到原有的索引文件当中。当我们选择full-import的时候,最好就是把原有的索引文件给清空重新索引。



索引成功如下如所示:



使用query进行测试,输入sa查出了sa



数据库中的数据如下图所示:



这就成功的配置完了MYSQL数据库了,如果配置过程有啥问题可以参考我这个已经配置成功代码:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: