Solr从数据库导入数据(DIH)

一. 数据导入 (DataImportHandler-DIH)

DIH 是 solr 提供的一种针对数据库、xml/HTTP、富文本对象导入到 solr 索引库的工具包。这里只针对数据库做介绍。

A、准备以下 jar 包

  • apache-solr-dataimporthandler-4.0.0.jar
  • apache-solr-dataimporthandler-extras-4.0.0.jar
  • apache-solr-dataimportscheduler-1.1.jar(增量导入使用)
  • 数据库对应的 jdbc 驱动包

这里使用的是 Oracle oracle10g.jar,放入 Tomcat6.0.36/webapps/solr/WEB-INF/lib

B、配置 solrconfig.xml

在 solrconfig.xml 中加入如下配置:

1
2
3
4
5
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">xx-data-config.xml</str>
</lst>
</requestHandler>

C、配置数据源

在与 solrconfig.xml 文件同级的目录中建立上述配置中的 xx-data-config.xml 文件,配置如下:

dih

query 属性为全导入的时候使用。其他为增量导入使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.0.129:1521:orcl"
user="username"
password="password"/>
<document>
<entity name="business_info"
pk="ID"
query="select t.ID id,business_name,bussiness_type from business t"
deltaImportQuery="select t.ID id,business_name,bussiness_type from business t where id='${dataimporter.delta.ID}'"
deltaQuery="select t.ID id,business_name,bussiness_type from business t where to_char(updatetime,'yyyy-mm-dd hh24:mi:ss')> '${dataimporter.last_index_time}'">
<field column="ID" name="id"/>
</entity>
</document>
</dataConfig>

至此所有 DIH 的配置完成,在浏览器中输入命令:

全导入:

1
http://localhost:8085/solr/core0/dataimport?command=full-import&commit=true

增量导入:

1
http://localhost:8085/solr/core0/dataimport?command=delta-import&clean=false&commit=true

查看导入状态:

1
http://localhost:8085/solr/core0/dataimport?command=status

D、处理 CLOB 字段

1
2
3
4
<entity name="meta" query="select id,filename,content,bytes from documents" transformer="ClobTransformer">
<field column="ID" name="id" />
<field column="CONTENT" name="CONTENT" clob="true" />
</entity>

CLOB 字段的 column 必须大写!!

E、DIH 内存溢出错误

在使用 DIH 时,容易报内存溢出错误。可以通过设置 jvm 大小来解决。设置方法如下:

tomcat\bin\startup.bat 加入:

1
SET JAVA_OPTS=-Xms128m -Xmx1024m

这里设置的是 1024M,根据情况可以适量增大

F、自动全导入和自动增量导入

此功能可以自己写程序实现,也可利用 apache-solr-dataimportscheduler-1.0.jar 包完成此功能。配置如下:

修改 solr.war 中 WEB-INF/web.xml,在 servlet 节点前面增加:

1
2
3
4
5
<listener>
<listener-class>
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
</listener-class>
</listener>

将 apache-solr-dataimportscheduler-.jar 中 dataimport.properties 取出并根据实际情况修改,然后放到 solr.home/conf(不是 solr.home/core/conf)目录下面

具体配置可参考:http://code.google.com/p/solr-dataimport-scheduler/

© 2025 Solr Community of China All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero