Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Connector-V2][Clickhouse] Clickhouse writing is triggered when a checkpoint is reached #4999

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/en/connector-v2/sink/Clickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ In addition to the above mandatory parameters that must be specified by `clickho

### bulk_size [number]

The number of rows written through [Clickhouse-jdbc](https://github.com/ClickHouse/clickhouse-jdbc) each time, the `default is 20000` .
The number of rows written through [Clickhouse-jdbc](https://github.com/ClickHouse/clickhouse-jdbc) each time, the `default is 20000`, if checkpoints are enabled, writing will also occur at the times when the checkpoints are satisfied .

### split_mode [boolean]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void write(SeaTunnelRow element) throws IOException {

@Override
public Optional<CKCommitInfo> prepareCommit() throws IOException {
flush();
return Optional.empty();
}

Expand All @@ -99,23 +100,7 @@ public void abortPrepare() {}
@Override
public void close() throws IOException {
this.proxy.close();
for (ClickhouseBatchStatement batchStatement : statementMap.values()) {
try (ClickHouseConnectionImpl needClosedConnection =
batchStatement.getClickHouseConnection();
JdbcBatchStatementExecutor needClosedStatement =
batchStatement.getJdbcBatchStatementExecutor()) {
IntHolder intHolder = batchStatement.getIntHolder();
if (intHolder.getValue() > 0) {
flush(needClosedStatement);
intHolder.setValue(0);
}
} catch (SQLException e) {
throw new ClickhouseConnectorException(
CommonErrorCode.SQL_OPERATION_FAILED,
"Failed to close prepared statement.",
e);
}
}
flush();
}

private void addIntoBatch(SeaTunnelRow row, JdbcBatchStatementExecutor clickHouseStatement) {
Expand All @@ -138,6 +123,26 @@ private void flush(JdbcBatchStatementExecutor clickHouseStatement) {
}
}

private void flush() {
for (ClickhouseBatchStatement batchStatement : statementMap.values()) {
try (ClickHouseConnectionImpl needClosedConnection =
batchStatement.getClickHouseConnection();
JdbcBatchStatementExecutor needClosedStatement =
batchStatement.getJdbcBatchStatementExecutor()) {
IntHolder intHolder = batchStatement.getIntHolder();
if (intHolder.getValue() > 0) {
flush(needClosedStatement);
intHolder.setValue(0);
}
} catch (SQLException e) {
throw new ClickhouseConnectorException(
CommonErrorCode.SQL_OPERATION_FAILED,
"Failed to close prepared statement.",
e);
}
}
}

private Map<Shard, ClickhouseBatchStatement> initStatementMap() {
Map<Shard, ClickhouseBatchStatement> result = new HashMap<>(Common.COLLECTION_SIZE);
shardRouter
Expand Down