solr mysql增量导入_Solr全量与增量导入
Solr Full Import全量導(dǎo)入
所謂全量索引一般指的是每次從數(shù)據(jù)庫(kù)中讀取需要導(dǎo)入的全部數(shù)據(jù),然后提交到Solr Server,最后刪除指定core的所有索引數(shù)據(jù)進(jìn)行重建。全量導(dǎo)入一般在數(shù)據(jù)首次導(dǎo)入或者備份數(shù)據(jù)恢復(fù)時(shí)執(zhí)行。
以下為一個(gè)多表查詢的全量導(dǎo)入案例:
ER圖:
ER圖
根據(jù)如上ER圖,我們?cè)跀?shù)據(jù)庫(kù)中執(zhí)行如下SQL建表并插入測(cè)試數(shù)據(jù)。
use solr;
create table feature(item_id bigint,descrip varchar(80));
create table item(id bigint, item_name varchar(20), manu varchar(20), weight float,price float, popularity int, includes varchar(20));
create table item_category(item_id bigint, category_id bigint);
create table category(id bigint, descrip varchar(80));
alter table item add primary key(id);
alter table item_category add primary key(item_id, category_id);
alter table category add primary key(id);
insert into item values(1,"item1", "menu1", 12.0, 33.1, 10, "includes1");
insert into item_category values(1,1);
insert into category values(1,"this is the description of category 1");
insert into feature values(1,"this is the feature 1");
需求描述:我們希望將item表的所有字段以及item的category信息,item的descrip描述信息一并導(dǎo)入到solr指定的core中,因此solr的schema.xml中需要預(yù)先定義如下域:name, manu, weight, price, popularity, includes, cat, features.
解決方案:
比較容易想到的就是通過SQL語(yǔ)句一并返回所需的域(數(shù)據(jù))。
use solr;
select i.id,i.item_name,i.manu,i.weight,i.price,i.popularity,i.includes,c.descrip as cat,f.descrip as feature from item i, item_category ic, category c, feature f where i.id=ic.item_id and ic.category_id=c.id and i.id=f.item_id;
也可以通過data-config.xml中嵌套entity來實(shí)現(xiàn)。
Solr Delta Import增量導(dǎo)入
當(dāng)索引數(shù)據(jù)量很大時(shí),每次都依靠全量導(dǎo)入顯然很不切實(shí)際,所以增量導(dǎo)入索引數(shù)據(jù)更為重要。
增量導(dǎo)入操作內(nèi)部是新開辟一個(gè)新線程來完成,并且此時(shí)core的dataimport運(yùn)行狀態(tài)為status="busy"。增量導(dǎo)入耗時(shí)時(shí)間取決于需要增量導(dǎo)入的數(shù)據(jù)集合大小。任何時(shí)候你都可以通過http://localhost:8080/solr//dataimport 這個(gè)鏈接來獲取到增量導(dǎo)入的運(yùn)行狀態(tài)。
當(dāng)增量導(dǎo)入操作被執(zhí)行,他會(huì)讀取存儲(chǔ)在conf/deltaimport.properties配置文件,利用配置文件里記錄的上一次操作時(shí)間來運(yùn)行增量查詢,增量導(dǎo)入完成后,會(huì)更新conf/deltaimport.properties配置文件里的上一次操作時(shí)間戳。首次執(zhí)行增量導(dǎo)入時(shí),若conf/deltaimport.properties配置文件不存在,會(huì)自動(dòng)創(chuàng)建。
#Sun Mar 03 19:59:43 IRKT 2019
item.last_index_time=2019-03-03 19\:59\:43
last_index_time=2019-03-03 19\:59\:43
如果要使用增量導(dǎo)入,前提是你的表必需有兩個(gè)字段,一個(gè)是刪除標(biāo)志字段即邏輯刪除標(biāo)志:isdeleted,另一個(gè)則是數(shù)據(jù)創(chuàng)建時(shí)間字段:create_date,字段名稱不一定非得是isdeleted和create_date,但必須要包含兩個(gè)表示該含義的字段。根據(jù)數(shù)據(jù)創(chuàng)建時(shí)間跟上一次增量導(dǎo)入操作時(shí)間一對(duì)比,就可以通過SQL語(yǔ)句查詢出需要增量導(dǎo)入的數(shù)據(jù),根據(jù)isdeleted字段可以查詢出被標(biāo)記為刪除的數(shù)據(jù),這些數(shù)據(jù)的ID主鍵需要傳遞給solr,這樣solr就能同步刪除索引中相關(guān)Document,實(shí)現(xiàn)數(shù)據(jù)增量更新。如果你數(shù)據(jù)表里的數(shù)據(jù)都是物理刪除,沒有邏輯標(biāo)志字段的話,那么找出已刪除的數(shù)據(jù)顯得比較困難,所以這就是需要邏輯刪除標(biāo)志字段的原因。
仍然使用上一節(jié)的那幾張表為例。對(duì)于復(fù)合主鍵記錄的增量更新,solr會(huì)拋出deltaQuery has no column to resolve to declared paimary key pk='key1, key2',暫時(shí)還沒有找到合適的解決方案。如有,請(qǐng)留言告知,謝謝。
pk:表示當(dāng)前entity表示主鍵字段名稱,這里的主鍵指的是數(shù)據(jù)庫(kù)表中的主鍵,而非solr中的uniqueKey主鍵域。如果你的sql語(yǔ)句中使用了as關(guān)鍵字為主鍵字段定義了別名,那么這里的pk屬性需要相應(yīng)的修改為主鍵字段的別名,切記;
query:用于指定全量導(dǎo)入時(shí)需要的sql語(yǔ)句,比如select * from xxx where isdeleted=0,查詢返回的是為被刪除的所有有效數(shù)據(jù),這個(gè)query參數(shù)只對(duì)全量導(dǎo)入有效,對(duì)增量導(dǎo)入無效;
deltaQuery:查詢需要增量導(dǎo)入的記錄的主鍵id所需的sql語(yǔ)句。可能是update,insert,delete等操作,比如:deltaQuery="select id from xxx where my_date > '${dataimporter.last_index_time}'",此參數(shù)值對(duì)增量導(dǎo)入有效;
deletedPkQuery:查詢已經(jīng)被邏輯刪除了的數(shù)據(jù)所需的SQL語(yǔ)句,所以這里你需要一個(gè)類似isdeleted的邏輯刪除標(biāo)志位字段。solr通過此參數(shù)表示的sql語(yǔ)句執(zhí)行后返回的結(jié)果集來刪除索引里面對(duì)應(yīng)的數(shù)據(jù)。使用示例:select id from myinfo where isdeleted=1,此參數(shù)對(duì)增量導(dǎo)入有效。
deltaImportQuery: deltaImpotQuery="select * from myinfo where id='${dataimporter.delta.id}'",利用deltaQuery參數(shù)返回的所有需要增量導(dǎo)入的數(shù)據(jù)主鍵id,遍歷每個(gè)主鍵id,然后循環(huán)執(zhí)行deltaImportQuery參數(shù)表示的sql語(yǔ)句返回所有需要增量導(dǎo)入的數(shù)據(jù)。其中變量${dataimporter.delta.id}用于獲取deltaQuery返回的每個(gè)主鍵id。
總結(jié)
以上是生活随笔為你收集整理的solr mysql增量导入_Solr全量与增量导入的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker ubuntu 安装包_史上
- 下一篇: 荣耀v40充电快吗 续航和快充都很重要