溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

solr同步mysql的方法

發(fā)布時(shí)間:2021-07-05 17:08:14 來(lái)源:億速云 閱讀:348 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“solr同步mysql的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一、首先創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)和表

這里創(chuàng)建了一個(gè)表,加上了測(cè)試數(shù)據(jù),注意這里有一個(gè)字段來(lái)記錄更新時(shí)間  update_date

solr同步mysql的方法

二、修改配置文件

我們首先介紹全量同步,再介紹增量同步

solr同步mysql的方法

我的 solr 版本是 7.5 的,new_core是我創(chuàng)建的 core,打開(kāi) solrconfig.xml,增加如下配置

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

然后在當(dāng)前 conf 目錄下創(chuàng)建 data-config.xml

<dataConfig>
		<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.131:3306/hongone"  user="root"  password="***"/>
           <document>
                <entity name="ho_front_message" pk="id" transformer="DateFormatTransformer" 
				query="SELECT id, content,object_id,type,update_date,create_date FROM ho_front_message"
				>
					<field column="id" name="id"/>
					<field column="object_id" name="objectId"/>
					<field column="content" name="content"/>
					<field column="type" name="type"/>
					<field column="enable_flag" name="enableFlag"/>
                    <field column="update_date" name="updateDate" dateTimeFormat="yyyy-MM-dd HH:mm:ss" />
					<field column="create_date" name="createDate" dateTimeFormat="yyyy-MM-dd HH:mm:ss" />
                </entity>
          </document>
</dataConfig

注意:修改 mysql 連接地址和數(shù)據(jù)庫(kù)名和用戶名和密碼

entity 標(biāo)簽下 

-name:表名

-pk:主鍵名

-query:查詢語(yǔ)句,全量同步下會(huì)同步當(dāng)前表中哪些數(shù)據(jù)

-field:表子段映射,注意時(shí)間格式

以上需要同步的表子段,需要配置到 managed-schema.xml ,對(duì)于已有的字段,不需要添加,例如 id 字段

  <!--ho_front_message-->
  <field name="type" type="string" indexed="true" stored="true"/>
  <field name="objectId" type="string" indexed="true" stored="true"/>
  <field name="enableFlag" type="string" indexed="true" stored="true"/>
  <field name="createDate" type="pdate" indexed="true" stored="true"/>
  <field name="updateDate" type="pdate" indexed="true" stored="true"/>
   <!--ho_front_message-->

注意 type="pdate" 因?yàn)槲业?solr 是7.5 版本的

三、測(cè)試數(shù)據(jù)

solr同步mysql的方法

選擇 full-import  全量導(dǎo)入

勾選 clean 表示導(dǎo)入之前會(huì)清空數(shù)據(jù)

entity 選擇我們?cè)赿ata-config.xml創(chuàng)建的

solr同步mysql的方法

可以看到數(shù)據(jù)已經(jīng)查詢出來(lái)了

四、增量同步

修改 data-config.xml

<dataConfig>
		<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.131:3306/hongone"  user="root"  password="***"/>
           <document>
                <entity name="ho_front_message" pk="id" transformer="DateFormatTransformer" 
				query="SELECT id, content,object_id,type,update_date,create_date FROM ho_front_message"
				deltaQuery="select id from ho_front_message where update_date > '${dih.last_index_time}'"
				deltaImportQuery="select * from ho_front_message where id='${dih.delta.id}'"
                deletedPkQuery="select id from ho_front_message where enable_flag='0'"
				>
					<field column="id" name="id"/>
					<field column="object_id" name="objectId"/>
					<field column="content" name="content"/>
					<field column="type" name="type"/>
					<field column="enable_flag" name="enableFlag"/>
                    <field column="update_date" name="updateDate" dateTimeFormat="yyyy-MM-dd HH:mm:ss" />
					<field column="create_date" name="createDate" dateTimeFormat="yyyy-MM-dd HH:mm:ss" />
                </entity>
          </document>
</dataConfig>

deltaQuery:增量索引查詢主鍵ID
deltaImportQuery:增量索引查詢導(dǎo)入的數(shù)據(jù)
deletedPkQuery:此操作值查詢那些數(shù)據(jù)庫(kù)里偽刪除的數(shù)據(jù)的ID(enable_flag=0的數(shù)據(jù))

配置完后后,打開(kāi)數(shù)據(jù)庫(kù)修改其中一條記錄的值和update_date

update ho_front_message set content='xxxx' ,update_date=now() where id='xxx'

導(dǎo)入增量數(shù)據(jù),勾選 delta-import

solr同步mysql的方法

再次查詢看看增量數(shù)據(jù)是否正確

solr同步mysql的方法

刪除數(shù)據(jù)就是把某條數(shù)據(jù)的 enable_flag=0 ,再操作一下增量導(dǎo)入

“solr同步mysql的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI