forked from alibaba/canal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
202 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 10 additions & 7 deletions
17
.../src/main/java/com/alibaba/otter/canal/client/adapter/clickhouse/config/ConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
package com.alibaba.otter.canal.client.adapter.clickhouse.config; | ||
|
||
import com.alibaba.otter.canal.client.adapter.support.MappingConfigsLoader; | ||
import com.alibaba.otter.canal.client.adapter.support.YamlUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.alibaba.otter.canal.client.adapter.support.MappingConfigsLoader; | ||
import com.alibaba.otter.canal.client.adapter.support.YamlUtils; | ||
|
||
/** | ||
* CLICKHOUSE表映射配置加载器 | ||
* | ||
* @author rewerma 2018-11-07 下午02:41:34 | ||
* @version 1.0.0 | ||
* @author: Xander | ||
* @date: Created in 2023/11/10 22:23 | ||
* @email: [email protected] | ||
* @version 1.1.8 | ||
*/ | ||
public class ConfigLoader { | ||
|
||
|
13 changes: 8 additions & 5 deletions
13
...src/main/java/com/alibaba/otter/canal/client/adapter/clickhouse/config/MappingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package com.alibaba.otter.canal.client.adapter.clickhouse.config; | ||
|
||
import com.alibaba.otter.canal.client.adapter.support.AdapterConfig; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import com.alibaba.otter.canal.client.adapter.support.AdapterConfig; | ||
|
||
/** | ||
* CLICKHOUSE表映射配置 | ||
* | ||
* @author rewerma 2018-11-07 下午02:41:34 | ||
* @version 1.0.0 | ||
* @author: Xander | ||
* @date: Created in 2023/11/10 22:23 | ||
* @email: [email protected] | ||
* @version 1.1.8 | ||
*/ | ||
public class MappingConfig implements AdapterConfig { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,17 @@ | |
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* @author: Xander | ||
* @date: Created in 2023/11/10 22:23 | ||
* @email: [email protected] | ||
* @version 1.1.8 | ||
*/ | ||
|
||
public class MirrorDbConfig { | ||
|
||
private String fileName; | ||
private MappingConfig mappingConfig; | ||
private String fileName; | ||
private MappingConfig mappingConfig; | ||
private Map<String, MappingConfig> tableConfig = new ConcurrentHashMap<>(); | ||
|
||
public static MirrorDbConfig create(String fileName, MappingConfig mappingConfig) { | ||
|
22 changes: 15 additions & 7 deletions
22
...va/com/alibaba/otter/canal/client/adapter/clickhouse/monitor/ClickHouseConfigMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
package com.alibaba.otter.canal.client.adapter.clickhouse.monitor; | ||
|
||
import com.alibaba.otter.canal.client.adapter.clickhouse.ClickHouseAdapter; | ||
import com.alibaba.otter.canal.client.adapter.clickhouse.config.MappingConfig; | ||
import com.alibaba.otter.canal.client.adapter.support.MappingConfigsLoader; | ||
import com.alibaba.otter.canal.client.adapter.support.Util; | ||
import com.alibaba.otter.canal.client.adapter.support.YamlUtils; | ||
import java.io.File; | ||
import java.util.Properties; | ||
|
||
import org.apache.commons.io.filefilter.FileFilterUtils; | ||
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; | ||
import org.apache.commons.io.monitor.FileAlterationMonitor; | ||
import org.apache.commons.io.monitor.FileAlterationObserver; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.util.Properties; | ||
import com.alibaba.otter.canal.client.adapter.clickhouse.ClickHouseAdapter; | ||
import com.alibaba.otter.canal.client.adapter.clickhouse.config.MappingConfig; | ||
import com.alibaba.otter.canal.client.adapter.support.MappingConfigsLoader; | ||
import com.alibaba.otter.canal.client.adapter.support.Util; | ||
import com.alibaba.otter.canal.client.adapter.support.YamlUtils; | ||
|
||
/** | ||
* @author: Xander | ||
* @date: Created in 2023/11/10 22:23 | ||
* @email: [email protected] | ||
* @version 1.1.8 | ||
*/ | ||
|
||
public class ClickHouseConfigMonitor { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
package com.alibaba.otter.canal.client.adapter.clickhouse.service; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.*; | ||
import java.util.concurrent.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONWriter.Feature; | ||
|
@@ -10,25 +23,14 @@ | |
import com.alibaba.otter.canal.client.adapter.clickhouse.support.SyncUtil; | ||
import com.alibaba.otter.canal.client.adapter.support.Dml; | ||
import com.alibaba.otter.canal.client.adapter.support.Util; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.*; | ||
import java.util.concurrent.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* ClickHouse batch synchronize | ||
* | ||
* @author: Xander | ||
* @date: Created in 2023/11/10 22:23 | ||
* @email: [email protected] | ||
* @version 1.1.8 | ||
*/ | ||
public class ClickHouseBatchSyncService { | ||
|
||
|
@@ -45,7 +47,7 @@ public class ClickHouseBatchSyncService { | |
private BatchExecutor alterExecutors; // Alter Single Executor(update/delete/truncate) | ||
|
||
private ExecutorService[] executorThreads; // Be initialized once | ||
|
||
private ScheduledExecutorService[] scheduledExecutors; | ||
|
||
private int threads = 3; // Default parallel thread count | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package com.alibaba.otter.canal.client.adapter.clickhouse.service; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import com.alibaba.otter.canal.client.adapter.clickhouse.config.MappingConfig; | ||
import com.alibaba.otter.canal.client.adapter.clickhouse.config.MappingConfig.DbMapping; | ||
|
@@ -9,19 +18,13 @@ | |
import com.alibaba.otter.canal.client.adapter.support.EtlResult; | ||
import com.alibaba.otter.canal.client.adapter.support.Util; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* ClickHouse ETL 操作业务类 | ||
* | ||
* @author rewerma @ 2018-11-7 | ||
* @version 1.0.0 | ||
* @author: Xander | ||
* @date: Created in 2023/11/10 22:23 | ||
* @email: [email protected] | ||
* @version 1.1.8 | ||
*/ | ||
public class ClickHouseEtlService extends AbstractEtlService { | ||
|
||
|
@@ -58,23 +61,23 @@ protected boolean executeSqlImport(DataSource srcDS, String sql, List<Object> va | |
Util.sqlRS(targetDS, | ||
"SELECT * FROM " + SyncUtil.getDbTableName(dbMapping, dataSource.getDbType()) + " LIMIT 1 ", | ||
rs -> { | ||
try { | ||
try { | ||
|
||
ResultSetMetaData rsd = rs.getMetaData(); | ||
int columnCount = rsd.getColumnCount(); | ||
List<String> columns = new ArrayList<>(); | ||
for (int i = 1; i <= columnCount; i++) { | ||
columnType.put(rsd.getColumnName(i).toLowerCase(), rsd.getColumnType(i)); | ||
columns.add(rsd.getColumnName(i)); | ||
} | ||
|
||
ResultSetMetaData rsd = rs.getMetaData(); | ||
int columnCount = rsd.getColumnCount(); | ||
List<String> columns = new ArrayList<>(); | ||
for (int i = 1; i <= columnCount; i++) { | ||
columnType.put(rsd.getColumnName(i).toLowerCase(), rsd.getColumnType(i)); | ||
columns.add(rsd.getColumnName(i)); | ||
columnsMap.putAll(SyncUtil.getColumnsMap(dbMapping, columns)); | ||
return true; | ||
} catch (Exception e) { | ||
logger.error(e.getMessage(), e); | ||
return false; | ||
} | ||
|
||
columnsMap.putAll(SyncUtil.getColumnsMap(dbMapping, columns)); | ||
return true; | ||
} catch (Exception e) { | ||
logger.error(e.getMessage(), e); | ||
return false; | ||
} | ||
}); | ||
}); | ||
|
||
Util.sqlRS(srcDS, sql, values, rs -> { | ||
int idx = 1; | ||
|
@@ -86,8 +89,10 @@ protected boolean executeSqlImport(DataSource srcDS, String sql, List<Object> va | |
insertSql.append("INSERT INTO ") | ||
.append(SyncUtil.getDbTableName(dbMapping, dataSource.getDbType())) | ||
.append(" ("); | ||
columnsMap | ||
.forEach((targetColumnName, srcColumnName) -> insertSql.append(backtick).append(targetColumnName).append(backtick).append(",")); | ||
columnsMap.forEach((targetColumnName, srcColumnName) -> insertSql.append(backtick) | ||
.append(targetColumnName) | ||
.append(backtick) | ||
.append(",")); | ||
|
||
int len = insertSql.length(); | ||
insertSql.delete(len - 1, len).append(") VALUES ("); | ||
|
@@ -97,7 +102,7 @@ protected boolean executeSqlImport(DataSource srcDS, String sql, List<Object> va | |
} | ||
len = insertSql.length(); | ||
insertSql.delete(len - 1, len).append(")"); | ||
logger.info("executeSqlImport sql:{}",insertSql.toString()); | ||
logger.info("executeSqlImport sql:{}", insertSql.toString()); | ||
try (Connection connTarget = targetDS.getConnection(); | ||
PreparedStatement pstmt = connTarget.prepareStatement(insertSql.toString())) { | ||
connTarget.setAutoCommit(false); | ||
|
@@ -110,7 +115,8 @@ protected boolean executeSqlImport(DataSource srcDS, String sql, List<Object> va | |
// 删除数据 | ||
Map<String, Object> pkVal = new LinkedHashMap<>(); | ||
StringBuilder deleteSql = new StringBuilder( | ||
"ALTER TABLE " + SyncUtil.getDbTableName(dbMapping, dataSource.getDbType()) + " DELETE WHERE "); | ||
"ALTER TABLE " + SyncUtil.getDbTableName(dbMapping, dataSource.getDbType()) | ||
+ " DELETE WHERE "); | ||
appendCondition(dbMapping, deleteSql, pkVal, rs, backtick); | ||
try (PreparedStatement pstmt2 = connTarget.prepareStatement(deleteSql.toString())) { | ||
int k = 1; | ||
|
Oops, something went wrong.