1. 程式人生 > >使用solr批量匯入mysql資料庫,以及Unable to read: dataimport.properties等坑

使用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