solr4 importdata

solr4 mysql導入數據創建索引

本文章主要描述solr4.10.3版本的創建索引的功能,solr前期配置參見上一篇文章

1.前期準備

1.按照上篇章博客步驟,將solr4.10.3版本環境配置好,solr配置地址

2.創建3張表作爲數據源,體驗一下多表查詢及多值等能力
比如我在創建一個用戶信息表,一個書本信息表,一個用戶關聯書本的中間表。中間表包含用戶的id,書本的id字段

2.配置步驟。

1.我們需要準備好以下幾個包,或者確認在tomcat-webapps-solr下面的lib裏面有以下幾個包
solr-dataimporthandler-4.10.3.jar
solr-dataimporthandler-extras-4.10.3.jar
以上兩個包之前配置solr就已經放入。還需要放入以下兩個jar包
mysql-connector-java-5.1.36.jar
solr-dataimportscheduler-1.1.jar

2.容器中的WEB-INF下solr的web.xml中添加監聽

<listener>
      <listener-class>
            org.apache.solr.handler.dataimport.scheduler.ApplicationListener
      </listener-class>
</listener>

3.在core的同級目錄下創建conf文件夾。裏面存放dataimport.properties文件。**注意**conf在core 的同級目錄,該目錄下還有solr.xml配置文件。千萬不要放錯。本人環境存放地址E:\solr410\solrtomcat\solr\conf\dataimport.properties.文件內容如下所示

syncEnabled=1
syncCores=xmcore
server=127.0.0.1 (solr啓動容器IP)
port=8180(solr啓動容器端口號)
webapp=solr
params=/dataimport?command=delta-import&clean=false&commit=true
interval=1 (增量創建索引間隔)
reBuildIndexInterval=7200(全量創建索引間隔)
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
reBuildIndexBeginTime=18:25:00

4.最爲重要和容易出錯的一步
在solr core的conf下的solrconf.xml文件中
在大概820行左右存在name=”/dataimport” 請求配置。修改爲:

 <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">xmcore-data-config.xml</str>
    </lst>
 </requestHandler>

xmcore-data-config.xml 文件在solrconf.xml同級目錄下創建該文件。示例內容爲:

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

<dataConfig>
<dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf8"
    user="dev" password="dev123"/>
<document>

    <entity pk="ID"  dataSource="mydb" name="user" query="select  *  from user_info WHERE status=0 and isdelete=0 "

        deltaQuery="select ID  from user_info where last_modify > '${dataimporter.last_index_time}'"

        deletedPkQuery="select ID from user_info where isdelete=1"

        deltaImportQuery="select * from user_info where ID='${dataimporter.delta.ID}'"  >

        <field column="ID" name="id" />
        <field column="name" name="name" />
        <field column="age" name="age" />
        <field column="phone" name="phone" />
        <field column="address" name="address" />
        <field column="status" name="status" />
        <field column="last_modify" name="last_modify" />
        <field column="isdelete" name="isdelete" />

        <entity pk="ID"  dataSource="mydb" name="user_book" query="select t1.bookid, t2.name from user_book t1 ,book t2 where  t1.bookid =   t2.id   and   t1.USERID =  '${user.id}' " >

        <field column="bookid" name="book_id" />
        <field column="name" name="book_name" />

        </entity>

    </entity>           
    </document>
</dataConfig>

注意該文件的節點開始及結束格式。不能錯誤,不然就會讀取該文件失敗。該文件演示了帶有中間表的多表關聯查詢及多值效果

**5.**schame.xml配置文件列

 <!--下面屬性是用戶表中心字段信息-->
 <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 

 <field name="name" type="text_zh" indexed="true" stored="true"/>
 <field name="age" type="int" indexed="true" stored="true"/> 
 <field name="phone" type="text_general" indexed="true" stored="true"/> 
 <field name="address" type="text_zh" indexed="true" stored="true"/> 
 <field name="status" type="int" indexed="true" stored="true"/> 
 <field name="last_modify" type="date" indexed="true" stored="true"/> 
 <field name="isdelete" type="int" indexed="true" stored="true"/> 
 <!--下面兩個屬性是中間表與書本表中信息-->
 <field name="book_id" type="int" indexed="true" stored="true" multiValued="true" /> 
 <field name="book_name" type="text_zh" indexed="true" stored="true" multiValued="true" /> 

6.驗證全量與增量創建索引
http://localhost:8180/solr/#/xmcore/dataimport//dataimport下面存在entity。下拉列表是否存在user的選項,如果存在表明上面步驟的配置文件沒有問題,否則則配置格式存在問題。
選擇user。可以進行全量與增量導入的測試。看看是否成功創建索引。
注意 mysql設計的表中必須存在last_modify列,改列用來記錄最後修改或者新增時間,用於增量創建索引。列名隨意,作爲第四部文件中deltaQuery中的條件。

7.可以修改數據庫數據與last_modify。修改第三步dataimport.properties文件的增量創建索引時間。查看能夠增量創建索引.

3.需要的包材料及全部配置好的solr環境,solrtomcat只需要解壓,修改一下web.xml的solrhome地址即可以啓動,並且具備同義詞,分詞,擴展詞。可以定時增量與全量創建索引,只需要修改第3步的配置文件。地址:http://download.csdn.net/detail/duxiaomeng1986_2008/9147669 傳送

4.注意,上面下載的包啓動地址是http://localhost:8180/solr/ 注意端口號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章