[jjzhu學java]之solr4.9同步mysql資料

來源:互聯網
上載者:User

標籤:server   hand   rom   sql   實現   phone   建立   查詢   conf   


Solr是一個高效能,採用Java5開發,基於Lucene的全文檢索搜尋server。同一時候對其進行了擴充,提供了比Lucene更為豐富的查詢語言,同一時候實現了可配置、可擴充並對查詢效能進行了最佳化,而且提供了一個完好的功能管理介面,是一款很優秀的全文檢索搜尋引擎。

       能夠將資料庫中的資料匯入到solr中,對於百萬層級的資料能夠高速響應查詢。

1、安裝jdk

JDK版本號碼須要1.7以上

2、安裝solr

下載solr壓縮包,解壓安裝包。進入到SOLR_HOME\example檔案夾下,執行命令java –jar start.jar啟動solr



Solr預設port為8983,訪問http://localhost:8983/solr,若有例如以下介面,則啟動成功


3、匯入並更新資料相關配置

       Solr提供了將資料庫資料匯入到solr索引庫的功能(dataimport)

3.1、下載資料庫驅動包

       下載mysql的驅動包。並放在{%SOLR_HOME%}contrib\dataimporthandler\lib檔案夾下


3.2、配置solrconfig.xml3.2.1、引用相關庫

       solrconfig.xml包括了用於配置自身行為的絕大部分參數,其作用範圍是當前core。位置在檔案檔案夾該檔案位於{%solr_home%} example\solr\collection1\conf下。

       資料匯入須要引用mysql的驅動包和DataImport所需的包。

       在檔案裡加入例如以下內容:

<libdir="../../../contrib/dataimporthandler/lib"regex=".*\.jar" />

       <lib dir="../../../dist/"regex="solr-dataimporthandler-\d.*\.jar" />

      

3.2.2、配置requestHandler

       在solrconfig.xml中增加例如以下配置:

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

       <lst name="defaults">

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

       </lst>

   </requestHandler>


3.3、配置schema.xml

              Schema.xml能夠靈活的配置不同類型的欄位,所在位置與solrconfig.xml同級。

              匯入資料須要配置與資料庫中欄位一致的類型,在設定檔裡


3.3、配置資料來源

       在與solrconfig.xml同級檔案夾下({%solr_home%}example\solr\collection1\con),建立與3.2.2中配置中同名的資料設定檔data-config.xml,


並加入例如以下內容

<dataConfig>

   <!-- driver: 資料庫驅動名

        url:資料庫路徑

        user:資料庫使用者

        password:資料庫密碼

        batchSize:防止在匯入資料過程中記憶體溢出

    -->

  <dataSource type="JdbcDataSource"

             driver="com.mysql.jdbc.Driver"

             url="jdbc:mysql://localhost/exercise"

              user="root"

              password=""

              batchSize="-1"/>

 

  <document>

   <!--要匯入的表,能夠配置多個

       並查詢出要建立索引的欄位

   -->

   <entity name="customerinfo" query="selectcustomerID,customerName,PID,telephone from customerinfo">

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

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

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

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

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

   </entity>

 </document>

</dataConfig>

4、匯入資料

       訪問solr介面



Command選擇full-import,Entity選擇要匯入的表,再點擊Execute便可匯入資料


完畢後會提示索引建立完畢。

然後在Query介面能夠輸入相關查詢條件進行查詢


[jjzhu學java]之solr4.9同步mysql資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.