您的位置:首页 > 数据库

Solr学习笔记之3、Solr dataimport - 从SQLServer导入数据建立索引

2014-03-13 13:58 525 查看
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="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>


View Code
对应在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 来终止一个在运行的操作

转载请保留本文地址:/article/6091268.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: