From 62be011324cfe5a58cbf561ddaa37a9d3b65a213 Mon Sep 17 00:00:00 2001 From: fuyao Date: Thu, 21 Apr 2022 11:34:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Emongodb=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90=E6=93=8D=E4=BD=9C=EF=BC=8C=E7=9B=AE=E5=89=8D=E6=9C=AA?= =?UTF-8?q?=E5=AF=B9=E6=93=8D=E4=BD=9C=E6=96=B9=E5=BC=8F=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=EF=BC=8C=E9=9C=80=E8=A6=81=E5=9C=A8mongodb?= =?UTF-8?q?=E4=B8=8A=E8=BF=9B=E8=A1=8C=E7=94=A8=E6=88=B7=E7=9A=84=E6=9D=83?= =?UTF-8?q?=E9=99=90=E9=99=90=E5=88=B6=EF=BC=8C=E4=BB=A5=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=BF=9B=E8=A1=8C=E9=9D=9E=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=B1=BB=E6=93=8D=E4=BD=9C=20sql=E7=9A=84=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E7=B1=BB=E4=BC=BC=EF=BC=9A=20{=20=20=20"find?= =?UTF-8?q?":"=E3=80=8A=E9=9B=86=E5=90=88=E5=90=8D=E7=A7=B0=E3=80=8B"=20}?= =?UTF-8?q?=20=E5=85=B7=E4=BD=93=E6=96=B9=E5=BC=8F=E5=8F=82=E8=80=83?= =?UTF-8?q?=EF=BC=9Ahttps://www.mongodb.com/docs/manual/reference/command/?= =?UTF-8?q?find/#mongodb-dbcommand-dbcmd.find?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- report-core/pom.xml | 5 + .../gaea/business/ReportApplication.java | 9 +- .../controller/dto/DataSourceDto.java | 9 ++ .../service/impl/DataSourceServiceImpl.java | 135 +++++++++++++++--- .../gaea/business/util/JdbcConstants.java | 1 + .../migration/V1.0.18__add_dict_mongodb.sql | 1 + 6 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 report-core/src/main/resources/db/migration/V1.0.18__add_dict_mongodb.sql diff --git a/report-core/pom.xml b/report-core/pom.xml index 7118783c..a2d9cc64 100644 --- a/report-core/pom.xml +++ b/report-core/pom.xml @@ -105,6 +105,11 @@ flyway-core 5.2.1 + + org.mongodb + mongo-java-driver + 3.12.10 + net.sf.ehcache diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/ReportApplication.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/ReportApplication.java index b4f7ea38..e5607109 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/ReportApplication.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/ReportApplication.java @@ -4,10 +4,12 @@ import com.anji.plus.gaea.annotation.enabled.EnabledGaeaConfiguration; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScans; +import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; /** * 业务模板 + * * @author lr * @since 2021-02-03 */ @@ -15,14 +17,15 @@ import org.springframework.context.annotation.ComponentScans; @SpringBootApplication(scanBasePackages = { "com.anjiplus.template.gaea", "com.anji.plus" -}) +}, + exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) @MapperScan(basePackages = { "com.anjiplus.template.gaea.business.modules.*.dao", "com.anjiplus.template.gaea.business.modules.*.**.dao", "com.anji.plus.gaea.*.module.*.dao" }) public class ReportApplication { - public static void main( String[] args ) { + public static void main(String[] args) { SpringApplication.run(ReportApplication.class); } } diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/controller/dto/DataSourceDto.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/controller/dto/DataSourceDto.java index e22eea62..9e8b5fa2 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/controller/dto/DataSourceDto.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/controller/dto/DataSourceDto.java @@ -53,6 +53,15 @@ public class DataSourceDto extends GaeaBaseDTO implements Serializable { /** 关系型数据库sql */ private String sql; + /**************************************************************/ + /**MongoDB数据库连接Url */ + private String connectUrl; + + /** MongoDB数据库 */ + private String database; + + /**************************************************************/ + /** http requestUrl */ private String apiUrl; diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/DataSourceServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/DataSourceServiceImpl.java index 0485a294..d09392b0 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/DataSourceServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/DataSourceServiceImpl.java @@ -21,8 +21,16 @@ import com.anjiplus.template.gaea.business.modules.datasource.service.DataSource import com.anjiplus.template.gaea.business.modules.datasource.service.JdbcService; import com.anjiplus.template.gaea.business.util.JdbcConstants; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.mongodb.BasicDBObject; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import com.mongodb.client.MongoDatabase; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.compress.utils.Lists; +import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -33,11 +41,15 @@ import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; -import java.sql.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * @author Raod @@ -68,6 +80,7 @@ public class DataSourceServiceImpl implements DataSourceService { /** * 获取所有数据源 + * * @return */ @Override @@ -106,6 +119,9 @@ public class DataSourceServiceImpl implements DataSourceService { case JdbcConstants.HTTP: testHttp(dto); break; + case JdbcConstants.MONGODB: + testMongoDb(dto); + break; default: throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_TYPE_DOES_NOT_MATCH_TEMPORARILY); } @@ -129,6 +145,8 @@ public class DataSourceServiceImpl implements DataSourceService { return executeRelationalDb(dto); case JdbcConstants.HTTP: return executeHttp(dto); + case JdbcConstants.MONGODB: + return executeMongoDb(dto); default: throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_TYPE_DOES_NOT_MATCH_TEMPORARILY); } @@ -157,11 +175,12 @@ public class DataSourceServiceImpl implements DataSourceService { /** * 获取mysql count 和添加limit分页信息 + * * @param sourceDto * @param dto * @return */ - public long mysqlTotal(DataSourceDto sourceDto, DataSetDto dto){ + public long mysqlTotal(DataSourceDto sourceDto, DataSetDto dto) { String dynSentence = sourceDto.getDynSentence(); String sql = "select count(1) as count from (" + dynSentence + ") as gaeaExecute"; sourceDto.setDynSentence(sql); @@ -177,7 +196,6 @@ public class DataSourceServiceImpl implements DataSourceService { } - public List executeElasticsearchSql(DataSourceDto dto) { analysisHttpConfig(dto); HttpHeaders headers = new HttpHeaders(); @@ -187,7 +205,7 @@ public class DataSourceServiceImpl implements DataSourceService { try { exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, JSONObject.class); } catch (Exception e) { - log.error("error",e); + log.error("error", e); throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); } if (exchange.getStatusCode().isError()) { @@ -214,7 +232,7 @@ public class DataSourceServiceImpl implements DataSourceService { result.add(jsonObject); } } catch (Exception e) { - log.error("error",e); + log.error("error", e); throw BusinessExceptionBuilder.build(ResponseCode.ANALYSIS_DATA_ERROR, e.getMessage()); } return result; @@ -246,7 +264,7 @@ public class DataSourceServiceImpl implements DataSourceService { Object result = dealResult(value); jo.put(t, result); } catch (SQLException throwable) { - log.error("error",throwable); + log.error("error", throwable); throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage()); } }); @@ -254,7 +272,7 @@ public class DataSourceServiceImpl implements DataSourceService { } return list; } catch (Exception throwable) { - log.error("error",throwable); + log.error("error", throwable); throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage()); } finally { try { @@ -262,15 +280,57 @@ public class DataSourceServiceImpl implements DataSourceService { pooledConnection.close(); } } catch (SQLException throwable) { - log.error("error",throwable); + log.error("error", throwable); throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, throwable.getMessage()); } } } + + /** + * MongoDB数据库 查询执行 + * + * @param dto + */ + public List executeMongoDb(DataSourceDto dto) { + analysisMongoDbConfig(dto); + MongoClient mongoClient = null; + List result = Lists.newArrayList(); + try { + //或者采用连接字符串 + MongoClientURI connectionString = new MongoClientURI(dto.getConnectUrl() + "/" + dto.getDatabase()); + mongoClient = new MongoClient(connectionString); + //连接数据库 + MongoDatabase database = mongoClient.getDatabase(dto.getDatabase()); + String dynSentence = dto.getDynSentence(); + if (StringUtils.isNotBlank(dynSentence)) { + BasicDBObject object = BasicDBObject.parse(dynSentence); + Document document = database.runCommand(object); + if (document.containsKey("ok")) { + Document cursor = document.get("cursor", Document.class); + if (null != cursor) { + List firstBatch = cursor.getList("firstBatch", Document.class); + if (CollectionUtils.isNotEmpty(firstBatch)) { + result = firstBatch.stream().map(s -> JSONObject.parseObject(JSONObject.toJSONString(s))).collect(Collectors.toList()); + } + } + } + } + System.out.println(result); + } catch (Exception e) { + throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); + } finally { + if (null != mongoClient) { + mongoClient.close(); + } + } + return result; + } + /** * 解决sql返回值 类型问题 * (through reference chain: java.util.HashMap["pageData"]->java.util.ArrayList[0]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"]) + * * @param result * @return * @throws SQLException @@ -302,7 +362,7 @@ public class DataSourceServiceImpl implements DataSourceService { try { exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, Object.class); } catch (Exception e) { - log.error("error",e); + log.error("error", e); throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); } if (exchange.getStatusCode().isError()) { @@ -336,7 +396,7 @@ public class DataSourceServiceImpl implements DataSourceService { log.info("数据库测试连接成功:{}", catalog); unPooledConnection.close(); } catch (SQLException e) { - log.error("error",e); + log.error("error", e); if (e.getCause() instanceof ClassNotFoundException) { throw BusinessExceptionBuilder.build(ResponseCode.CLASS_NOT_FOUND, e.getCause().getMessage()); } else { @@ -346,6 +406,34 @@ public class DataSourceServiceImpl implements DataSourceService { } } + + /** + * MongoDB数据库 测试连接 + * + * @param dto + */ + public void testMongoDb(DataSourceDto dto) { + analysisMongoDbConfig(dto); + MongoClient mongoClient = null; + try { + //或者采用连接字符串 + MongoClientURI connectionString = new MongoClientURI(dto.getConnectUrl() + "/" + dto.getDatabase()); + mongoClient = new MongoClient(connectionString); + //连接数据库 + MongoDatabase database = mongoClient.getDatabase(dto.getDatabase()); + BasicDBObject object = new BasicDBObject(); + object.put("hello", "1"); + Document document = database.runCommand(object); + log.info("数据库测试连接成功:{}", document); + } catch (Exception e) { + throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); + } finally { + if (null != mongoClient) { + mongoClient.close(); + } + } + } + /** * http 测试连接 * @@ -399,8 +487,8 @@ public class DataSourceServiceImpl implements DataSourceService { public void analysisRelationalDbConfig(DataSourceDto dto) { JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); - GaeaAssert.isFalse(json.containsKey("jdbcUrl"), ResponseCode.PARAM_IS_NULL,"jdbcUrl not empty"); - GaeaAssert.isFalse(json.containsKey("driverName"), ResponseCode.PARAM_IS_NULL,"driverName not empty"); + GaeaAssert.isFalse(json.containsKey("jdbcUrl"), ResponseCode.PARAM_IS_NULL, "jdbcUrl not empty"); + GaeaAssert.isFalse(json.containsKey("driverName"), ResponseCode.PARAM_IS_NULL, "driverName not empty"); String jdbcUrl = json.getString("jdbcUrl"); String username = json.getString("username"); String password = json.getString("password"); @@ -420,10 +508,10 @@ public class DataSourceServiceImpl implements DataSourceService { */ public void analysisHttpConfig(DataSourceDto dto) { JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); - GaeaAssert.isFalse(json.containsKey("apiUrl"), ResponseCode.PARAM_IS_NULL,"apiUrl not empty"); - GaeaAssert.isFalse(json.containsKey("method"), ResponseCode.PARAM_IS_NULL,"method not empty"); - GaeaAssert.isFalse(json.containsKey("header"), ResponseCode.PARAM_IS_NULL,"header not empty"); - GaeaAssert.isFalse(json.containsKey("body"), ResponseCode.PARAM_IS_NULL,"body not empty"); + GaeaAssert.isFalse(json.containsKey("apiUrl"), ResponseCode.PARAM_IS_NULL, "apiUrl not empty"); + GaeaAssert.isFalse(json.containsKey("method"), ResponseCode.PARAM_IS_NULL, "method not empty"); + GaeaAssert.isFalse(json.containsKey("header"), ResponseCode.PARAM_IS_NULL, "header not empty"); + GaeaAssert.isFalse(json.containsKey("body"), ResponseCode.PARAM_IS_NULL, "body not empty"); String apiUrl = json.getString("apiUrl"); String method = json.getString("method"); String header = json.getString("header"); @@ -438,6 +526,21 @@ public class DataSourceServiceImpl implements DataSourceService { dto.setBody(body); } + /** + * 请求参数转换 + * + * @param dto + */ + public void analysisMongoDbConfig(DataSourceDto dto) { + JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); + GaeaAssert.isFalse(json.containsKey("connectUrl"), ResponseCode.PARAM_IS_NULL, "connectUrl not empty"); + GaeaAssert.isFalse(json.containsKey("database"), ResponseCode.PARAM_IS_NULL, "database not empty"); + String connectUrl = json.getString("connectUrl"); + String database = json.getString("database"); + dto.setConnectUrl(connectUrl); + dto.setDatabase(database); + } + /** * 操作后续处理 * diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/util/JdbcConstants.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/util/JdbcConstants.java index 0d2febe5..12658b0a 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/util/JdbcConstants.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/util/JdbcConstants.java @@ -15,6 +15,7 @@ public class JdbcConstants { public final static String SQL_SERVER = "mssqlserver"; public final static String JDBC = "jdbc"; public final static String POSTGRESQL = "postgresql"; + public final static String MONGODB = "mongodb"; public final static String JTDS = "jtds"; diff --git a/report-core/src/main/resources/db/migration/V1.0.18__add_dict_mongodb.sql b/report-core/src/main/resources/db/migration/V1.0.18__add_dict_mongodb.sql new file mode 100644 index 00000000..978a6ce0 --- /dev/null +++ b/report-core/src/main/resources/db/migration/V1.0.18__add_dict_mongodb.sql @@ -0,0 +1 @@ +INSERT INTO `aj_report`.`gaea_dict_item` (`id`, `dict_code`, `item_name`, `item_value`, `item_extend`, `enabled`, `locale`, `remark`, `sort`, `create_by`, `create_time`, `update_by`, `update_time`, `version`) VALUES (277, 'SOURCE_TYPE', 'mongodb', 'mongodb', '[{\"label\":\"connectUrl\",\"value\":\"mongodb://test:123456@localhost:27017\",\"labelValue\":\"连接串\"},{\"label\":\"database\",\"value\":\"asc_db\",\"labelValue\":\"数据库名称\"}]', 1, 'zh', NULL, 1, 'fuyao', '2021-03-23 10:54:08', 'fuyao', '2021-03-26 13:22:05', 3); -- Gitee