solr mysql 集成_Solr与Mysql集成指南

在《企业级搜索引擎Solr使用入门指南》及

从Solr 1.3开始,Solr提供DataImportHandler工具完成对mysql数据库数据的操作。因此极大方便了Solr与Mysql的集成,因此可以借助DataImportHandler来完成Solr与Mysql的集成,基本思路如下:

1、使用mysql的触发器trigger获取数据库表数据的增删改操作

2、触发器通过mysql的用户自定义函数user-defined function (UDF) 包mysql-udf-http 调用Solr DataImportHandler对应的命令

3、由Solr DataImportHandler完成增删改数据的索引操作。

此种方案的优点在于将索引逻辑完全与应用的业务逻辑处理完全隔离,尤其是对批量导入、增量导入有其优势。

下面简单介绍Solr与Mysql集成的方案。关于solr的配置及使用说明可参考《企业级搜索引擎Solr使用入门指南》及

测试环境说明:

操作系统:CentOS 5

数据库:Mysql 5 ,直接使用了rpm安装的,mysql的lib库安装在/usr/lib/mysql

Tomcat 6:tomcat安装在/opt/tomcat

JDK 6

一、Solr安装配置

1)、Solr的安装

wget http://www.apache.org/dist//lucene/solr/3.4.0/apache-solr-3.4.0.tgz

tar zxvf apache-solr-3.4.0.tgz

cd apache-solr-3.4.0

cp -r example/webapps/solr.war /opt/tomcat/webapps/

cp -r example/solr/ /opt/solr-tomcat

cp -r dist/  /opt/solr-tomcat/

注dist目录下中有使用dataimport所必须的jar包apache-solr-dataimporthandler

将mysql的JDBC Driver mysql-connector-java-5.1.17-bin.jar拷贝到/opt/solr-tomcat/lib/下

设定solr.solr.home

在当前用户的环境变量中(.bash_profile)或在/opt/tomcat/catalina.sh中添加如下环境变量

export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/opt/solr-tomcat/solr"

mkdir –p /opt/tomcat/conf/Catalina/localhost

vi  /opt/tomcat/conf/Catalina/localhost/solr.xml ,内容如下:

注:

为了避免在Solr管理界面测试时候出现中文乱码问题,需要做如下设置:

/opt/tomcat/conf/server.xml 中设定URIEncoding:

connectionTimeout="20000"

redirectPort="8443"  URIEncoding="UTF-8" />

/opt/tomcat/webapps/solr/WEB-INF/web.xml 设定过滤器

Set Character Encoding

org.apache.catalina.filters.SetCharacterEncodingFilter

encoding

UTF-8

ignore

true

Set Character Encoding

