Skip to content

[FIX][Clickhouse] Fix read clickhouse table to console, task cannot be stopped #7322

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.seatunnel.connectors.seatunnel.clickhouse.source;

import org.apache.seatunnel.api.source.Boundedness;
import org.apache.seatunnel.api.source.Collector;
import org.apache.seatunnel.api.source.SourceReader;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
Expand All @@ -28,34 +29,37 @@
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.client.ClickHouseResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedDeque;

@Slf4j
public class ClickhouseSourceReader implements SourceReader<SeaTunnelRow, ClickhouseSourceSplit> {

private final List<ClickHouseNode> servers;
private ClickHouseClient client;
private final SeaTunnelRowType rowTypeInfo;
private final SourceReader.Context readerContext;
private ClickHouseRequest<?> request;
private final String sql;

private final List<ClickhouseSourceSplit> splits;
private final Context context;
private final Deque<ClickhouseSourceSplit> splitsQueue;

ClickhouseSourceReader(
List<ClickHouseNode> servers,
SourceReader.Context readerContext,
Context context,
SeaTunnelRowType rowTypeInfo,
String sql) {
this.servers = servers;
this.readerContext = readerContext;
this.context = context;
this.rowTypeInfo = rowTypeInfo;
this.sql = sql;
this.splits = new ArrayList<>();
this.splitsQueue = new ConcurrentLinkedDeque<>();
}

@Override
Expand All @@ -75,39 +79,45 @@ public void close() throws IOException {

@Override
public void pollNext(Collector<SeaTunnelRow> output) throws Exception {
if (!splits.isEmpty()) {
try (ClickHouseResponse response = this.request.query(sql).executeAndWait()) {
response.stream()
.forEach(
record -> {
Object[] values =
new Object[this.rowTypeInfo.getFieldNames().length];
for (int i = 0; i < record.size(); i++) {
if (record.getValue(i).isNullOrEmpty()) {
values[i] = null;
} else {
values[i] =
TypeConvertUtil.valueUnwrap(
this.rowTypeInfo.getFieldType(i),
record.getValue(i));
synchronized (output.getCheckpointLock()) {
ClickhouseSourceSplit nextSplit = splitsQueue.poll();
if (nextSplit != null) {
try (ClickHouseResponse response = this.request.query(sql).executeAndWait()) {
response.stream()
.forEach(
record -> {
Object[] values =
new Object[this.rowTypeInfo.getFieldNames().length];
for (int i = 0; i < record.size(); i++) {
if (record.getValue(i).isNullOrEmpty()) {
values[i] = null;
} else {
values[i] =
TypeConvertUtil.valueUnwrap(
this.rowTypeInfo.getFieldType(i),
record.getValue(i));
}
}
}
output.collect(new SeaTunnelRow(values));
});
output.collect(new SeaTunnelRow(values));
});
}
}
if (Boundedness.BOUNDED.equals(context.getBoundedness()) && splitsQueue.isEmpty()) {
// signal to the source that we have reached the end of the data.
log.info("Closed the bounded Clickhouse source");
context.signalNoMoreElement();
}
this.readerContext.signalNoMoreElement();
this.splits.clear();
}
}

@Override
public List<ClickhouseSourceSplit> snapshotState(long checkpointId) throws Exception {
return Collections.emptyList();
return new ArrayList<>(splitsQueue);
}

@Override
public void addSplits(List<ClickhouseSourceSplit> splits) {
this.splits.addAll(splits);
splitsQueue.addAll(splits);
}

@Override
Expand Down
Loading