|
| 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 | +} |
0 commit comments