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
package com.gc.solr;
import java.util.List;
/**
*
* @author gc
*
* @param
*/
public interface CommonRepository {
void add(T entity);
void addList(List list);
List 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
*/
public class CommonRepositoryImpl implements CommonRepository {
private Class clazz;
@SuppressWarnings("unchecked")
public CommonRepositoryImpl() { // 通过构造方法完成赋值
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
clazz = (Class) 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 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 byId = (Optional) 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 getList(String query, Boolean flag) throws Exception {
List list = new ArrayList();
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 {
}
package com.gc.solr;
import org.springframework.stereotype.Service;
import com.gc.doc.NewDoc;
@Service("newDocSolr")
public class NewDocSolrImpl extends CommonRepositoryImpl 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 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 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 importDataToSolr(){
InputStream in =null;
BufferedReader br=null;
List 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>() {});
}
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注册中心,网关模块的开发