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 來終止一個在運行的操作
轉載請保留本文地址:http://www.cnblogs.com/wangwangfei/p/3598462.html