支持mongodb

This commit is contained in:
inrgihc
2023-08-21 23:19:01 +08:00
parent ae1d28f896
commit 3ee96f87c6
42 changed files with 634 additions and 33 deletions

View File

@@ -45,6 +45,7 @@
├── dbswitch-product-sybase // -> sybase方言实现类
├── dbswitch-product-hive // -> hive方言实现类
├── dbswitch-product-sqlite // -> sqlite方言实现类
├── dbswitch-product-mongodb // -> mongodb方言实现类
├── dbswitch-data // 工具入口模块,读取配置文件中的参数执行异构迁移同步
├── dbswitch-admin // 在以上模块的基础上引入Quartz的调度服务与接口
├── dbswitch-admin-ui // 基于Vue2的前段WEB交互页面
@@ -343,6 +344,13 @@ jdbc驱动名称org.sqlite.JDBC
>
> (d) SQLite为单写多读方式禁止人为方式造成多写导致锁表。
**MongoDB数据库**
```
jdbc连接地址jdbc:mongodb://172.17.2.12:27017/test?authSource=admin&authMechanism=SCRAM-SHA-1
jdbc驱动名称com.wisecoders.dbschema.mongodb.JdbcDriver
```
#### (2)、启动方法
- linux系统下

View File

@@ -2,7 +2,7 @@
set -e
DBSWITCH_VERSION=1.8.1
DBSWITCH_VERSION=1.8.2
BUILD_DOCKER_DIR="$( cd "$( dirname "$0" )" && pwd )"
PROJECT_ROOT_DIR=$( dirname "$BUILD_DOCKER_DIR")
DOCKER_DBSWITCH_DIR=$BUILD_DOCKER_DIR/dbswitch

View File

@@ -19,7 +19,7 @@ services:
start_period: 30s
dbswitch:
container_name: dbswitch_webui
image: inrgihc/dbswitch:1.8.1
image: inrgihc/dbswitch:1.8.2
environment:
MYSQLDB_HOST: dbswitch_mysqldb
MYSQLDB_PORT: 3306

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<artifactId>dbswitch-admin</artifactId>

View File

