1. 程式人生 > >springboot專案練習三 配置solr欄位和配置檔案

springboot專案練習三 配置solr欄位和配置檔案

  1. 編寫solr的配置類完成solrClient的注入
  2. 編寫操作solr的常用操作封裝CRUD操作
  3. 編寫controller類完成匯入資料的程式入口

1  新建solrContext.java檔案

package com.gc.config;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;



@Configuration
@EnableSolrRepositories(schemaCreationSupport = true,basePackages="com.gc.entity.solr")
public class SolrContext {
	
	@Value("${spring.data.solr.host}")//從配置application.properties中讀取solr的地址
	private String host;
	 @SuppressWarnings("deprecation")
	@Bean
	  public SolrClient solrClient() {
	    return new HttpSolrClient(host);//生成solrClient 交給spring容器進行管理
	  }
	
}

2 新建介面類CommonRepository<T>

package com.gc.solr;

import java.util.List;

/**
 * 
 * @author gc
 *
 * @param <T>
 */
public interface CommonRepository<T> {
	void add(T entity);

	void addList(List<T> list);

	List<T> getList(String query, Boolean flag) throws Exception;

	T getById(String id);

	int deleteById(String id);

	int deleteByString(String query) throws Exception;

	int deletAll() throws Exception;
}

3 完成介面的實現

package com.gc.solr;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest.METHOD;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.solr.core.SolrOperations;

/**
 * 
 * @author gc
 *
 * @param <T>
 */
public class CommonRepositoryImpl<T> implements CommonRepository<T> {
	private Class<T> clazz;

	@SuppressWarnings("unchecked")
	public CommonRepositoryImpl() { // 通過構造方法完成賦值
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		clazz = (Class<T>) pt.getActualTypeArguments()[0];
	}

	private SolrOperations solrTemp;
	@Value("${spring.data.solr.collection}")
	private String collection;

	@Autowired
	public void setOperations(SolrOperations operations) {
		this.solrTemp = operations;
	}

	@Override
	public void add(T entity) {
		solrTemp.saveBean(collection, entity);
		solrTemp.commit(collection);

	}

	@Override
	public void addList(List<T> list) {
		solrTemp.saveBeans(collection, list);
		solrTemp.commit(collection);
	}

	@Override
	public int deleteById(String id) {
		int status = solrTemp.deleteByIds(collection, id).getStatus();
		solrTemp.commit(collection);
		return status;
	}

	@Override
	public int deleteByString(String query) throws Exception {
		UpdateResponse status = solrTemp.getSolrClient().deleteByQuery(collection, query);
		solrTemp.commit(collection);
		return status.getStatus();
	}

	@Override
	public T getById(String id) {
		@SuppressWarnings("unchecked")
		Optional<T> byId = (Optional<T>) solrTemp.getById(collection, id, clazz);
		return byId != null ? byId.get() : null;
	}

	@Override
	public int deletAll() throws Exception {
		int code = solrTemp.getSolrClient().deleteByQuery(collection, "*:*").getStatus();
		solrTemp.commit(collection);
		return code;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> getList(String query, Boolean flag) throws Exception {
		List<T> list = new ArrayList<T>();
		SolrQuery param = new SolrQuery();
		param.setQuery(query);
		QueryResponse response = solrTemp.getSolrClient().query(collection, param, METHOD.GET);
		int status = response.getStatus();
		if (status == 0) {
			if (flag) {
				SolrDocumentList results = response.getResults();
				for (SolrDocument solrDocument : results) {
					list.add((T) solrDocument);
				}
			} else {
				list = response.getBeans(clazz);
			}
		}
		return list;

	}
}

4 NewDocSolr 和NewDocSolrServiceImpl

package com.gc.solr;

import com.gc.doc.NewDoc;

public interface NewDocSolr extends CommonRepository<NewDoc> {

}
package com.gc.solr;

import org.springframework.stereotype.Service;

import com.gc.doc.NewDoc;
@Service("newDocSolr")
public class NewDocSolrImpl extends CommonRepositoryImpl<NewDoc> implements NewDocSolr {

}

  5 編寫controller類

package com.gc.controller;

import java.util.List;

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

import com.gc.doc.NewDoc;
import com.gc.solr.NewDocSolr;
import com.gc.utils.JsonUtils;

/**
 * 新聞
 * @author jiji
 *
 */
@RequestMapping("/news")
@Controller
public class NewController {
	/**
	 * 注入solr
	 */
	@Autowired
	private NewDocSolr newDocSolr;
	
