harbor run -t openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0490harbor download openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0490solution file
package ru.dzen.kafka.connect.ytsaurus.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.connect.errors.DataException;
import org.apache.kafka.connect.header.Header;
import org.apache.kafka.connect.json.JsonConverter;
import org.apache.kafka.connect.sink.SinkRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.dzen.kafka.connect.ytsaurus.common.BaseTableWriterConfig.AuthType;
import ru.dzen.kafka.connect.ytsaurus.common.BaseTableWriterConfig.OutputTableSchemaType;
import tech.ytsaurus.client.ApiServiceTransaction;
import tech.ytsaurus.client.YTsaurusClient;
import tech.ytsaurus.client.YTsaurusClientConfig;
import tech.ytsaurus.client.request.StartTransaction;
import tech.ytsaurus.ysontree.YTree;
import tech.ytsaurus.ysontree.YTreeNode;
public abstract class BaseTableWriter {
protected static final ObjectMapper objectMapper = new ObjectMapper();
private static final Logger log = LoggerFactory.getLogger(BaseTableWriter.class);
private static final JsonConverter JSON_CONVERTER;
static {
JSON_CONVERTER = new JsonConverter();
JSON_CONVERTER.configure(Collections.singletonMap("schemas.enable", "false"), false);
}
protected final YTsaurusClient client;
protected final BaseOffsetsManager offsetsManager;
protected final BaseTableWriterConfig config;
protected BaseTableWriter(BaseTableWriterConfig config, BaseOffsetsManager offsetsManager) {
this.config = config;
this.client = YTsaurusClient.builder()
.setConfig(YTsaurusClientConfig.builder()
.setTvmOnly(config.getAuthType().equals(AuthType.SERVICE_TICKET)).build())
.setCluster(config.getYtCluster()).setAuth(config.getYtClientAuth()).build();
this.offsetsManager = offsetsManager;
}
protected ApiServiceTransaction createTransaction() throws Exception {
return client.startTransaction(StartTransaction.master()).get();
}
public Map<TopicPartition, OffsetAndMetadata> getSafeToCommitOffsets(
Map<TopicPartition, OffsetAndMetadata> unsafeOffsets) throws Exception {
return offsetsManager.getPrevOffsets(createTransaction(), unsafeOffsets.keySet()).entrySet()
.stream()
.filter(entry -> unsafeOffsets.containsKey(entry.getKey()))
.map(entry -> {
var topicPartition = entry.getKey();
var prevOffset = entry.getValue();
var unsafeOffset = unsafeOffsets.get(topicPartition);
return unsafeOffset.offset() >= prevOffset.offset() ?
Map.entry(topicPartition, prevOffset) :
Map.entry(topicPartition, unsafeOffset);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
protected Object convertRecordKey(SinkRecord record) throws Exception {
if (record.key() == null) {
return JsonNodeFactory.instance.nullNode();
}
if (record.key() instanceof String) {
return record.key();
}
byte[] jsonBytes = JSON_CONVERTER.fromConnectData(record.topic(), record.keySchema(),
record.key());
var jsonString = new String(jsonBytes, StandardCharsets.UTF_8);
JsonNode jsonNode = objectMapper.readTree(jsonString);
return jsonNode;
}
protected YTreeNode convertRecordKeyToNode(SinkRecord record) throws Exception {
var recordKey = convertRecordKey(record);
if
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py