@@ -9,7 +9,6 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.admin.service;
import com.gitee.dbswitch.common.converter.ConverterFactory;
import com.gitee.dbswitch.admin.common.exception.DbswitchException;
import com.gitee.dbswitch.admin.common.response.PageResult;
import com.gitee.dbswitch.admin.common.response.Result;
@@ -28,6 +27,7 @@ import com.gitee.dbswitch.admin.model.response.AssignmentDetailResponse;
import com.gitee.dbswitch.admin.model.response.AssignmentInfoResponse;
import com.gitee.dbswitch.admin.type.ScheduleModeEnum;
import com.gitee.dbswitch.admin.util.PageUtils;
import com.gitee.dbswitch.common.converter.ConverterFactory;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.data.config.DbswichProperties;
import com.gitee.dbswitch.data.entity.SourceDataSourceProperties;
@@ -72,13 +72,19 @@ public class AssignmentService {
assignmentConfigDAO.insert(assignmentConfigEntity);
Long targetConnectionId = assignmentConfigEntity.getTargetConnectionId();
DatabaseConnectionEntity entity = databaseConnectionDAO.getById(targetConnectionId);
if (ProductTypeEnum.SQLITE3 == entity.getType()) {
if (ProductTypeEnum.isUnsupportedTargetSqlite(entity.getUrl())) {
DatabaseConnectionEntity targetEntity = databaseConnectionDAO.getById(targetConnectionId);
if (ProductTypeEnum.SQLITE3 == targetEntity.getType()) {
if (ProductTypeEnum.isUnsupportedTargetSqlite(targetEntity.getUrl())) {
throw new DbswitchException(ResultCode.ERROR_INVALID_ASSIGNMENT_CONFIG,
"不支持目的端数据源为远程服务器上的SQLite或内存方式下的SQLite");
}
}
Long sourceConnectionId = assignmentConfigEntity.getSourceConnectionId();
DatabaseConnectionEntity sourceEntity = databaseConnectionDAO.getById(sourceConnectionId);
if (ProductTypeEnum.MONGODB == sourceEntity.getType()) {
throw new DbswitchException(ResultCode.ERROR_INVALID_ASSIGNMENT_CONFIG,
"不支持源端数据源为MongoDB数据库");
}
return ConverterFactory.getConverter(AssignmentInfoConverter.class)
.convert(assignmentTaskDAO.getById(assignment.getId()));
@@ -114,6 +120,12 @@ public class AssignmentService {
"不支持目的端数据源为远程服务器上的SQLite或内存方式下的SQLite");
}
}
Long sourceConnectionId = assignmentConfigEntity.getSourceConnectionId();
DatabaseConnectionEntity sourceEntity = databaseConnectionDAO.getById(sourceConnectionId);
if (ProductTypeEnum.MONGODB == sourceEntity.getType()) {
throw new DbswitchException(ResultCode.ERROR_INVALID_ASSIGNMENT_CONFIG,
"不支持源端数据源为MongoDB数据库");
}
}
public PageResult<AssignmentInfoResponse> listAll(String searchText, Integer page, Integer size) {

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<artifactId>dbswitch-common</artifactId>

View File

@@ -136,6 +136,14 @@ public enum ProductTypeEnum {
"SELECT 1",
"jdbc:opengauss://",
new String[]{"jdbc:opengauss://{host}[:{port}]/[{database}][\\?{params}]"}),
/**
* MongoDB数据库类型
*/
MONGODB(15, "\"", "mongoDB", "com.gitee.jdbc.mongodb.JdbcDriver", 27017,
"use admin;",
"jdbc:mongodb://",
new String[]{"jdbc:mongodb://{host}[:{port}]/[{database}][\\?{params}]"}),
;
private int id;
@@ -223,6 +231,15 @@ public enum ProductTypeEnum {
return this == HIVE;
}
/**
* 是否为MongoDB数据库类型
*
* @return boolean
*/
public boolean isMongodb() {
return this == MONGODB;
}
/**
* 是否存在指定字符串名称的数据库类型
*

View File

@@ -86,10 +86,14 @@ public final class DatabaseAwareUtils {
}
ProductTypeEnum type = productNameMap.get(productName);
if (null == type) {
throw new IllegalStateException("Unable to detect database type from data source instance");
if (null != type) {
return type;
}
return type;
String url = connection.getMetaData().getURL();
if (null != url && url.contains("mongodb://")) {
return ProductTypeEnum.MONGODB;
}
throw new IllegalStateException("Unable to detect database type from data source instance");
} catch (SQLException se) {
throw new RuntimeException(se);
}

View File

@@ -308,6 +308,16 @@ public final class JdbcUrlUtils {
} else {
System.out.println("error for sqlite!");
}
// 12、mongo数据库
// jdbc:sqlite:/tmp/phone.db
final Matcher matcher11 = JdbcUrlUtils.getPattern("jdbc:mongodb://[{user}][:{password}@]{host}[:{port}]/[{database}][\\?{params}]")
.matcher("jdbc:mongodb://root:123456@127.0.0.1:27017/test?authSource=admin&authMechanism=SCRAM-SHA-1&expand=true");
if (matcher11.matches()) {
System.out.println("mongodb database:" + matcher11.group("database"));
} else {
System.out.println("error for mongodb!");
}
}
}

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<artifactId>dbswitch-core</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<artifactId>dbswitch-data</artifactId>

View File

@@ -235,7 +235,16 @@ public class MigrationHandler implements Supplier<Long> {
TableOperateProvider targetOperator = targetFactoryProvider.createTableOperateProvider();
TableDataSynchronizer targetSynchronizer = targetFactoryProvider.createTableDataSynchronizer();
if (properties.getTarget().getTargetDrop() || targetProductType.isLikeHive()) {
if (targetProductType.isMongodb()) {
try {
targetFactoryProvider.createTableOperateProvider()
.dropTable(targetSchemaName, targetTableName);
log.info("Target Table {}.{} is exits, drop it now !", targetSchemaName, targetTableName);
} catch (Exception e) {
log.info("Target Table {}.{} is not exits, create it!", targetSchemaName, targetTableName);
}
return doFullCoverSynchronize(targetWriter, targetOperator, sourceQuerier);
} else if (properties.getTarget().getTargetDrop() || targetProductType.isLikeHive()) {
/*
如果配置了dbswitch.target.datasource-target-drop=true时
<p>

View File

@@ -63,6 +63,8 @@ public final class DataSourceUtils {
parameters.put("remarksReporting", "true");
} else if (properties.getDriverClassName().contains("db2")) {
ds.setConnectionTestQuery("SELECT 1 FROM SYSIBM.SYSDUMMY1");
} else if (properties.getDriverClassName().contains("mongodb")) {
ds.setConnectionTestQuery("use admin;");
} else {
ds.setConnectionTestQuery("SELECT 1");
}
@@ -103,6 +105,8 @@ public final class DataSourceUtils {
ds.setConnectionTestQuery("SELECT 'Hello' from DUAL");
} else if (properties.getDriverClassName().contains("db2")) {
ds.setConnectionTestQuery("SELECT 1 FROM SYSIBM.SYSDUMMY1");
} else if (properties.getDriverClassName().contains("mongodb")) {
ds.setConnectionTestQuery("use admin;");
} else {
ds.setConnectionTestQuery("SELECT 1");
}

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<artifactId>dbswitch-dist</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dbswitch-product-dm</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dbswitch-product-mongodb</artifactId>
<dependencies>
<dependency>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,59 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mongodb;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
import com.gitee.dbswitch.provider.meta.MetadataProvider;
import com.gitee.dbswitch.provider.operate.TableOperateProvider;
import com.gitee.dbswitch.provider.query.TableDataQueryProvider;
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.MONGODB)
public class MongodbFactoryProvider extends AbstractFactoryProvider {
public MongodbFactoryProvider(DataSource dataSource) {
super(dataSource);
}
public ProductFeatures getProductFeatures() {
return new MongodbFeatures();
}
@Override
public MetadataProvider createMetadataQueryProvider() {
return new MongodbMetadataQueryProvider(this);
}
@Override
public TableDataQueryProvider createTableDataQueryProvider() {
return new MongodbTableDataQueryProvider(this);
}
@Override
public TableDataWriteProvider createTableDataWriteProvider(boolean useInsert) {
return new MongodbTableDataWriteProvider(this);
}
@Override
public TableOperateProvider createTableOperateProvider() {
return new MongodbTableOperateProvider(this);
}
@Override
public TableDataSynchronizer createTableDataSynchronizer() {
return new MongodbTableDataSynchronizer(this);
}
}

View File

@@ -0,0 +1,16 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mongodb;
import com.gitee.dbswitch.features.ProductFeatures;
public class MongodbFeatures implements ProductFeatures {
}

View File

@@ -0,0 +1,179 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mongodb;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import com.gitee.dbswitch.provider.meta.AbstractMetadataProvider;
import com.gitee.dbswitch.schema.ColumnDescription;
import com.gitee.dbswitch.schema.ColumnMetaData;
import com.gitee.dbswitch.schema.IndexDescription;
import com.gitee.dbswitch.schema.TableDescription;
import com.google.common.collect.Sets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
@Slf4j
public class MongodbMetadataQueryProvider extends AbstractMetadataProvider {
private static final Set<String> systemSchemas = Sets.newHashSet("admin", "config", "local");
public MongodbMetadataQueryProvider(ProductFactoryProvider factoryProvider) {
super(factoryProvider);
}
@Override
public List<String> querySchemaList(Connection connection) {
List<String> catalogs = new ArrayList<String>();
try (ResultSet rs = connection.getMetaData().getCatalogs()) {
while (rs.next()) {
String name = rs.getString("TABLE_CAT");
if (!systemSchemas.contains(name)) {
catalogs.add(name);
}
}
return catalogs;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<TableDescription> queryTableList(Connection connection, String schemaName) {
List<TableDescription> ret = new ArrayList<>();
try (ResultSet rs = connection.getMetaData().getTables(schemaName, null, null, null)) {
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
TableDescription td = new TableDescription();
td.setSchemaName(schemaName);
td.setTableName(tableName);
td.setRemarks(rs.getString("REMARKS"));
td.setTableType(rs.getString("TABLE_TYPE").toUpperCase());
ret.add(td);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return ret;
}
@Override
public TableDescription queryTableMeta(Connection connection, String schemaName, String tableName) {
try (ResultSet rs = connection.getMetaData()
.getTables(schemaName, null, tableName, new String[]{"TABLE"})) {
if (rs.next()) {
TableDescription td = new TableDescription();
td.setSchemaName(schemaName);
td.setTableName(tableName);
td.setRemarks(rs.getString("REMARKS"));
td.setTableType(rs.getString("TABLE_TYPE").toUpperCase());
return td;
}
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public String getTableDDL(Connection connection, String schemaName, String tableName) {
return null;
}
public String getViewDDL(Connection connection, String schemaName, String tableName) {
return null;
}
public List<String> queryTableColumnName(Connection connection, String schemaName, String tableName) {
List<String> ret = new ArrayList<>();
try (ResultSet rs = connection.getMetaData().getColumns(schemaName, null, tableName, null)) {
while (rs.next()) {
ret.add(rs.getString("COLUMN_NAME"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return ret;
}
public List<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
List<ColumnDescription> ret = new ArrayList<>();
try (ResultSet rs = connection.getMetaData().getColumns(schemaName, null, tableName, null)) {
ResultSetMetaData m = rs.getMetaData();
while (rs.next()) {
ColumnDescription cd = new ColumnDescription();
cd.setFieldName(rs.getString("COLUMN_NAME"));
cd.setLabelName(rs.getString("COLUMN_NAME"));
cd.setFieldType(NumberUtils.toInt(rs.getString("DATA_TYPE")));
cd.setFieldTypeName(rs.getString("TYPE_NAME"));
cd.setFiledTypeClassName(rs.getString("DATA_TYPE"));
cd.setDisplaySize(0);
cd.setPrecisionSize(0);
cd.setScaleSize(0);
cd.setAutoIncrement(false);
cd.setNullable(!"_id".equals(cd.getFieldName()));
cd.setProductType(getProductType());
ret.add(cd);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return ret;
}
public List<ColumnDescription> querySelectSqlColumnMeta(Connection connection, String sql) {
return Collections.emptyList();
}
public List<String> queryTablePrimaryKeys(Connection connection, String schemaName, String tableName) {
List<String> ret = new ArrayList<>();
try (ResultSet rs = connection.getMetaData().getPrimaryKeys(schemaName, null, tableName)) {
while (rs.next()) {
ret.add(rs.getString("COLUMN_NAME"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return ret;
}
public List<IndexDescription> queryTableIndexes(Connection connection, String schemaName, String tableName) {
return Collections.emptyList();
}
public void testQuerySQL(Connection connection, String sql) {
}
public String getQuotedSchemaTableCombination(String schemaName, String tableName) {
return String.format("%s.%s", schemaName, tableName);
}
public String getFieldDefinition(ColumnMetaData v, List<String> pks, boolean useAutoInc, boolean addCr,
boolean withRemarks) {
return null;
}
public String getPrimaryKeyAsString(List<String> pks) {
return null;
}
public List<String> getTableColumnCommentDefinition(TableDescription td, List<ColumnDescription> cds) {
return Collections.emptyList();
}
}

View File

@@ -0,0 +1,101 @@
package com.gitee.dbswitch.product.mongodb;
import cn.hutool.json.JSONUtil;
import com.gitee.dbswitch.common.consts.Constants;
import com.gitee.dbswitch.common.entity.ResultSetWrapper;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import com.gitee.dbswitch.provider.query.TableDataQueryProvider;
import com.gitee.dbswitch.schema.SchemaTableData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.sql.DataSource;
public class MongodbTableDataQueryProvider implements TableDataQueryProvider {
private ProductFactoryProvider factoryProvider;
private DataSource dataSource;
public MongodbTableDataQueryProvider(ProductFactoryProvider factoryProvider) {
this.factoryProvider = factoryProvider;
this.dataSource = factoryProvider.getDataSource();
}
@Override
public ProductTypeEnum getProductType() {
return this.factoryProvider.getProductType();
}
@Override
public int getQueryFetchSize() {
return 0;
}
@Override
public void setQueryFetchSize(int size) {
}
@Override
public ResultSetWrapper queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("%s.%s.find().sort({ %s })",
schemaName, tableName, orders.stream().map(s -> String.format("'%s' : 1", s))
.collect(Collectors.joining(",")));
try {
Connection connection = this.dataSource.getConnection();
Statement statement = connection.createStatement();
statement.setQueryTimeout(Constants.DEFAULT_QUERY_TIMEOUT_SECONDS);
return ResultSetWrapper.builder()
.connection(connection)
.statement(statement)
.resultSet(statement.executeQuery(sql))
.build();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
@Override
public SchemaTableData queryTableData(Connection connection, String schemaName, String tableName, int rowCount) {
String querySQL = String.format("%s.%s.find({});", schemaName, tableName);
SchemaTableData data = new SchemaTableData();
data.setSchemaName(schemaName);
data.setTableName(tableName);
data.setColumns(new ArrayList<>());
data.setRows(new ArrayList<>());
try (Statement st = connection.createStatement()) {
try (ResultSet rs = st.executeQuery(querySQL)) {
ResultSetMetaData m = rs.getMetaData();
int count = m.getColumnCount();
for (int i = 1; i <= count; i++) {
data.getColumns().add(m.getColumnLabel(i));
}
int counter = 0;
while (rs.next() && counter++ < rowCount) {
List<Object> row = new ArrayList<>(count);
for (int i = 1; i <= count; i++) {
Object value = rs.getObject(i);
if (value instanceof Map || value instanceof List) {
row.add(JSONUtil.toJsonStr(value));
} else {
row.add(null == value ? null : value.toString());
}
}
data.getRows().add(row);
}
return data;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,37 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mongodb;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import com.gitee.dbswitch.provider.sync.DefaultTableDataSynchronizer;
import java.util.List;
public class MongodbTableDataSynchronizer extends DefaultTableDataSynchronizer {
public MongodbTableDataSynchronizer(ProductFactoryProvider factoryProvider) {
super(factoryProvider);
}
@Override
public long executeInsert(List<Object[]> records) {
return 0;
}
@Override
public long executeUpdate(List<Object[]> records) {
return 0;
}
@Override
public long executeDelete(List<Object[]> records) {
return 0;
}
}

View File

@@ -0,0 +1,75 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mongodb;
import cn.hutool.json.JSONUtil;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import com.gitee.dbswitch.provider.write.DefaultTableDataWriteProvider;
import com.google.common.collect.Lists;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
@Slf4j
public class MongodbTableDataWriteProvider extends DefaultTableDataWriteProvider {
public MongodbTableDataWriteProvider(ProductFactoryProvider factoryProvider) {
super(factoryProvider);
}
@Override
public void prepareWrite(String schemaName, String tableName, List<String> fieldNames) {
this.columnType = Collections.emptyMap();
this.schemaName = schemaName;
this.tableName = tableName;
}
@Override
public long write(List<String> fieldNames, List<Object[]> recordValues) {
if (CollectionUtils.isEmpty(fieldNames) || CollectionUtils.isEmpty(recordValues)) {
return 0L;
}
for (List<Object[]> partRecordValues : Lists.partition(recordValues, 500)) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s.%s.insertMany", schemaName, tableName));
sb.append("( ").append(asString(fieldNames, partRecordValues)).append(" )");
String sql = sb.toString();
try (Connection connection = getDataSource().getConnection()) {
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return recordValues.size();
}
private String asString(List<String> fieldNames, List<Object[]> recordValues) {
int fieldCount = Math.min(fieldNames.size(), recordValues.get(0).length);
List<Map<String, Object>> rows = new ArrayList<>(recordValues.size());
for (Object[] row : recordValues) {
Map<String, Object> columns = new LinkedHashMap<>(fieldCount);
for (int i = 0; i < fieldCount; ++i) {
columns.put(fieldNames.get(i), row[i]);
}
rows.add(columns);
}
return JSONUtil.toJsonStr(rows);
}
}

View File

@@ -0,0 +1,35 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mongodb;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import com.gitee.dbswitch.provider.operate.DefaultTableOperateProvider;
public class MongodbTableOperateProvider extends DefaultTableOperateProvider {
public MongodbTableOperateProvider(ProductFactoryProvider factoryProvider) {
super(factoryProvider);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
cleanup(schemaName, tableName);
}
@Override
public void dropTable(String schemaName, String tableName) {
cleanup(schemaName, tableName);
}
private void cleanup(String schemaName, String tableName) {
String sql = String.format("%s.%s.drop();", schemaName, tableName);
this.executeSql(sql);
}
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.mongodb.MongodbFactoryProvider

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dbswitch-product-mysql</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-product</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -83,6 +83,11 @@
<artifactId>dbswitch-product-openguass</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-product-mongodb</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>dbswitch-parent</artifactId>
<groupId>com.gitee.dbswitch</groupId>
<version>1.8.1</version>
<version>1.8.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dbswitch-product</artifactId>
@@ -26,6 +26,7 @@
<module>dbswitch-product-hive</module>
<module>dbswitch-product-gbase</module>
<module>dbswitch-product-openguass</module>
<module>dbswitch-product-mongodb</module>
<module>dbswitch-register-product</module>
</modules>

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
<packaging>pom</packaging>
<name>dbswitch</name>
<description>database switch project</description>

View File

@@ -1,6 +1,6 @@
@echo off
set APP_VERSION=1.8.1
set APP_VERSION=1.8.2
echo "Clean Project ..."
call mvn clean -f pom.xml