	/**
	 * 匯入json資料
	 */
	@RequestMapping("import")
	@ResponseBody
	public Object importNewsData(){
		List<NewDoc> newDocList = JsonUtils.importDataToSolr();
		newDocSolr.addList(newDocList);
		return "ok";
	}
	/**
	 * 刪除所有的新聞
	 * @return
	 */
	@RequestMapping("del")
	@ResponseBody
	public Object delNewsData(){
		int count =0;
		try {
			count = newDocSolr.deleteByString("source_news:*");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return count;
	}
	/**
	 * list 返回所有的新聞資料
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Object listNewData(){
		List<NewDoc> list =null;
		try {
			list = newDocSolr.getList("source_news:*",true);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return list;
	}
	/**
	 * 根據id查詢
	 * @param id
	 * @return
	 */
	@RequestMapping("getOne/{id}")
	@ResponseBody
	public Object getOne(@PathVariable("id") String id){
		 NewDoc news = null;
		try {
			news = newDocSolr.getById(id);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return news;
	}
}
http://localhost:8090/news/import  //資料匯入入口
http://localhost:8090/news/getOne/1546672567730437

http://localhost:8090/news/list // 資料列表

http://localhost:8090/news/del // 刪除所有資料介面

 初步的介面調通了,除錯介面中遺漏了一個重要的問題:

1 在建立solr物件時忘記了id欄位 後面補加上了

@Field(value="id")
	private String id;

2 在json檔案解析時將id欄位設定有效值  可以採用UUID生成唯一的ID字串  我採用的是本機時間加上4位隨機數 

for (NewDoc newDoc : list) {
				newDoc.setId(String.valueOf(System.currentTimeMillis())+new Random().nextInt(1000));
				System.out.println(newDoc.toString());
			}

完成的兩個檔案如下:

package com.gc.doc;

import java.util.UUID;

import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;

@SolrDocument
public class NewDoc {
	@Field(value="id")
	private String id;
	
	public String getId() {
		return UUID.randomUUID().toString();
	}

	public void setId(String id) {
		this.id = id;
	}

	@Field(value="votecount")
	private String votecount;
	
	@Field(value="docid")
	private String docid;
	
	@Field(value="lmodify")
	private String lmodify;
	@Field(value="url_3w")
	private String url_3w;
	
	@Field(value="source_news")
	private String source;
	
	@Field(value="postid")
	private String postid;
	
	@Field(value="priority")
	private String priority;
	
	@Field(value="title_news")
	private String title;
	
	@Field(value="mtime")
	private String mtime;
	
	@Field(value="mtime")
	private String url;
	
	@Field(value="replyCount_news")
	private String replyCount;
	

	@Field(value="ptime")
	private String ptime;
	
	@Field(value="title_news")
	private String ltitle;
	
	@Field(value="subtitle")
	private String subtitle;
	
	@Field(value="digest")
	private String digest;
	
	@Field(value="boardid")
	private String boardid;
	
	@Field(value="imgsrc")
	private String imgsrc;
	
	@Override
	public String toString() {
		return "NewDoc [votecount=" + votecount + ", docid=" + docid + ", lmodify=" + lmodify + ", url_3w=" + url_3w
				+ ", source=" + source + ", postid=" + postid + ", priority=" + priority + ", title=" + title
				+ ", mtime=" + mtime + ", url=" + url + ", replyCount=" + replyCount + ", ptime=" + ptime + ", ltitle="
				+ ltitle + ", subtitle=" + subtitle + ", digest=" + digest + ", boardid=" + boardid + ", imgsrc="
				+ imgsrc + "]";
	}

	public String getVotecount() {
		return votecount;
	}

	public void setVotecount(String votecount) {
		this.votecount = votecount;
	}

	public String getDocid() {
		return docid;
	}

	public void setDocid(String docid) {
		this.docid = docid;
	}

	public String getLmodify() {
		return lmodify;
	}

	public void setLmodify(String lmodify) {
		this.lmodify = lmodify;
	}

	public String getUrl_3w() {
		return url_3w;
	}

	public void setUrl_3w(String url_3w) {
		this.url_3w = url_3w;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getPostid() {
		return postid;
	}

	public void setPostid(String postid) {
		this.postid = postid;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) {
		this.priority = priority;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMtime() {
		return mtime;
	}

	public void setMtime(String mtime) {
		this.mtime = mtime;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getReplyCount() {
		return replyCount;
	}

	public void setReplyCount(String replyCount) {
		this.replyCount = replyCount;
	}

	public String getPtime() {
		return ptime;
	}

	public void setPtime(String ptime) {
		this.ptime = ptime;
	}

	public String getLtitle() {
		return ltitle;
	}

	public void setLtitle(String ltitle) {
		this.ltitle = ltitle;
	}

	public String getSubtitle() {
		return subtitle;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public String getDigest() {
		return digest;
	}

	public void setDigest(String digest) {
		this.digest = digest;
	}

	public String getBoardid() {
		return boardid;
	}

	public void setBoardid(String boardid) {
		this.boardid = boardid;
	}

	public String getImgsrc() {
		return imgsrc;
	}

	public void setImgsrc(String imgsrc) {
		this.imgsrc = imgsrc;
	}

	
}
package com.gc.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Random;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gc.doc.NewDoc;
/**
 * 將json陣列匯入solr
 * @author gc
 *
 */
public class JsonUtils {
public static  List<NewDoc>  importDataToSolr(){
	InputStream in =null;
	BufferedReader br=null;
	List<NewDoc> list=null;
	try {
		in = new FileInputStream(new File("F:\\springboot\\springboot_solr\\src\\main\\resources\\news.json"));
		 br = new BufferedReader(new InputStreamReader(in));
		String line;
		StringBuffer strb = new StringBuffer();
		while ((line = br.readLine()) != null) {
			strb.append(line);
		}
		ObjectMapper mapper  = new  ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		JsonNode jsonNode = mapper.readTree(strb.toString());
		JsonNode root = jsonNode.get("T1348647853363");
		
		if(root.isArray()){
			 list=	mapper.readValue(root.toString(), new TypeReference<List<NewDoc>>() {});
		}
		if(list!=null){
			for (NewDoc newDoc : list) {
				newDoc.setId(String.valueOf(System.currentTimeMillis())+new Random().nextInt(1000));
				System.out.println(newDoc.toString());
			}
		}
	} catch (Exception e) {
		
		e.printStackTrace();
	}finally {
			try {
			if(br!=null){
					br.close();
			}
			if(in!=null){
				in.close();
			}
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	return list;
	
}
public static void main(String[] args) {
	importDataToSolr();
}
}

 簡單的介面除錯就完成了,在下面會使用前端ligerUi外掛來完成頁面互動部分。

剩餘:頁面互動、靜態化

加入多執行緒爬蟲來增加資料的豐富度,現在資料量還是太少了。

期望:完成使用者模組的服務+redis+springcloud註冊中心,閘道器模組的開發