Solr学习笔记之3、Solr导入SQLServer数据建立索引
一、下载MSSQLServer的JDBC驱动
下载:Microsoft JDBC Driver 4.0 for SQL Server
地址:http://www.microsoft.com/zh-CN/download/details.aspx?displaylang=en&id=11774
二、配置Solr dataimport for SQLServer
1、依赖jar包配置
将MSSQLServer的JDBC驱动中的sqljdbc4.jar复制到\tomcat\webapps\solr\WEB-INF\lib文件夹中。
将solr-dataimporthandler-4.7.0.jar和solr-dataimporthandler-extras-4.7.0.jar复制到\tomcat\webapps\solr\WEB-INF\lib文件夹中。
2、solrconfig.xml配置
在Solr对应core的配置文件中(如:SolrSingle\collection1\conf),打开solrconfig.xml文件,新增如下节点:
<!--导入--> <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
然后在同一目录下新增data-config.xml文件。
3、data-config.xml文件配置如下:
一对一模式:

<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=SolrDemoDB;username=sa;password=123456" user="sa" password="123456" batchSize="100"/> <document> <entity name="Article" pk="ArticleId" query="select * from Article where IsDelete=0" deltaQuery="select ArticleId from Article where CreateDate > '${dataimporter.last_index_time}'" deletedPkQuery="select ArticleId from Article where IsDelete=1" deltaImportQuery="select * from Article where ArticleId='${dataimporter.delta.ArticleId}'" transformer="ClobTransformer,HTMLStripTransformer,DateFormatTransformer" > <field column="ArticleId" name="ArticleId" /> <field column="Title" name="Title" /> <field column="CreateDate" name="CreateDate" dateTimeFormat="yyyy-MM-dd" /> <field column="IsDelete" name="IsDelete" /> <field column="Content" name="Content" /> <field column="TypeId" name="TypeId" /> <entity name="ArticleType" query="select * from ArticleType where TypeId=${Article.TypeId}"> <field column="ArticleTypeName" name="ArticleTypeName" /> </entity> </entity> </document> </dataConfig>
<!--query:查询数据库表符合记录数据-->
<!--deltaQuery:查询出需要增量索引的数据,所有经过修改的记录的Id,可能是修改操作、添加操作、删除操作产生的(此查询只对增量导入起作用,而且只能返回Id值) -->
<!--deletedPkQuery:查询出需要删除的数据记录主键Id,solr通过它来删除索引里面对应的数据(此查询只对增量导入起作用,而且只能返回ID值)-->
<!--deltaImportQuery:次查询是获取以上两步的Id,然后把其全部数据获取,根据获取的数据,对索引库进行更新操作,可能是删除,添加,修改(此查询只对增量导入起作用,可以返回多个字段的值,一般情况下,都是返回所有字段的列)-->
<!--transformer:格式转化-->
对应在schema.xml文件中的field设置为:
<!--Article Begin--> <field name="ArticleId" type="string" indexed="true" stored="true" required="true" multiValued="false"/> <field name="Title" type="text_general" indexed="true" stored="true"/> <field name="CreateDate" type="date" indexed="true" stored="true"/> <field name="IsDelete" type="boolean" indexed="true" stored="true"/> <field name="Content" type="text_general" indexed="true" stored="true"/> <field name="TypeId" type="int" indexed="true" stored="true"/> <field name="ArticleTypeName" type="string" indexed="true" stored="true" /> <!--Article End--> <uniqueKey>ArticleId</uniqueKey>
一对多模式:

<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=SolrDemoDB;username=sa;password=123456" user="sa" password="123456" batchSize="100"/> <document> <entity name="ArticleType" pk="TypeId" query="select * from ArticleType"> <field column="ArticleTypeName" name="ArticleTypeName" /> <field column="TypeId" name="TypeId" /> <entity name="Article" pk="ArticleId" query="select * from Article where IsDelete=0 and TypeId=${ArticleType.TypeId}"> <field column="ArticleId" name="ArticleId" /> <field column="Title" name="Title" /> <field column="CreateDate" name="CreateDate" dateTimeFormat="yyyy-MM-dd" /> <field column="IsDelete" name="IsDelete" /> <field column="Content" name="Content" /> <field column="TypeId" name="TypeId" /> </entity> </entity> </document> </dataConfig>
对应在schema.xml文件中的field设置为:
<!--Article Begin--> <field name="ArticleId" type="string" indexed="true" stored="true" required="true" multiValued="true"/> <field name="Title" type="text_general" indexed="true" stored="true" multiValued="true"/> <field name="CreateDate" type="date" indexed="true" stored="true" multiValued="true"/> <field name="IsDelete" type="boolean" indexed="true" stored="true" multiValued="true"/> <field name="Content" type="text_general" indexed="true" stored="true" multiValued="true"/> <field name="TypeId" type="string" indexed="true" stored="true" required="true" multiValued="false"/> <field name="ArticleTypeName" type="string" indexed="true" stored="true" /> <!--Article End--> <uniqueKey>TypeId</uniqueKey>
4、在数据库中添加相应的数据库及数据表
此文示例数据库为SolrDemoDB,示例数据表为:Article。
5、重启tomcat,访问http://localhost:8080/solr/。看到如下页面,则说明成功。
6、solr dataimport commond
全量索引:http://ip:port/webapp_name/core_name/dataimport?command=full-import&clean=false&commit=true
增量索引:http://ip:port/webapp_name/core_name/dataimport?command=delta-import&clean=false&commit=true
备注:可在Client端通过http请求,来发送命令。
参数说明:
-
full-import : "全量导入"这个操作可以通过访问URL http://:/solr/dataimport?command=full-import 完成。
-
这个操作,将会新起一个线程。response中的attribute属性将会显示busy。
-
这个操作执行的时间取决于数据集的大小。
-
当这个操作运行完了以后,它将在conf/dataimport.properties这个文件中记录下这个操作的开始时间
-
当“增量导入”被执行时,stored timestamp这个时间戳将会被用到
-
solr的查询在“全量导入”时,不是阻塞的
-
它还有下面一些参数:
-
clean : (default 'true'). 决定在建立索引之前,删除以前的索引。
-
commit: (default 'true'). 决定这个操作之后是否要commit
-
optimize: (default 'true'). 决定这个操作之后是否要优化。
-
debug : (default false). 工作在debug模式下。详情请看 the interactive development mode (see here)
-
-
-
delta-import : 当遇到一些增量的输入,或者发生一些变化时使用http://:/solr/dataimport?command=delta-import . 它同样支持 clean, commit, optimize and debug 这几个参数.
-
status : 想要知道命令执行的状态 , 访问 URL http://:/solr/dataimport?command=status.它给出了关于文档创建、删除,查询、结果获取等等的详细状况。
-
reload-config : 如果data-config.xml已经改变,你不希望重启solr,而要重新加载配置时,运行一下的命令http://:/solr/dataimport?command=reload-config
-
abort : 你可以通过访问 url http://:/solr/dataimport?command=abort 来终止一个在运行的操作