天天看點

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

一、需求

建立一個taotao-search工程, 做為一個服務的工程,     taotao-search-web 是一個前端通路的工程.前台進行通路的時候,直接通路的是taotao-search-web這個工程.

導入資料庫資料的時候,使用的是背景操作的方式導入的資料,今天使用solrJ的方式把資料庫裡的資料導入到solr索引庫中,查詢的時候,直接從solr索引庫中查詢出資料.

二、步驟

1. 建立taotao-search工程

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫
淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

修改pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.taotao</groupId>
	<artifactId>taotao-search</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<dependencies>
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-manager-dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<modules>
		<module>taotao-search-interface</module>
		<module>taotao-search-service</module>
	</modules>
	
	<build>
		<!-- 配置插件 -->
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8084</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
           

2. 建立taotao-search-interface工程

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

3. 建立taotao-search-service

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

3.1 修改pom檔案

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.taotao</groupId>
    <artifactId>taotao-search</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>taotao-search-service</artifactId>
  <packaging>war</packaging>
  <dependencies>
  
  	<dependency>
  		<groupId>com.taotao</groupId>
	    <artifactId>taotao-search-interface</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
  	</dependency>
  
  <!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jms</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
			</dependency>
  		<!-- dubbo相關 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>dubbo</artifactId>
				<exclusions>
					<exclusion>
						<artifactId>spring</artifactId>
						<groupId>org.springframework</groupId>
					</exclusion>
					<exclusion>
						<artifactId>netty</artifactId>
						<groupId>org.jboss.netty</groupId>
					</exclusion>
				</exclusions>
			</dependency>
			<dependency>
				<groupId>org.apache.zookeeper</groupId>
				<artifactId>zookeeper</artifactId>
			</dependency>
			<dependency>
				<groupId>com.github.sgroschupf</groupId>
				<artifactId>zkclient</artifactId>
			</dependency>
			<!-- solr用戶端 -->
			<dependency>
				<groupId>org.apache.solr</groupId>
				<artifactId>solr-solrj</artifactId>
			</dependency>
  </dependencies>
</project>
           

3.2 修改web.xml檔案

3.2.1 建立WEB-INF

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

3.2.2 建立web.xml

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

3.2.3 修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="taotao" version="2.5">
	<display-name>taotao-search</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- 初始化spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>
           

4. 參考spring的配置

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

4.1 修改applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 配置service掃描包 -->
	<context:component-scan base-package="com.taotao.search.service"/>
	
	<!-- 生産者,在這釋出一個dubbo服務,相當于把service釋出到注冊中心去. -->	
	<!-- 使用dubbo釋出服務 -->
	<!-- 提供方應用資訊,用于計算依賴關系 -->
	<dubbo:application name="taotao-search" />
	<dubbo:registry protocol="zookeeper" address="172.18.34.94:2181" />
	<!-- 用dubbo協定在20880端口暴露服務 -->
	<dubbo:protocol name="dubbo" port="20882" />
	
	<!-- 聲明需要暴露的服務接口 -->
    <!-- 留下一個做參考 -->                
	<dubbo:service inter ref="itemServiceImpl" />
	
</beans>
           

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

4.2 修改applicationContext-tranc.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 資料源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 傳播行為 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.taotao.search.service.*.*(..))" />
	</aop:config>
	
</beans>
           
淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

4.3 applicationContext-dao.xml 暫時不做修改

三、代碼的編寫

1. 需求分析

頁面查詢的時候,有可能根據哪些字段查詢?

頁面上顯示哪些内容?哪些字段

商品id,名稱,賣點,價格,詳情,圖檔,分類資訊

資料來源幾張表:

三張表: 商品表,商品分類表,商品詳情表

SELECT

a.id,

  a.title,

  a.sell_point,

  a.price,

  a.image,

  t.`name`,

  c.item_desc

FROM

tb_item a

LEFT JOIN tb_item_cat t ON a.cid = t.id

LEFT JOIN tb_item_desc c ON a.id = c.item_id where a.`status`=1

2. 建立一個封裝類SearchItem.java

package com.taotao.utils;

import java.io.Serializable;
/**
 * 封裝的要查詢的字段
 * @author fengjinzhu
 *
 */
public class SearchItem implements Serializable{
	private String id;
	private String title;
	private String sell_point;
	private long price;
	private String image;
	private String catName;
	private String item_desc;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSell_point() {
		return sell_point;
	}
	public void setSell_point(String sell_point) {
		this.sell_point = sell_point;
	}
	public long getPrice() {
		return price;
	}
	public void setPrice(long price) {
		this.price = price;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public String getCatName() {
		return catName;
	}
	public void setCatName(String catName) {
		this.catName = catName;
	}
	public String getItem_desc() {
		return item_desc;
	}
	public void setItem_desc(String item_desc) {
		this.item_desc = item_desc;
	}
	
	
	
}
           
淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

3. 建立dao層:因為這個dao隻是自己用,是以單獨建立

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

SearchItemMapper.java

package com.taotao.search.mapper;

import java.util.List;

import com.taotao.utils.SearchItem;

public interface SearchItemMapper {
	//查詢出的是多條資料
	public List<SearchItem> getSearchItemList();
}
           

SearchItemMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.taotao.search.mapper.SearchItemMapper" >
  
