zoukankan      html  css  js  c++  java
  • Solr导入MySql中的数据

    1、参照 http://www.cnblogs.com/luxh/p/5016894.html 部署好solr的环境

    2、在solr_home下建立一个core_item目录

    [root@iZ23exixsjaZ solr_home]# pwd
    /luxh/solr/solr_home
    [root@iZ23exixsjaZ solr_home]# mkdir core_item

      在core_item目录中建立data目录

    [root@iZ23exixsjaZ core_item]# pwd
    /luxh/solr/solr_home/core_item
    [root@iZ23exixsjaZ core_item]# mkdir data

    3、将/luxh/solr/solr-5.3.1/example/example-DIH/solr/db路径下的conf目录拷贝到刚才创建的core_item目录中

    [root@iZ23exixsjaZ data_driven_schema_configs]# pwd
    /luxh/solr/solr-5.3.1/example/example-DIH/solr/db
    [root@iZ23exixsjaZ data_driven_schema_configs]# cp -r conf /luxh/solr/solr_home/core_item

    4、在solr管理界面配置添加一个core

    5、拷贝相应的jar包到solr项目中

      拷贝mysql-connector-java-5.1.37.jar和solr-dataimporthandler-5.3.1.jar到solr项目中

      拷贝路径:

    /luxh/solr/apache-tomcat-8.0.29/webapps/solr/WEB-INF/lib

    6、配置solrconfig.xml,加入solr数据导入处理器(其实从solr拷贝过来的文件已经配置好了)

      <requestHandler name="/dataimport" class="solr.DataImportHandler">
        <lst name="defaults">
          <str name="config">db-data-config.xml</str>
        </lst>
      </requestHandler>

    7、配置db-data-config.xml,加入连接数据库的信息,按自己的实际字段配置

    <dataConfig>
      <dataSource  name="testDB" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/fashion?rewriteBatchedStatements=true&amp;useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true" user="root" password="root" />
      <document>
        <entity  name="item" query="select * from item">
            <field column="Uuid" name="id"/>
            <field column="ShopId" name="ShopId"/>
            <field column="Cat" name="Cat"/>
            <field column="Building" name="Building"/>
            <field column="Floor" name="Floor"/>
            <field column="Position" name="Position"/>
            <field column="Title" name="Title"/>
            <field column="Price" name="Price"/>
            <field column="Color" name="Color"/>
            <field column="Size" name="Size"/>
            <field column="Input_str" name="Input_str"/>
        </entity>
      </document>
    </dataConfig>

      column表示数据库中的列;name是solr的schema.xml配置的field的name

    8、配置solr的schema.xml,加入一下内容:

       <field name="ShopId" type="int" indexed="false" stored="true"/>
       <field name="Cat" type="text_ansj" indexed="true" stored="true"/>
       <field name="Building" type="text_ansj" indexed="true" stored="true"/>
       <field name="Floor" type="int" indexed="false" stored="true"/>
       <field name="Position" type="text_ansj" indexed="true" stored="true"/>
       <field name="Title" type="text_ansj" indexed="true" stored="true"/>
       <field name="Price" type="float" indexed="true" stored="true"/>
       <field name="Color" type="text_ansj" indexed="true" stored="true"/>
       <field name="Input_str" type="text_ansj" indexed="true" stored="true"/>
       <field name="Size" type="text_ansj" indexed="true" stored="true"/>
      
       <field name="allwords" type="text_ansj" indexed="true" stored="false" multiValued="true"/>
    
       
       
       <copyField source="Cat" dest="allwords"/>
       <copyField source="Title" dest="allwords"/>
       <copyField source="Color" dest="allwords"/>
       <copyField source="Input_str" dest="allwords"/>
       <copyField source="Size" dest="allwords"/>

    9、配置好中文分词器

      参照 http://www.cnblogs.com/luxh/p/5017336.html

    10、启动tomcat

    11、导入数据

    12、如果日志中有SolrResourceLoader Can't find (or read) directory to add to classloader: ../../../contrib/extraction/lib等找不到库的警告,

      主要是找不到solr安装目录中的solr-5.3.1/contrib和  solr-5.3.1/dist.   则在solrconfig.xml中指定这两个目录的路径即可

      <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*.jar" />
    
      <lib dir="${solr.install.dir:../../../..}/contrib/extraction/lib" regex=".*.jar" />
      <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-cell-d.*.jar" />
    
      <lib dir="${solr.install.dir:../../../..}/contrib/clustering/lib/" regex=".*.jar" />
      <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-clustering-d.*.jar" />
    
      <lib dir="${solr.install.dir:../../../..}/contrib/langid/lib/" regex=".*.jar" />
      <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-langid-d.*.jar" />
    
      <lib dir="${solr.install.dir:../../../..}/contrib/velocity/lib" regex=".*.jar" />
      <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-velocity-d.*.jar" />
    
    替换为:
      <lib dir="/luxh/solr/solr_home/dist/" regex="solr-dataimporthandler-.*.jar" />
    
      <lib dir="/luxh/solr/solr_home/contrib/extraction/lib" regex=".*.jar" />
      <lib dir="/luxh/solr/solr_home/dist/" regex="solr-cell-d.*.jar" />
    
      <lib dir="/luxh/solr/solr_home/contrib/clustering/lib/" regex=".*.jar" />
      <lib dir="/luxh/solr/solr_home/dist/" regex="solr-clustering-d.*.jar" />
    
      <lib dir="/luxh/solr/solr_home/contrib/langid/lib/" regex=".*.jar" />
      <lib dir="/luxh/solr/solr_home/dist/" regex="solr-langid-d.*.jar" />
    
      <lib dir="/luxh/solr/solr_home/contrib/velocity/lib" regex=".*.jar" />
      <lib dir="/luxh/solr/solr_home/dist/" regex="solr-velocity-d.*.jar" />
  • 相关阅读:
    【BZOJ】【3004】吊灯
    【BZOJ】【3653】谈笑风生
    【BZOJ】【2500】幸福的道路
    【BZOJ】【3612】【HEOI 2014】平衡
    【BZOJ】【1485】【HNOI2009】有趣的数列
    【BZOJ】【1293】【SCOI2009】生日礼物
    【BZOJ】【1055】【HAOI2008】玩具取名
    【BZOJ】【1053】【HAOI2007】反素数ant
    【BZOJ】【1052】【HAOI2007】覆盖问题
    【BZOJ】【1050】【HAOI2006】旅行comf
  • 原文地址:https://www.cnblogs.com/luxh/p/5021396.html
Copyright © 2011-2022 走看看