/*

在xml请求数据中增加编码说明:

123

Solr企业级搜索

Apache Software Foundation

liangchuan’s solr "hello,world" test

http://www.google.com

3)、配置solr

cd /opt/solr-tomcat/

vi solrconfig.xml ,增加如下内容

/opt/solr-tomcat/conf/data-config.xml

com.mysql.jdbc.Driver

jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8

user

password

这里的data-config.xml就是DataImportHand

ler的配置文件

vi  /opt/solr-tomcat/conf/data-config.xml,内容如下:

query="select id,title,content,date_added from documents"

deltaImportQuery="select  id,title,content,date_added  from documents where ID=’${dataimporter.delta.id}’"

deltaQuery="select id  from documents where date_added > ‘${dataimporter.last_index_time}’"

>

这里就常用的用法简单说明:

query语句是用于批量导入(Full Import)中获取符合条件的全部数据的SQL。

deltaQuery语句是用于增量导入(Delta Import)中获取符合增量导入标准的数据的主键的SQL,供deltaImportQuery查询使用。

deltaImportQuery语句增量导入(Delta Import)中获取需要增量索引数据(document)的字段(field)

DataImportHandler的常见命令:

批量导入(full-import):

http://:/solr/dataimport?command=full-import&commit=y

增量导入(delta-import):

http://:/solr/dataimport?command=delta-import&commit=y

导入状态查询(status):

http://:/solr/dataimport

重新装载配置文件(reload-config):

http://:/solr/dataimport?command=reload-config

终止导入(abort):

http://:/solr/dataimport?command=abort

Solr执行增量导入的大致原理:

1、Solr 读取conf/dataimport.properties 文件,得到solr最后一次执行索引操作的时间戳last_index_time,以及单个实体最后一次执行索引的时间戳:entity_name.last_index_time

2、Solr对指定的实体使用deltaImportQuery SQL查询得到insert或update时间戳大于${dataimporter.last_index_time}需要增量索引的字段,然后调用deltaQuery对符合条件需要执行增量索引的文档的字段进行索引,并更新dataimport.properties 的时间戳

数据库的测试表结构如下:

DROP TABLE IF EXISTS `documents`;

CREATE TABLE `documents` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`date_added` datetime NOT NULL,

`title` varchar(255) NOT NULL,

`content` text NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4)、测试Solr配置

在数据库中documents表中插入几条数据,然后

执行批量导入操作:

tar zxvf mysql-udf-http-1.0.tar.gz

cd mysql-udf-http-1.0/

./configure –prefix=/usr –with-mysql=/usr/bin/mysql_config

make && make install

echo "/usr/lib/mysql/plugin" >> /etc/ld.so.conf.d/mysql-i386.conf

/sbin/ldconfig

/sbin/ldconfig -v|grep mysql

如果没有类似如下的输出

/usr/lib/mysql/plugin:

mysql-udf-http.so.0 -> mysql-udf-http.so   则建立mysql-udf-http.so 的软连接(当然也可以通过修改编译Makefile脚本)

ln -s /usr/lib/mysql/plugin/mysql-udf-http.so /usr/lib/mysql/plugin/libmysql-udf-http.so

建立软连接后再执ldconfig 命令,使用/sbin/ldconfig -v|grep mysql是否有如下内容:

/usr/lib/mysql/plugin:

mysql-udf-http.so.0 -> libmysql-udf-http.so原因是mysql-udf-http的Makefile中指定了动态链接库的SONAME为mysql-udf-http.so.0 ,而ldconfig缺省只处理以lib开头的动态链接库(例如libmysqlclient.so.15),因此执行ldconfig命令时候忽略了mysql-udf-http.so 。

创建mysql的用户自定义函数:

mysql >

create function http_get returns string soname ‘libmysql-udf-http.so’;

create function http_post returns string soname ‘libmysql-udf-http.so’;

create function http_put returns string soname ‘libmysql-udf-http.so’;

create function http_delete returns string soname ‘libmysql-udf-http.so’;

三、创建调用mysql-udf-http的触发器

/* INSERT插入操作的触发器 */

DELIMITER |

DROP TRIGGER IF EXISTS documents_insert;

CREATE TRIGGER documents_insert

AFTER INSERT ON documents

FOR EACH ROW BEGIN

SET @result = (SELECT http_get(‘http://www.yeeach.com/solr/dataimport?command=delta-import&commit=yes’));

END |

DELIMITER ;

/* UPDATE更新操作的触发器 */

DELIMITER |

DROP TRIGGER IF EXISTS documents_update;

CREATE TRIGGER documents_update

AFTER UPDATE ON documents

FOR EACH ROW BEGIN

60;    SET @result = (SELECT http_get("http://www.yeeach.com/solr/dataimport?command=delta-import&commit=yes"));

END |

DELIMITER ;

/* DELETE删除操作的触发器 */

DELIMITER |

DROP TRIGGER IF EXISTS documents_delete;

CREATE TRIGGER documents_delete

AFTER DELETE ON documents

FOR EACH ROW BEGIN

SET @result = (SELECT http_get(CONCAT(‘http://www.yeeach.com/solr/update/?stream.body=’,OLD.id,’&stream.contentType=text/xml;charset=utf-8&commit=true’)));

END |

DELIMITER ;

四、测试

在数据库执行增删改操作,然后在Solr的管理界面执行查询操作,测试触发器调用mysql-udf-http是否正常。

五、参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值