当前位置:   article > 正文

Solr(三):数据库数据批量导入solr_如何向solr中导入大量数据

如何向solr中导入大量数据

一、实现功能

将数据库中数据,批量导入solr,构建全文检索。

二、环境

1.CentOS6.4

2.CDH5.7.0

3.solr-4.10.3-cdh5.7.0

三、步骤

1.导入原始数据库

2.依据业务修改schema.xml

vi solr-4.10.3-cdh5.7.0/solrhome/collection2/conf/schema.xml

针对具体的业务需要自定义一套Field,例如:

  1. <!--product-->
  2. <field name="product_name" type="text_ik" indexed="true" stored="true"/>
  3. <field name="product_price" type="float" indexed="true" stored="true"/>
  4. <field name="product_description" type="text_ik" indexed="true" stored="false" />
  5. <field name="product_picture" type="string" indexed="false" stored="true" />
  6. <field name="product_catalog_name" type="string" indexed="true" stored="true" />
  7. <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
  8. <copyField source="product_name" dest="product_keywords"/>
  9. <copyField source="product_description" dest="product_keywords"/>

这些自定义域,要与数据库中对应字段对应,名称不一定一样,但是要有清晰的对应关系,如下

3.使用dataimport插件批量导入,将dataimport插件依赖的jar包添加到实例solrcore的lib下(solrhome/collection1/lib)中

(1)solr-4.10.3-cdh5.7.0/dist中的

  1. solr-dataimporthandler-4.10.3-cdh5.7.0.jar
  2. solr-dataimporthandler-extras-4.10.3-cdh5.7.0.jar

(2)mysql数据库连接驱动包

mysql-connector-java-5.1.27-bin.jar

4.配置solrconfig.mxl文件,添加一个requestHandler

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

5.在solrconfig.mxl同级目录下,创建data-config.xml,即数据导入配置文件。

(1)作用是:

-》连接数据库

-》SQL语句

-》构建数据库原始字段与solr新定义的域的对应关系

(2)具体内容

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <dataConfig>
  3. <dataSource type="JdbcDataSource"
  4. driver="com.mysql.jdbc.Driver"
  5. url="jdbc:mysql://hadoop:3306/test"
  6. user="root"
  7. password="mysql密码"/>
  8. <document>
  9. <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
  10. <field column="pid" name="id"/>
  11. <field column="name" name="product_name"/>
  12. <field column="catalog_name" name="product_catalog_name"/>
  13. <field column="price" name="product_price"/>
  14. <field column="description" name="product_description"/>
  15. <field column="picture" name="product_picture"/>
  16. </entity>
  17. </document>
  18. </dataConfig>

6.重启tomcat

四、测试

1.进入solr

2.点击Execute执行,然后,等待结果

3.成功~

 

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号