使用solr批量导入mysql数据库,以及Unable to read: dataimport.properties等坑

折腾了一下午终于成功了!先放一张成功图:

成功把mysql的数据添加进去了,我这里是整合了tomcat9,整合步骤挺麻烦的,百度一大堆!

这里主要介绍批量导入数据,这里有些坑,所以记录一下:

步骤:

第一: 引入需要的jar包

我这里把home目录放外面了,所以核心配置文件引入jar包需要改一下:

在下面这个目录下复制进去需要的jar包,mysql自己找,下面两个可以在solr安装包的dist目录中找到,我这里是7.3.版本:

然后在需要加的core的solrconfig,xml添加引用

第二: 添加配置文件

 在solrconfig.xml中需要引入:

<!-- 配置批量导入处理器 -->  
	<admin>  
		<defaultQuery>*:*</defaultQuery>  
	</admin>  
	<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  
	<lst name="defaults">  
		<str name="config">data-config.xml</str>  <!-- mysql数据配置映射文件 -->
	</lst>  
	</requestHandler>  

  同级目录下新建data-config.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<dataConfig>  
    <!-- 配置数据源 -->  
    <!-- url中最后要加上serverTimezone=UTC否则发送请求的时候会乱码 -->  
    <dataSource driver="com.mysql.jdbc.Driver"   
                url="jdbc:mysql://localhost:3306/solr?characterEncoding=utf-8&serverTimezone=UTC"   
                user="root"   
                password="799827577"/>  
    <document>  
        <!-- query中写SQL语句 -->  
        <entity name="products" query="select pid,name,catalog_name,picture,description,price from products">  
            <!-- column对应数据库中的列名,name为对应的域名(在scheme中没有的话需要配置,即设置业务系统域),  
            这是一个映射关系 -->  
            <field column="pid" name="id"/>  
            <field column="name" name="product_name"/>  
            <field column="price" name="product_price"/>    
            <field column="catalog_name" name="product_catalog_name"/>  
            <field column="picture" name="product_picture"/>  
            <field column="description" name="product_description"/>  
        </entity>  
    </document>  
</dataConfig> 

  

第三: 设置业务域

这里是mysql的表结构

然后最后是对应的配置,在core下面的从目录中:

 虽然现在支持API添加,但是我还是觉得麻烦,直接手动添加重启,毕竟不是企业

<!-- 配置producta表业务域 -->
	<field name="product_name" type="text_ik" indexed="true" stored="true"/>
	<field name="product_price" type="pfloat" indexed="true" stored="true"/>
	<field name="product_catalog_name" type="string" indexed="true" stored="true"/>
	<field name="product_description" type="text_ik" indexed="true" stored="false"/>
	<field name="product_picture" type="string" indexed="false" stored="true"/>
	
	<!-- 配置拷贝域,商品名称和描述组合查询 -->
	<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
	<copyField source="product_name" dest="product_keywords"/>
	<copyField source="product_description" dest="product_keywords"/>

  

data-config.xml配置注意事项:

数据库连接url后面要配置serverTimezone,否则会报错,日志中显示你必须要指定这个

其次如果还有其他信息,因为是xml,所以 & 参数连接符需要转义,xml中&是作为实体存在的

mysql://localhost:3306/solr?characterEncoding=utf-8&amp;serverTimezone=UTC

猜你喜欢

转载自www.cnblogs.com/houzheng/p/10012951.html