  <!-- 查詢所有 -->
  <select id="getSearchItemList" resultType="com.taotao.utils.SearchItem">
		SELECT
		  a.id,
		  a.title,
		  a.sell_point,
		  a.price,
		  a.image,
		  t.name as catName,
		  c.item_desc
	  FROM
			tb_item a
		LEFT JOIN tb_item_cat t ON a.cid = t.id
		LEFT JOIN tb_item_desc c ON a.id = c.item_id where a.status=1  	
  </select>
</mapper>



           

4. 建立applicationContext-solr.xml,加載solrserver

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫
淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 配置SolrServer對象 -->
	<!-- 單機版 -->
	<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
		<constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg>
	</bean>

</beans>
           

5. resource.properties

#solr服務位址
SOLR.SERVER.URL=http://172.18.34.111:8080/solr
           

6. 加載resource.properties

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

7. 釋出服務

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

SearchItemService.java

package com.taotao.search.service;

import com.taotao.utils.TaotaoResult;

public interface SearchItemService {

	public TaotaoResult importSearchItem();
}
           

SearchItemServiceImpl.java

package com.taotao.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.search.mapper.SearchItemMapper;
import com.taotao.search.service.SearchItemService;
import com.taotao.utils.ExceptionUtil;
import com.taotao.utils.SearchItem;
import com.taotao.utils.TaotaoResult;

@Service
public class SearchItemServiceImpl implements SearchItemService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SearchItemServiceImpl.class);
	
	@Autowired
	private SearchItemMapper searchItemMapper;
	@Autowired
	private SolrServer solrServer;
	
	@Override
	public TaotaoResult importSearchItem() {
		try {
			//查詢到資料
			List<SearchItem> list = searchItemMapper.getSearchItemList();
			//啟動Tomcat的時候,讓spring去建立solrserver執行個體
			//把資料導入到索引庫
			SolrInputDocument document = new SolrInputDocument();
			for (SearchItem searchItem : list) {
				//根據索引庫域的定義,将字段寫入到索引庫
				document.setField("id", searchItem.getId());
				document.setField("item_title", searchItem.getTitle());
				document.setField("item_category_name", searchItem.getCatName());
				document.setField("item_image", searchItem.getImage());
				document.setField("item_desc", searchItem.getItem_desc());
				document.setField("item_price", searchItem.getPrice());
				document.setField("item_sell_point", searchItem.getSell_point());
				
				solrServer.add(document);
			}
			solrServer.commit();
			return TaotaoResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("錯誤資訊展示:", e);
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	
	}

}
           

8. 為了友善檢視,是以我們在taotao-manager-web自定義一個頁面

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

item-importIndex.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<link href="/js/kindeditor-4.1.10/themes/default/default.css" target="_blank" rel="external nofollow"  type="text/css" rel="stylesheet">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
	function searchImport(){
		$.getJSON("searchItemImportAll" ,function(data){
			  if(data.status == 200){
				  $.messager.alert("提示", "資料導入成功")
			  }
		});
	}
</script>
<body>
	<a href="#" target="_blank" rel="external nofollow"  class="easyui-linkbutton" onclick="searchImport()">導入到索引庫</a>
</body>
</html>
           

在index.jsp中添加(友善在首頁展示)

<li>
         		<span>索引庫管理</span>
         		<ul>
	         		<li data-options="attributes:{'url':'item-importIndex'}">導入索引庫</li>
	         	</ul>
         	</li>
           

9. 建立controller

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫
package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.search.service.SearchItemService;
import com.taotao.utils.TaotaoResult;

@Controller
public class SearchItemController {

	@Autowired
	private SearchItemService searchItemService;
	
	/**
	 * 導入資料到索引庫
	 * @return
	 */
	@RequestMapping("/searchItemImportAll")
	@ResponseBody
	public TaotaoResult searchItemImport(){
		TaotaoResult result = searchItemService.importSearchItem();
		return result;
	}
}
           

10. 修改springmvc.xml的配置,調用服務

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

11. 錯誤更改

因為我們在applicationContenx-dao.xml中 沒有配置,是以我們在taotao-search-service中的dao掃描不到,是以如圖修改

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

12. 啟動項目

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

報錯了:

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

 解決:在taotao-search-service工程的pom中添加:

<!-- 如果不添加此節點mybatis的mapper.xml檔案都會被漏掉。 -->
	<build>
		<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
	</build>
           
淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

測試:

淘淘商城23_在Linux上的操作_solrJ用戶端_01資料導入索引庫一、需求二、步驟三、代碼的編寫

繼續閱讀