Solr 學習筆記(三) 資料匯入

來源:互聯網
上載者:User
Solr 學習筆記(三) 資料匯入

大多數的應用程式將資料存放區在關聯式資料庫、xml檔案中。對這樣的資料進行搜尋是很常見的應用。所謂的DataImportHandler提供一種可配置的方式向solr匯入資料,可以一次全部匯入,也可以增量匯入。

他可以實現

能夠讀取關聯式資料庫中的資料。  
通過可配置的方式,能夠將資料庫中多列、多表的資料產生solr文檔    
能夠通過solr文檔更新solr  
提供通過設定檔就能夠匯入所有資料的能力  
能夠發現並處理 由insert、update帶來的變化(我們假定在表中有一個叫做“last-modified的列”)  
能夠配置"完全匯入"和"增量匯入"的時間  
讓讀取xml檔案,並建立索引成為可配置。  
能夠將 其他的資料來源(例如:ftp,scp,etc)或者其他格式的文檔(Json,csv)以外掛程式的形式整合到項目中。 

這個 Handler首先要在solrconfig.xml檔案中配置下,如下所示。

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

從它的名字上,我們或許也可以猜到,  
DataImportHandler正是requestHandler的實現。我們一共需要在兩個地方設定檔中進行一些配置。  
solrconfig.xml 。 data-config.xml必須在這個檔案中配置,datasource也可以。不過,一般將datasource放在data- config.xml檔案中。 

下面幾個步驟是必要的.  
1.定義一個data-config.xml 檔案,並這個它的路徑配置到solrconfig.xml中關於DataImportHandler的配置中。給出Connection的資訊(假設你選擇在solrconfig中配置datasource)  

<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="db_username" password="db_password"/>  

屬性type 指定了實現的類型。它是可選的。預設的實現是JdbcDataSource。 
一個設定檔可以配置多個資料來源。增加一個dataSource元素就可以增加一個資料來源了。name屬性可以區分不同的資料來源。如果配 置了多於一個的資料來源,那麼要注意將name配置成唯一的。 

例如:

<dataSource type="JdbcDataSource" name="ds-1" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://db1-host/dbname" user="db_username" password="db_password"/>  <dataSource type="JdbcDataSource" name="ds-2" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://db2-host/dbname" user="db_username" password="db_password"/>  

然後這樣使用 :

<entity name="one" dataSource="ds-1" ...>  ..</entity>  

2.開啟DataImportHandler頁面去驗證,是否該配置的都配置好了。http://localhost:8983/solr/dataimport 

2.1.使用“完全匯入”命令將資料從資料庫中匯出,並提交給solr建立索引  

http://localhost:8983/solr/dataimport?command=full-import
2.2.使用“增量匯入”命令對資料庫發生的變化的資料匯出,並提交給solr建立索引。

http://localhost:8983/solr/dataimport?command=delta-import

參考:

http://cpf1985.iteye.com/blog/1074425

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.