1. 程式人生 > >solr+mysql資料同步配置

solr+mysql資料同步配置

這兩天在網上連續踩了N個坑,使我堅定一定要把這個簡單但容易出錯的配置一步一步的記錄下來。

solr版本5.5.3 下載連結:http://apache.fayea.com/lucene/solr/5.5.3/solr-5.5.3.tgz

上一篇博文中我已經完成了資料如何從mysql中匯入到solr。

基本要新增修改的檔案是自定義core下面conf中:schema.xml  db-data-config.xml solrconfig.xml

今天在這基礎上做同步其實一樣主要修改兩個檔案:schema.xml db-data-config.xml

如下:db-data-config.xml (注意這個檔案非常關鍵以下每個關鍵字大小寫都得正確才行,特別是 deletedPKQuery select id where條件等)

<?xml version="1.0" encoding="UTF-8" ?>  
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_ldjs" user="root" password="123456"/>
<document name="testmysqladdDoc">
    <entity name="tb_solr_add"
            pk="id"
            query="select id,province,city,company,name,mobile,total,flg,modify_time,is_deleted from tb_solr_add where is_deleted=0"
            deltaImportQuery="select id,province,city,company,name,mobile,total,flg,modify_time,is_deleted  from tb_solr_add where ID='${dih.delta.id}'"
            deltaQuery="select id from tb_solr_add where modify_time> '${dataimporter.last_index_time}' and is_deleted=0"
            deletedPkQuery="select id from tb_solr_add where is_deleted=1">
    <field column="id" name="id"/>
    <field column="province" name="province"/>
    <field column="city" name="city"/>
    <field column="company" name="company"/>
    <field column="name" name="name"/>
    <field column="mobile" name="mobile"/>
    <field column="total" name="total"/>
    <field column="flg" name="flg"/>
    <field column="modify_time" name="modify_time"/>
    <field column="is_deleted" name="is_deleted"/>
   </entity>
</document>
</dataConfig>

schema.xml:中欄位定義如下:

<!-- <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> -->
   <field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="province" type="string" indexed="true" stored="true" />
   <field name="city" type="string" indexed="true" stored="true" />
   <field name="company" type="string" indexed="true" stored="true" />
   <field name="name" type="string" indexed="true" stored="true" />
   <field name="mobile" type="string" indexed="true" stored="true" />
   <field name="total" type="string" indexed="true" stored="true" />
   <field name="flg" type="string" indexed="true" stored="true" />
   <field name="modify_time" type="date" indexed="true" stored="true" />
   <field name="is_deleted" type="boolean" indexed="true" stored="true" />


mysql資料庫的表定義如下:

-- ----------------------------
--  Table structure for `tb_solr_add`
-- ----------------------------
DROP TABLE IF EXISTS `tb_solr_add`;
CREATE TABLE `tb_solr_add` (
  `id` bigint(18) NOT NULL AUTO_INCREMENT,
  `province` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `company` varchar(100) DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `mobile` varchar(20) NOT NULL DEFAULT '',
  `total` decimal(40,0) DEFAULT NULL,
  `flg` varchar(3) NOT NULL DEFAULT '',
  `modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '刪除標記 1:表示已經刪除,0:正常',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = 1;

定時部分:

建議直接用Linux的crontab

時間自己設定一下

首先你要full-import一下,後面可以設定每分鐘更新一次:

#00 20 * * * curl "http://localhost:8283/solr/ndc-prod-customer-2/dataimport?command=delta-import&clean=false&commit=true"; 這樣就OK了 你無論是在mysql資料庫中 update,insert,還是delete 就都會同步到solr中 但是delete的話我們在mysql中做的邏輯刪除 update tb_solr_add set is_deleted=1 where id=9