Skip to content

Commit 6864e27

Browse files
committed
集成了es7.0版本
1 parent 9b7083a commit 6864e27

File tree

13 files changed

+458
-7
lines changed

13 files changed

+458
-7
lines changed

pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@
219219
<artifactId>wxpay-sdk</artifactId>
220220
<version>0.0.3</version>
221221
</dependency>
222+
<!--elasticsearch 引擎依赖-->
223+
<dependency>
224+
<groupId>org.elasticsearch.client</groupId>
225+
<artifactId>elasticsearch-rest-high-level-client</artifactId>
226+
<version>7.0.0</version>
227+
</dependency>
228+
<dependency>
229+
<groupId>org.elasticsearch</groupId>
230+
<artifactId>elasticsearch</artifactId>
231+
<version>7.0.0</version>
232+
</dependency>
222233
</dependencies>
223234

224235
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.baozi.configurer;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.baozi.entity.coupon.LpzTaobaoCoupon;
5+
import com.baozi.service.coupon.ILpzTaobaoCouponService;
6+
import com.baozi.util.LogUtils;
7+
import com.baozi.util.spring.SpringContextUtil;
8+
import org.apache.http.HttpHost;
9+
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
10+
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
11+
import org.elasticsearch.action.bulk.BulkRequest;
12+
import org.elasticsearch.action.bulk.BulkResponse;
13+
import org.elasticsearch.action.index.IndexRequest;
14+
import org.elasticsearch.client.IndicesClient;
15+
import org.elasticsearch.client.RequestOptions;
16+
import org.elasticsearch.client.RestClient;
17+
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
18+
import org.elasticsearch.client.RestHighLevelClient;
19+
import org.elasticsearch.common.settings.Settings;
20+
import org.elasticsearch.common.xcontent.XContentBuilder;
21+
import org.elasticsearch.common.xcontent.XContentFactory;
22+
import org.elasticsearch.common.xcontent.XContentType;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
import java.util.Date;
27+
import java.util.HashMap;
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
/**
32+
* Copyright: 张文君
33+
*
34+
* @author: zhangwenjun
35+
* @version: V1.0
36+
* @Date: 2019-05-13 14:45
37+
*/
38+
@Configuration
39+
public class ElasticsearchConfig {
40+
41+
private static RestHighLevelClient client = client();
42+
43+
/*初始化api客户端*/
44+
@Bean
45+
private static RestHighLevelClient client() {
46+
RestHighLevelClient client = new RestHighLevelClient(
47+
RestClient.builder(
48+
new HttpHost("123.56.219.123", 9200, "http")));
49+
LogUtils.logInfo("********************************************");
50+
LogUtils.logInfo(client.toString());
51+
LogUtils.logInfo("********************************************");
52+
return client;
53+
}
54+
55+
/*生成mapping*/
56+
private static XContentBuilder genMapping() throws Exception{
57+
XContentBuilder mapping = XContentFactory.jsonBuilder()
58+
.startObject()
59+
.startObject("properties")
60+
.startObject("id").field("type","text").endObject()
61+
.startObject("goodsName").field("type","text").field("analyzer", "ik_smart").field("search_analyzer", "ik_smart").endObject()
62+
.startObject("goodsRemark").field("type","text").field("analyzer", "ik_smart").field("search_analyzer", "ik_smart").endObject()
63+
.startObject("onlinePrice").field("type","double").endObject()
64+
.startObject("couponPrice").field("type","double").endObject()
65+
.startObject("sellCount").field("type","integer").endObject()
66+
.startObject("update").field("type","text").field("analyzer", "ik_smart").field("search_analyzer", "ik_smart").endObject()
67+
.startObject("createDate").field("type","date").endObject()
68+
.startObject("linkUrl").field("type","text").endObject()
69+
.endObject()
70+
.endObject();
71+
return mapping;
72+
}
73+
74+
/*创建索引*/
75+
public static boolean createIndex(String indexName,String type) {
76+
try {
77+
Settings settings = Settings.builder().put("number_of_shards", 3).put("number_of_replicas", 3).build();
78+
CreateIndexRequest request = new CreateIndexRequest(indexName).settings(settings).mapping(type,genMapping());
79+
IndicesClient indices = client.indices();
80+
CreateIndexResponse createIndexResponse = indices.create(request,RequestOptions.DEFAULT);
81+
boolean acknowledged = createIndexResponse.isAcknowledged();
82+
return acknowledged;
83+
}catch (Exception e){
84+
LogUtils.logError("创建索引发生异常",e);
85+
return false;
86+
}
87+
}
88+
89+
/*删除索引*/
90+
public static boolean deleteIndex(String indexName){
91+
try {
92+
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
93+
client.indices().delete(request,RequestOptions.DEFAULT);
94+
return true;
95+
}catch (Exception e){
96+
LogUtils.logError("删除索引发生异常",e);
97+
return false;
98+
}
99+
}
100+
101+
/*插入数据*/
102+
public static boolean batchImportData(String indexName){
103+
try {
104+
LogUtils.logInfo("开始向Elasticsearch灌数据");
105+
Long sendTime = new Date().getTime();
106+
Long endTime = new Long("0");
107+
ILpzTaobaoCouponService lLpzTaobaoCouponService = SpringContextUtil.getBean(ILpzTaobaoCouponService.class);
108+
Map<String,Object> paramMap = new HashMap<>();
109+
int count = 0;
110+
int page = 1;
111+
paramMap.put("page",page);
112+
paramMap.put("limit",1000);
113+
BulkRequest request = new BulkRequest();
114+
while (page<88){
115+
paramMap.put("page",page);
116+
List<LpzTaobaoCoupon> allList = lLpzTaobaoCouponService.selectLpzTaobaoCouponAll(paramMap).getList();
117+
if (allList.isEmpty())
118+
break;
119+
for (int i=0;i<allList.size();i++) {
120+
LpzTaobaoCoupon lpzTaobaoCoupon = allList.get(i);
121+
Map<String,Object> current = new HashMap<>();
122+
current.put("id",lpzTaobaoCoupon.getId());
123+
current.put("goodsName",lpzTaobaoCoupon.getGoodsname());
124+
current.put("goodsRemark",lpzTaobaoCoupon.getGoodsremark());
125+
current.put("onlinePrice",lpzTaobaoCoupon.getOnlineprice());
126+
current.put("couponPrice",lpzTaobaoCoupon.getCouponprice());
127+
current.put("sellCount",lpzTaobaoCoupon.getSellcount());
128+
current.put("update",lpzTaobaoCoupon.getUpdate());
129+
current.put("createDate",lpzTaobaoCoupon.getCreatedate());
130+
current.put("linkUrl",lpzTaobaoCoupon.getLinkurl());
131+
request.add(new IndexRequest(indexName).source(current,XContentType.JSON));
132+
BulkResponse bulkResponse = client.bulk(request,RequestOptions.DEFAULT);
133+
System.out.println(bulkResponse);
134+
if (count % 1000 == 0){
135+
page++;
136+
}
137+
count++;
138+
}
139+
}
140+
endTime = new Date().getTime();
141+
LogUtils.logInfo("数据灌入完毕,耗时["+(endTime-sendTime)+"]毫秒");
142+
return true;
143+
} catch ( Exception e ){
144+
LogUtils.logError("灌数据发生异常",e);
145+
return false;
146+
}
147+
}
148+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baozi.controller.coupon;
2+
3+
import com.baozi.controller.base.ResponseBase;
4+
import com.baozi.service.coupon.ILpzTaobaoCouponService;
5+
import com.baozi.util.LoggerUtils;
6+
import com.baozi.util.WebUtil;
7+
import io.swagger.annotations.Api;
8+
import io.swagger.annotations.ApiOperation;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
import javax.servlet.http.HttpServletRequest;
15+
import java.util.Map;
16+
17+
/**
18+
* <p>
19+
* 前端控制器
20+
* </p>
21+
*
22+
* @author wenjunzhangp
23+
* @since 2019-05-14
24+
*/
25+
@Controller
26+
@RequestMapping("coupon")
27+
@Api(tags = "优惠券controller", description = "优惠券controller")
28+
public class LpzTaobaoCouponController {
29+
30+
@Autowired
31+
ILpzTaobaoCouponService lpzTaobaoCouponService;
32+
33+
/**
34+
* 优惠券分页列表查询
35+
* @return
36+
*/
37+
@RequestMapping("query/coupon/couponList")
38+
@ResponseBody
39+
@ApiOperation(value="优惠券分页查询", notes="优惠券分页查询")
40+
public Map<String,Object> queryCoupon(HttpServletRequest request){
41+
try {
42+
Map<String,Object> paramMap = WebUtil.genRequestMapSingle(request);
43+
return ResponseBase.setResultMapOkByPage(lpzTaobaoCouponService.selectCouponList(paramMap));
44+
} catch ( Exception e ) {
45+
LoggerUtils.logError("优惠券分页查询出现异常",e);
46+
return ResponseBase.setResultMapError(e);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baozi.dao.coupon;
2+
3+
4+
import com.baomidou.mybatisplus.mapper.BaseMapper;
5+
import com.baozi.entity.coupon.LpzTaobaoCoupon;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* <p>
12+
* Mapper 接口
13+
* </p>
14+
*
15+
* @author wenjunzhangp
16+
* @since 2019-05-14
17+
*/
18+
public interface LpzTaobaoCouponMapper extends BaseMapper<LpzTaobaoCoupon> {
19+
20+
List<LpzTaobaoCoupon> selectCouponList(Map<String,Object> paramMap);
21+
22+
List<LpzTaobaoCoupon> selectLpzTaobaoCouponAll(Map<String,Object> paramMap);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baozi.entity.coupon;
2+
3+
import com.baomidou.mybatisplus.activerecord.Model;
4+
import java.math.BigDecimal;
5+
import java.util.Date;
6+
import com.baomidou.mybatisplus.annotations.TableName;
7+
import lombok.Data;
8+
9+
import java.io.Serializable;
10+
11+
12+
/**
13+
* <p>
14+
*
15+
* </p>
16+
*
17+
* @author wenjunzhangp
18+
* @since 2019-05-14
19+
*/
20+
@TableName("lpz_taobao_coupon")
21+
@Data
22+
public class LpzTaobaoCoupon extends Model<LpzTaobaoCoupon> {
23+
24+
private static final long serialVersionUID = 1L;
25+
26+
/**
27+
* 主键ID
28+
*/
29+
private String id;
30+
/**
31+
* 商品名称
32+
*/
33+
private String goodsname;
34+
/**
35+
* 商品卖点
36+
*/
37+
private String goodsremark;
38+
/**
39+
* 在售价
40+
*/
41+
private BigDecimal onlineprice;
42+
/**
43+
* 折后价
44+
*/
45+
private BigDecimal couponprice;
46+
/**
47+
* 累计销量
48+
*/
49+
private Integer sellcount;
50+
/**
51+
* 上新时间
52+
*/
53+
private String update;
54+
/**
55+
* 卷入库更新时间
56+
*/
57+
private Date createdate;
58+
/**
59+
* 领劵地址
60+
*/
61+
private String linkurl;
62+
63+
@Override
64+
protected Serializable pkVal() {
65+
return this.id;
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baozi.service.coupon;
2+
3+
import com.baomidou.mybatisplus.service.IService;
4+
import com.baozi.entity.coupon.LpzTaobaoCoupon;
5+
import com.github.pagehelper.PageInfo;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* <p>
12+
* 服务类
13+
* </p>
14+
*
15+
* @author wenjunzhangp
16+
* @since 2019-05-14
17+
*/
18+
public interface ILpzTaobaoCouponService extends IService<LpzTaobaoCoupon> {
19+
20+
/**
21+
* 优惠券列表分页查询
22+
* @param paramMap
23+
* @return
24+
*/
25+
PageInfo<LpzTaobaoCoupon> selectCouponList(Map<String,Object> paramMap);
26+
27+
/**
28+
* 查询所有优惠券数据灌入es中
29+
* @return
30+
*/
31+
PageInfo<LpzTaobaoCoupon> selectLpzTaobaoCouponAll(Map<String,Object> paramMap);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baozi.service.impl.coupon;
2+
3+
import com.baozi.dao.coupon.LpzTaobaoCouponMapper;
4+
import com.baozi.entity.coupon.LpzTaobaoCoupon;
5+
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
6+
import com.baozi.service.coupon.ILpzTaobaoCouponService;
7+
import com.github.pagehelper.PageHelper;
8+
import com.github.pagehelper.PageInfo;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
/**
16+
* <p>
17+
* 服务实现类
18+
* </p>
19+
*
20+
* @author wenjunzhangp
21+
* @since 2019-05-14
22+
*/
23+
@Service
24+
public class LpzTaobaoCouponServiceImpl extends ServiceImpl<LpzTaobaoCouponMapper, LpzTaobaoCoupon> implements ILpzTaobaoCouponService {
25+
26+
@Autowired(required = false)
27+
LpzTaobaoCouponMapper lpzTaobaoCouponMapper;
28+
29+
/**
30+
* 优惠券列表分页查询
31+
*
32+
* @param paramMap
33+
* @return
34+
*/
35+
@Override
36+
public PageInfo<LpzTaobaoCoupon> selectCouponList(Map<String, Object> paramMap) {
37+
PageHelper.startPage(Integer.valueOf(paramMap.get("page").toString()),Integer.valueOf(paramMap.get("limit").toString()),true);
38+
List<LpzTaobaoCoupon> dataList = lpzTaobaoCouponMapper.selectCouponList(paramMap);
39+
return new PageInfo<LpzTaobaoCoupon>(dataList);
40+
}
41+
42+
/**
43+
* 查询所有优惠券数据灌入es中
44+
*
45+
* @return
46+
*/
47+
@Override
48+
public PageInfo<LpzTaobaoCoupon> selectLpzTaobaoCouponAll(Map<String,Object> paramMap) {
49+
PageHelper.startPage(Integer.valueOf(paramMap.get("page").toString()),Integer.valueOf(paramMap.get("limit").toString()),true);
50+
List<LpzTaobaoCoupon> dataList = lpzTaobaoCouponMapper.selectLpzTaobaoCouponAll(paramMap);
51+
return new PageInfo<LpzTaobaoCoupon>(dataList);
52+
}
53+
}

0 commit comments

Comments
 (0)