mysql导入数据到solrcloud5

本文是用solr+jetty方式的环境下完成的。
一、全量导入
1、上传包到/opt/solr/server/solr-webapp/webapp/WEB-INF/lib,在集群中的每个节点上都执行,并且需要重启solr服务

solr-dataimporthandler-5.2.1.jar
solr-dataimporthandler-extras-5.2.1.jar
注意:上述2个jar包在solr5.2.1的dist目录下
mysql-connector-java-5.1.31.jar

2、在/opt/solr/server/solr/configsets/data_driven_schema_configs/conf/solrconfig.xml文件中配置如下代码段

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">mysql-config.xml</str>
    </lst>
</requestHandler>

3、在/opt/solr/server/solr/configsets/data_driven_schema_configs/conf/目录下创建mysql-config.xml

<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.222:3306/test" user="root" password="123456"/>
    <document>
        <entity name="t_person" query="select id, name,sex,age from t_person"><!--t_person,要导入的表名-->
            <field column="id" name="id" />
            <field column="name" name="name" />
            <field column="sex" name="sex" />
            <field column="age" name="age" />
        </entity>
    </document>
</dataConfig>

4、在/opt/solr/server/solr/configsets/data_driven_schema_configs/conf/managed-schema文件中修改或者加入如下代码段

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true"/>
<field name="sex" type="string" indexed="true" stored="true"/>
<field name="age" type="int" indexed="true" stored="true"/>

5、mysql赋权
在win7控制台,输入mysql -u root,以root管理员身份登录mysql,然后执行:

grant all PRIVILEGES on *.* to 'root'@'192.168.1.240'  identified by '123456';

二、增量导入

增量导入包括全量导入的功能,步骤也和全量导入一样,下面只描述和全量导入不一致的地方(mysql-config.xml)。

注意:需要增量导入数据到solr中,那么数据库表必须新增一个时间字段,这个字段需要和上次导入时间进行比较。

<dataConfig>
    <!--solrDB表示数据源名称,和entity中的dataSource对应,如果需要导入多个mysql库的数据时,需要配置多个数据源-->
    <!--<dataSource name="solrDB1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.225:3306/test" user="root" password="123456"/>-->
    <dataSource name="solrDB" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.222:3306/test" user="root" password="123456"/>
    <document>
        <entity pk="id" <!--指明主键,必须和下面<field column="id" name="id" />中name="id" 这里一致,包括大小写,建议下面涉及到id的都和这里的id一致-->
                dataSource="solrDB"
                name="t_person" 
                query="select id, name,sex,age,update_time from t_person"
                <!--表示根据id查询数据并导入solr,${dih.delta.id}这里一定要这么写,不这么写是否正确没有验证-->
                deltaImportQuery="select id, name,sex,age,update_time from t_person where ID= '${dih.delta.id}'"
                <!--根据更新时间大于上次导入时间查询需要导入的数据id,{dataimporter.last_index_time}这里一定要这么写,因为zookeeper中记录的上次更新时间就是last_index_time-->
                deltaQuery="select id from t_person where update_time > '${dataimporter.last_index_time}'"
                >
            <field column="id" name="id" />
            <field column="name" name="name" />
            <field column="sex" name="sex" />
            <field column="age" name="age" />
            <field column="update_time" name="update_time" /><!--新增字段-->
        </entity>
        <!--
        <entity pk="id"
                dataSource="solrDB1"
                name="t_person" 
                query="select id, name,sex,age,update_time from t_person"
                deltaImportQuery="select id, name,sex,age,update_time from t_person where ID= '${dih.delta.id}'"
                deltaQuery="select id from t_person where update_time > '${dataimporter.last_index_time}'"
                >
            <field column="id" name="id" />
            <field column="name" name="name" />
            <field column="sex" name="sex" />
            <field column="age" name="age" />
            <field column="update_time" name="update_time" />
        </entity>
        -->
    </document>
</dataConfig>

4、在managed-schema文件中修改或者加入如下代码段

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true"/>
<field name="sex" type="string" indexed="true" stored="true"/>
<field name="age" type="int" indexed="true" stored="true"/>
<field name="update_time" type="date" indexed="true" stored="true"/>

三、定时增量导入(在增量基础上操作)
1、下载并上传solr-dataimportscheduler-1.1.jar包到/opt/solr/server/solr-webapp/webapp/WEB-INF/lib,在集群中的每个节点上都执行。(这个jar包在solr5.2.1中没有,需要在网上去下载)
2、在solrhome目录下(/var/solr/data)新建文件夹conf,并新建文件dataimport.properties,文件复制下面的就好,具体配置含义已给出注释

    #  to sync or not to sync
    #  1 - active; anything else - inactive
    # 这里的配置不用修改
    syncEnabled=1

    #  which cores to schedule
    #  in a multi-core environment you can decide which cores you want syncronized
    #  leave empty or comment it out if using single-core deployment
    #  修改成你所使用的core,我这里是我自定义的core:simple
    syncCores=mycollection3

    #  solr server name or IP address
    #  [defaults to localhost if empty]
    #这个一般都是localhost不会变
    server=localhost

    #  solr server port
    #  [defaults to 80 if empty]
    #  安装solr的tomcat端口,如果你使用的是默认的端口,就不用改了,否则改成自己的端口就好了
    port=8983

    #  application name/context
    #  [defaults to current ServletContextListener's context (app) name]
    #  这里默认不改
    webapp=solr

    #  URL params [mandatory]
    #  remainder of URL
    #  这里改成下面的形式,solr同步数据时请求的链接
    params=/deltaimport?command=delta-import&clean=false&commit=true

    #  schedule interval
    #  number of minutes between two runs
    #  [defaults to 30 if empty]
    #这里是设置定时任务的,单位是分钟,也就是多长时间你检测一次数据同步,根据项目需求修改
    #  开始测试的时候为了方便看到效果,时间可以设置短一点
    interval=1

    #  重做索引的时间间隔,单位分钟,默认7200,即5天; 
    #  为空,为0,或者注释掉:表示永不重做索引
    reBuildIndexInterval=7200

    #  重做索引的参数
    reBuildIndexParams=/select?qt=/deltaimport&command=full-import&clean=true&commit=true

    #  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
    #  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期
    reBuildIndexBeginTime=03:10:00

3、在各个节点上的/opt/solr-5.2.1/server/solr-webapp/webapp/WEB-INF目录下的web.xml文件中添加监听配置:

    <listener>
        <listener-class>
            org.apache.solr.handler.dataimport.scheduler.ApplicationListener
        </listener-class>
    </listener>

4、最后重启solr服务

四、数据导入http方式的API:
http://host:port/solr/collection_name/dataimport?command=delta-import&clean=true&commit=true&optimize=true&debug=true&entity=xxxx

command:
    abort           强制取消(实验证明和status一样)
    delta-import    增量导入
    full-import     全量导入
    reload-config   重新加载数据导入相关配置文件(理论上而言没什么卵用,实验证明和status一样)
    status          获取数据导入相关状态信息
    show-config     返回相关配置信息

full-import/delta-import相关参数:
    clean           决定在建立索引之前,删除以前的索引,默认为true
    commit          决定这个操作之后是否要commit,默认为true
    debug           以debug的方式执行,默认为false
    entity          导入那个表里的数据,如果未填写,则导入所有表的数据
    optimize        决定这个操作之后是否要优化,默认为true
    synchronous     是否同步方式进行

猜你喜欢

转载自blog.csdn.net/zhaocuit/article/details/71915173