mirror of
https://gitee.com/freshday/radar.git
synced 2026-03-22 12:47:16 +08:00
Merge pull request #36 from CatCccC/mongodb
fix:error mongodb collectionName,upgrade dependency
This commit is contained in:
14
pom.xml
14
pom.xml
@@ -14,7 +14,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.7.RELEASE</version>
|
||||
<version>2.2.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.pgmmers</groupId>
|
||||
@@ -30,8 +30,7 @@
|
||||
<mapper.version>4.0.0</mapper.version>
|
||||
<mybatisGenerator.version>1.3.7</mybatisGenerator.version>
|
||||
<mysql.version>5.1.47</mysql.version>
|
||||
<springboot.version>2.1.7.RELEASE</springboot.version>
|
||||
<tomcat.version>8.5.37</tomcat.version>
|
||||
<springboot.version>2.2.5.RELEASE</springboot.version>
|
||||
<tensorflow.version>1.12.0</tensorflow.version>
|
||||
<es.version>7.6.1</es.version>
|
||||
</properties>
|
||||
@@ -113,14 +112,9 @@
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.60</version>
|
||||
<version>1.2.66</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>3.2.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
@@ -208,7 +202,7 @@
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>28.0-jre</version>
|
||||
<version>28.2-jre</version>
|
||||
</dependency>
|
||||
|
||||
<!-- admin 模块独立引用-->
|
||||
|
||||
@@ -25,7 +25,8 @@ elasticsearch:
|
||||
cluster:
|
||||
name: elasticsearch
|
||||
url: http://127.0.0.1:9200
|
||||
|
||||
username: # 不存在就留空
|
||||
password: #
|
||||
sys:
|
||||
conf:
|
||||
app: admin # admin 或者 engine, 根据启动的项目名称进行选择
|
||||
|
||||
@@ -29,7 +29,8 @@ elasticsearch:
|
||||
cluster:
|
||||
name: elasticsearch
|
||||
url: http://127.0.0.1:9200
|
||||
|
||||
username: # 不存在就留空
|
||||
password:
|
||||
sys:
|
||||
conf:
|
||||
app: engine # admin 或者 engine, 根据启动的项目名称进行选择
|
||||
|
||||
@@ -8,28 +8,23 @@ import com.mongodb.client.model.Filters;
|
||||
import com.pgmmers.radar.enums.FieldType;
|
||||
import com.pgmmers.radar.service.engine.AggregateCommand;
|
||||
import com.pgmmers.radar.service.impl.util.MongodbUtil;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AggregateCommandImpl implements AggregateCommand {
|
||||
|
||||
@Value("${spring.data.mongodb.uri}")
|
||||
private String url;
|
||||
|
||||
@Override
|
||||
public long count(String modelId, String searchField, Object searchFieldValue, String refDateName, Date begin,
|
||||
Date end) {
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
Long qty = MongodbUtil.count(tempUrl, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
String collectionName = "entity_" + modelId;
|
||||
Long qty = MongodbUtil.count(collectionName, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
Filters.gte(refDateName, begin.getTime()), Filters.lte(refDateName, end.getTime())));
|
||||
return qty;
|
||||
}
|
||||
@@ -37,8 +32,8 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
@Override
|
||||
public long distinctCount(String modelId, String searchField, Object searchFieldValue, String refDateName,
|
||||
Date begin, Date end, String distinctBy) {
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
Long qty = MongodbUtil.distinctCount(tempUrl, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
String collectionName = "entity_" + modelId;
|
||||
Long qty = MongodbUtil.distinctCount(collectionName, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
Filters.gte(refDateName, begin.getTime()), Filters.lte(refDateName, end.getTime())), distinctBy);
|
||||
return qty;
|
||||
}
|
||||
@@ -47,14 +42,14 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
public BigDecimal sum(String modelId, String searchField, Object searchFieldValue, String refDateName, Date begin,
|
||||
Date end, String funcField) {
|
||||
BigDecimal sum = null;
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
String collectionName = "entity_" + modelId;
|
||||
Document match = new Document("$match", new Document(searchField, searchFieldValue).append(refDateName,
|
||||
new Document("$gte", begin.getTime()).append("$lte", end.getTime())));
|
||||
|
||||
Document group = new Document("$group", new Document("_id", null).append("sum", new Document("$sum", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(tempUrl, pipeline);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
sum = new BigDecimal(doc.get("sum").toString());
|
||||
@@ -68,14 +63,14 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
public BigDecimal average(String modelId, String searchField, Object searchFieldValue, String refDateName,
|
||||
Date begin, Date end, String funcField) {
|
||||
BigDecimal avg = null;
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
String collectionName = "entity_" + modelId;
|
||||
Document match = new Document("$match", new Document(searchField, searchFieldValue).append(refDateName,
|
||||
new Document("$gte", begin.getTime()).append("$lte", end.getTime())));
|
||||
|
||||
Document group = new Document("$group", new Document("_id", null).append("avg", new Document("$avg", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(tempUrl, pipeline);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
avg = new BigDecimal(doc.get("avg").toString());
|
||||
@@ -87,14 +82,14 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
public BigDecimal median(String modelId, String searchField, Object searchFieldValue, String refDateName,
|
||||
Date begin, Date end, String funcField) {
|
||||
BigDecimal avg = null;
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
String collectionName = "entity_" + modelId;
|
||||
Document match = new Document("$match", new Document(searchField, searchFieldValue).append(refDateName,
|
||||
new Document("$gte", begin.getTime()).append("$lte", end.getTime())));
|
||||
|
||||
Document sort = new Document("$sort", new Document(funcField, 1));
|
||||
List<Bson> pipeline = Arrays.asList(match, sort);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(tempUrl, pipeline);
|
||||
List<Document> docList = new ArrayList<Document>();
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
List<Document> docList = new ArrayList<>();
|
||||
it.forEach(new Block<Document>() {
|
||||
|
||||
@Override
|
||||
@@ -130,14 +125,14 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
public BigDecimal max(String modelId, String searchField, Object searchFieldValue, String refDateName, Date begin,
|
||||
Date end, String funcField) {
|
||||
BigDecimal max = null;
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
String collectionName = "entity_" + modelId;
|
||||
Document match = new Document("$match", new Document(searchField, searchFieldValue).append(refDateName,
|
||||
new Document("$gte", begin.getTime()).append("$lte", end.getTime())));
|
||||
|
||||
Document group = new Document("$group", new Document("_id", null).append("max", new Document("$max", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(tempUrl, pipeline);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
max = new BigDecimal(doc.get("max").toString());
|
||||
@@ -149,14 +144,14 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
public BigDecimal min(String modelId, String searchField, Object searchFieldValue, String refDateName, Date begin,
|
||||
Date end, String funcField) {
|
||||
BigDecimal min = null;
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
String collectionName = "entity_" + modelId;
|
||||
Document match = new Document("$match", new Document(searchField, searchFieldValue).append(refDateName,
|
||||
new Document("$gte", begin.getTime()).append("$lte", end.getTime())));
|
||||
|
||||
Document group = new Document("$group", new Document("_id", null).append("min", new Document("$min", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(tempUrl, pipeline);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
min = new BigDecimal(doc.get("min").toString());
|
||||
@@ -189,11 +184,11 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
@Override
|
||||
public BigDecimal variance(String modelId, String searchField, Object searchFieldValue, String refDateName,
|
||||
Date begin, Date end, String funcField, FieldType fieldType) {
|
||||
String tempUrl = url + ".entity_" + modelId;
|
||||
String collectionName = "entity_" + modelId;
|
||||
Bson filter = Filters.and(Filters.eq(searchField, searchFieldValue), Filters.gte(refDateName, begin.getTime()),
|
||||
Filters.lte(refDateName, end.getTime()));
|
||||
FindIterable<Document> findIt = MongodbUtil.find(tempUrl, filter);
|
||||
List<BigDecimal> records = new ArrayList<BigDecimal>();
|
||||
FindIterable<Document> findIt = MongodbUtil.find(collectionName, filter);
|
||||
List<BigDecimal> records = new ArrayList<>();
|
||||
BigDecimal sum = new BigDecimal("0");
|
||||
findIt.forEach(new Block<Document>() {
|
||||
@Override
|
||||
|
||||
@@ -6,25 +6,21 @@ import com.pgmmers.radar.service.cache.SubscribeHandle;
|
||||
import com.pgmmers.radar.service.impl.util.MongodbUtil;
|
||||
import com.pgmmers.radar.service.model.EntityService;
|
||||
import com.pgmmers.radar.vo.model.ModelVO;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import javax.annotation.PostConstruct;
|
||||
import org.bson.Document;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EntityServiceImpl implements EntityService, SubscribeHandle {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(EntityServiceImpl.class);
|
||||
|
||||
@Value("${spring.data.mongodb.uri}")
|
||||
private String url;
|
||||
|
||||
@Autowired
|
||||
private ModelDal modelDal;
|
||||
@@ -39,9 +35,10 @@ public class EntityServiceImpl implements EntityService, SubscribeHandle {
|
||||
modelList = modelDal.listModel(null);
|
||||
cacheService.subscribeModel(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int save(Long modelId, String jsonString, boolean isAllowDuplicate) {
|
||||
String tmpUrl = url + ".entity_" + modelId;
|
||||
String tmpUrl = "entity_" + modelId;
|
||||
Document doc = Document.parse(jsonString);
|
||||
if (!isAllowDuplicate) {
|
||||
ModelVO model = null;//cacheService.getModel(modelId);
|
||||
@@ -71,14 +68,16 @@ public class EntityServiceImpl implements EntityService, SubscribeHandle {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String channel, String message) {
|
||||
modelList = modelDal.listModel(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int save(Long modelId, String jsonString, String attachJson,
|
||||
boolean isAllowDuplicate) {
|
||||
String tmpUrl = url + ".entity_" + modelId;
|
||||
String tmpUrl = "entity_" + modelId;
|
||||
Document doc = Document.parse(jsonString);
|
||||
Document atta = Document.parse(attachJson);
|
||||
ModelVO model = null;//cacheService.getModel(modelId);
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.pgmmers.radar.service.search.SearchEngineService;
|
||||
import com.pgmmers.radar.vo.model.FieldVO;
|
||||
import com.pgmmers.radar.vo.model.ModelVO;
|
||||
import com.pgmmers.radar.vo.model.PreItemVO;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -33,14 +34,6 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@Service
|
||||
public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
|
||||
@@ -53,9 +46,6 @@ public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
@Autowired
|
||||
private CacheService cacheService;
|
||||
|
||||
@Value("${spring.data.mongodb.uri}")
|
||||
private String url;
|
||||
|
||||
@Value("${sys.conf.mongo-restore-days}")
|
||||
private Integer eventExpireDays;
|
||||
|
||||
@@ -180,9 +170,9 @@ public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
ModelVO modelVO = modelDal.getModelById(id);
|
||||
List<FieldVO> fields = modelDal.listField(id);
|
||||
List<PreItemVO> items = modelDal.listPreItem(id, null);
|
||||
String tempUrl = url + ".entity_" + id;
|
||||
MongodbUtil.mongoTemplate.getCollection(tempUrl).drop();
|
||||
MongodbUtil.mongoTemplate.createCollection(tempUrl);
|
||||
String collectionName = "entity_" + id;
|
||||
MongodbUtil.mongoTemplate.getCollection(collectionName).drop();
|
||||
MongodbUtil.mongoTemplate.createCollection(collectionName);
|
||||
List<IndexModel> indexes = new ArrayList<>();
|
||||
|
||||
if (fields == null) {
|
||||
@@ -207,14 +197,16 @@ public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
|
||||
indexes.add(ttlIndex);
|
||||
|
||||
MongodbUtil.getCollection(tempUrl).createIndexes(indexes);
|
||||
MongodbUtil.getCollection(collectionName).createIndexes(indexes);
|
||||
//
|
||||
|
||||
// 重建es index
|
||||
JSONObject total = buildEsMappingJson(fields, items);
|
||||
|
||||
// execute
|
||||
boolean isCreated = searchService.createIndex(modelVO.getGuid().toLowerCase(), modelVO.getModelName().toLowerCase(), total.toJSONString());
|
||||
boolean isCreated = searchService
|
||||
.createIndex(modelVO.getGuid().toLowerCase(), modelVO.getModelName().toLowerCase(),
|
||||
total.toJSONString());
|
||||
logger.info("index mapping:{} is create {}", total.toJSONString(), isCreated);
|
||||
if (isCreated) {
|
||||
modelVO.setStatus(StatusType.INACTIVE.getKey());
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.pgmmers.radar.service.impl.util;
|
||||
|
||||
import com.mongodb.MongoClientURI;
|
||||
import com.mongodb.client.AggregateIterable;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.bson.BsonValue;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
@@ -24,24 +22,22 @@ public class MongodbUtil implements InitializingBean {
|
||||
mongoTemplate = (MongoTemplate) BeanUtils.getBean("mongoTemplate");
|
||||
}
|
||||
|
||||
public static MongoCollection<Document> getCollection(String uri) {
|
||||
MongoClientURI clientURI = new MongoClientURI(uri);
|
||||
return mongoTemplate.getCollection(Objects.requireNonNull(clientURI.getCollection()));
|
||||
public static MongoCollection<Document> getCollection(String collectionName) {
|
||||
return mongoTemplate.getCollection(collectionName);
|
||||
}
|
||||
|
||||
public static void insert(String url, Document doc) {
|
||||
MongoCollection<Document> collection = getCollection(url);
|
||||
public static void insert(String collectionName, Document doc) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
collection.insertOne(doc);
|
||||
}
|
||||
|
||||
public static long count(String url, Bson filter) {
|
||||
MongoCollection<Document> collection = getCollection(url);
|
||||
long count = collection.count(filter);
|
||||
return count;
|
||||
public static long count(String collectionName, Bson filter) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
return collection.countDocuments(filter);
|
||||
}
|
||||
|
||||
public static long distinctCount(String url, Bson filter, String fieldName) {
|
||||
MongoCollection<Document> collection =getCollection(url);
|
||||
public static long distinctCount(String collectionName, Bson filter, String fieldName) {
|
||||
MongoCollection<Document> collection =getCollection(collectionName);
|
||||
long count = 0;
|
||||
Iterator<BsonValue> it = collection.distinct(fieldName, filter, BsonValue.class).iterator();
|
||||
while (it.hasNext()) {
|
||||
@@ -51,16 +47,14 @@ public class MongodbUtil implements InitializingBean {
|
||||
return count;
|
||||
}
|
||||
|
||||
public static AggregateIterable<Document> aggregate(String url, List<Bson> pipeline) {
|
||||
MongoCollection<Document> collection = getCollection(url);
|
||||
AggregateIterable<Document> it = collection.aggregate(pipeline);
|
||||
return it;
|
||||
public static AggregateIterable<Document> aggregate(String collectionName, List<Bson> pipeline) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
return collection.aggregate(pipeline);
|
||||
}
|
||||
|
||||
public static FindIterable<Document> find(String url, Bson filter) {
|
||||
MongoCollection<Document> collection = getCollection(url);
|
||||
FindIterable<Document> it = collection.find(filter);
|
||||
return it;
|
||||
public static FindIterable<Document> find(String collectionName, Bson filter) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
return collection.find(filter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user