wskickahn 发表于 2015-11-12 09:19:58

Solr之关系型数据库导入数据

  通常创建数据库有有如下方式:
  1.通过程序或http创建索引
  2.通过和数据库对接自动创建索引
  下面我们演示一下通过数据库全量或增量创建索引。以mysql为例。
  一、创建测试数据库
  

-- ----------------------------
-- Table structure for `tb_student`
-- ----------------------------
DROP TABLE IF EXISTS `tb_student`;
CREATE TABLE `tb_student` (
`id` varchar(40) COLLATE utf8_bin NOT NULL,
`name` varchar(40) COLLATE utf8_bin DEFAULT NULL COMMENT '姓名',
`age` int(4) DEFAULT NULL COMMENT '年龄',
`desc` varchar(200) COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
`updateDate` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Records of tb_student
-- ----------------------------
INSERT INTO `tb_student` VALUES ('421245251215121452521251', '张三', '30', '张三是个农民,勤劳致富,奔小康', '2015-06-17 15:37:29');
INSERT INTO `tb_student` VALUES ('4224558524254245848524243', '李四', '25', '李四是个企业家,白手起家,致富一方', '2015-07-16 15:37:57');
INSERT INTO `tb_student` VALUES ('2224558524254245848524299', '王五', '40', '王五好吃懒做,溜须拍马,跟着李四,也过着小康的日子', '2015-07-01 15:38:16');
二、安装配置solr  
  1.安装solr略,配置field
  

<field name=&quot;name&quot; type=&quot;textComplex&quot; indexed=&quot;true&quot; stored=&quot;true&quot; />   
<field name=&quot;age&quot; type=&quot;int&quot; indexed=&quot;false&quot; stored=&quot;true&quot; />   
<field name=&quot;desc&quot; type=&quot;textComplex&quot; indexed=&quot;true&quot; stored=&quot;true&quot; />   
<!--拷贝字段 多值-->   
<field name=&quot;context&quot; type=&quot;textComplex&quot; indexed=&quot;true&quot; stored=&quot;false&quot; multiValued=&quot;true&quot;/>

<copyField source=&quot;name&quot; dest=&quot;context&quot;/>
<copyField source=&quot;desc&quot; dest=&quot;context&quot;/>以及中文分词,参考:http://blog.iyunv.com/zhu_tianwei/article/details/46711511
  
  2.配置solr与数据同步
  1)solr-5.2.1\dist目录下的solr-dataimporthandler-5.2.1.jar和solr-dataimporthandler-extras-5.2.1.jar复制到apache-tomcat-7.0.30\webapps\solr\WEB-INF\lib目录下
  2)复制一个MySQL数据库驱动jar包到到apache-tomcat-7.0.30\webapps\solr\WEB-INF\lib目录下
  3)配置

默认dataImport功能在Solr5中是禁用的,需要在solrconfig.xml中添加如下配置开启数据导入功能:



<requestHandler name=&quot;/dataimport&quot; class=&quot;solr.DataImportHandler&quot;>
<lst name=&quot;defaults&quot;>
<str name=&quot;config&quot;>solr-data-config.xml</str>
</lst>
</requestHandler>这里的solr-data-config是个相对路径,是相对于你当前core下的conf目录即apache-tomcat-7.0.30\webapps\solr\solr_home\test\conf,当然你也可以直接写成绝对路径,建议写成相对路径。
  
  编辑:solr-data-config.xml
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<dataConfig>
<dataSource name=&quot;solrDB&quot; type=&quot;JdbcDataSource&quot;
driver=&quot;com.mysql.jdbc.Driver&quot;
user=&quot;root&quot; password=&quot;123456&quot; batchSize=&quot;-1&quot;
url=&quot;jdbc:mysql://192.168.100.206:3306/solr&quot; />
<document>
<entityname=&quot;student&quot; dataSource=&quot;solrDB&quot;
query=&quot;SELECT id,name,age,`desc` FROM tb_student&quot;
deltaImportQuery=&quot;SELECT id, name,age,`desc` FROM tb_student WHERE id='${dataimporter.delta.id}'&quot;
deltaQuery=&quot;SELECT id FROM tb_student WHERE updateDate > '${dataimporter.last_index_time}'&quot;>
<field column=&quot;id&quot; name=&quot;id&quot; />
<field column=&quot;name&quot; name=&quot;name&quot; />
<field column=&quot;age&quot; name=&quot;age&quot; />
<field column=&quot;desc&quot; name=&quot;desc&quot; />
</entity>
</document>
</dataConfig>
  
  其中solrDB为数据源自定义名称,随便取,没什么约束,type这是固定值,表示JDBC数据源,后面的driver表示JDBC驱动类,这跟你使用的数据库有关,url即JDBC链接URL,后面的user,password分别表示链接数据库的账号密码,下面的entity映射有点类似hiberante的mapping映射,column即数据库表的列名称,name即schema.xml中定义的域名称。
  根元素是document。一个document代表了一种文档。一个document元素包含一个或者多个root实体。一个root实体包含多个子实体。实体实际就是数据库中的表或者视图。

entity属性:

name 唯一标识,用于识别entity

processor:只有当datasource是RDBMS时才是必须的。默认是SqlEntityProcessor

transformer:转换器将会被应用到这个entity上。

pk:entity的主键,可选。但是用增量导入的时候是必需的。它跟schema.xml中定义的uniqueKey没有必然的联系。但他们可以相同。

rootEntity:document元素下就是根实体,每一个实体就是一个document
  SqlEntityProcessor的一些属性:

query 必须的 sql语句

deltaQuery 增量导入的时候使用

parentDeltaQuery 增量导入的时候使用

deletedPKQuery 增量导入的时候使用

deltaImportQuery 增量导入的时候使用,如果这个存在,那么他将会在增量导入中导入phase时替代query产生作用。


  启动tomcat服务。
  三、同步数据

访问web控制台,点击左侧的Dataimport菜单,如下图:
  


  full-import:全量导入,它会覆盖原有的索引

delta-import:即增量导入,它会在原有索引的基础上追加

下面的几个多选框含义解释如下:

verbose:这个选项设为true的话,会打印导入的一些中间过程的详细信息,有利于调试以及了解内部操作细节

clean:表示是否在导入数据创建索引之前先清空掉原有的索引

commit:表示是否立即提交索引

optimize:表示是否优化索引

debug: 表示是否开启调试模式

然后选择需要导入的Entity,点击Execute按钮开始执行数据导入操作,如图:


  


  如果你看到indexing字样,如图:


  


  如果你导入的数据并不多,但这个界面停留了很长时间(比如超过了30秒,就不需要再傻傻等着了),那么很有可能数据导入过程出错了,这时,请切换到logging菜单查看solr日志。如果出错我们需要清掉索引目录重新执行dataimport操作。


  若执行成功,对我们刚插入的索引进行查询测试:
  


  除此之外,我们可以请求http url调用数据同步:
  增量建索引链接

http://localhost:8080/solr/test/dataimport?command=delta-import&clean=false&commit=true&entity=article&optimize=false

全量建索引链接

http://localhost:8080/solr/test/dataimport?command=full-import&clean=true&commit=true&optimize=true


  参考文章:

1.Solr DIH以Mysql为数据源批量创建索引

2.解决Solr DataImportHandler(DIH)以 MySQL为数据源创建索引内存溢出问题

3.Solr实时创建增量或全量索引

4.跟益达学Solr5之从MySQL数据库导入数据并索引

5.Solr对数据库建立索引
页: [1]
查看完整版本: Solr之关系型数据库导入数据