castorqin commented on code in PR #8824: URL: https://github.com/apache/inlong/pull/8824#discussion_r1312633395
########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/sink/pulsar/PulsarSinkOperator.java: ########## @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.manager.service.sink.pulsar; + +import org.apache.inlong.manager.common.consts.SinkType; +import org.apache.inlong.manager.common.consts.SortStandAloneConfig.PulsarParamsConfig; +import org.apache.inlong.manager.common.enums.ErrorCodeEnum; +import org.apache.inlong.manager.common.exceptions.BusinessException; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.dao.entity.InlongStreamEntity; +import org.apache.inlong.manager.dao.entity.StreamSinkEntity; +import org.apache.inlong.manager.dao.mapper.InlongStreamEntityMapper; +import org.apache.inlong.manager.pojo.sink.SinkField; +import org.apache.inlong.manager.pojo.sink.SinkRequest; +import org.apache.inlong.manager.pojo.sink.StreamSink; +import org.apache.inlong.manager.pojo.sink.pulsar.PulsarSink; +import org.apache.inlong.manager.pojo.sink.pulsar.PulsarSinkDTO; +import org.apache.inlong.manager.pojo.sink.pulsar.PulsarSinkRequest; +import org.apache.inlong.manager.service.sink.AbstractSinkOperator; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +/** + * Pulsar sink operator + */ +@Service +public class PulsarSinkOperator extends AbstractSinkOperator { + + private static final Logger LOGGER = LoggerFactory.getLogger(PulsarSinkOperator.class); + + @Autowired + private ObjectMapper objectMapper; + @Autowired + InlongStreamEntityMapper inlongStreamEntityMapper; + + @Override + public Boolean accept(String sinkType) { + return SinkType.KAFKA.equals(sinkType); + } + + @Override + protected String getSinkType() { + return SinkType.KAFKA; + } + + @Override + protected void setTargetEntity(SinkRequest request, StreamSinkEntity targetEntity) { + if (!this.getSinkType().equals(request.getSinkType())) { + throw new BusinessException(ErrorCodeEnum.SINK_TYPE_NOT_SUPPORT, + ErrorCodeEnum.SINK_TYPE_NOT_SUPPORT.getMessage() + ": " + getSinkType()); + } + PulsarSinkRequest sinkRequest = (PulsarSinkRequest) request; + try { + PulsarSinkDTO dto = PulsarSinkDTO.getFromRequest(sinkRequest, targetEntity.getExtParams()); + targetEntity.setExtParams(objectMapper.writeValueAsString(dto)); + } catch (Exception e) { + throw new BusinessException(ErrorCodeEnum.SINK_SAVE_FAILED, + String.format("serialize extParams of Kafka SinkDTO failure: %s", e.getMessage())); + } + } + + @Override + public StreamSink getFromEntity(StreamSinkEntity entity) { + PulsarSink sink = new PulsarSink(); + if (entity == null) { + return sink; + } + + PulsarSinkDTO dto = PulsarSinkDTO.getFromJson(entity.getExtParams()); + CommonBeanUtils.copyProperties(entity, sink, true); + CommonBeanUtils.copyProperties(dto, sink, true); + List<SinkField> sinkFields = super.getSinkFields(entity.getId()); + sink.setSinkFieldList(sinkFields); + return sink; + } + + @Override + public Map<String, String> parse2IdParams(StreamSinkEntity streamSink, List<String> fields) { + + InlongStreamEntity inlongStreamEntity = inlongStreamEntityMapper.selectByIdentifier( + streamSink.getInlongGroupId(), streamSink.getInlongStreamId()); + Map<String, String> params = super.parse2IdParams(streamSink, fields); + PulsarSinkDTO pulsarSinkDTO; + try { + pulsarSinkDTO = objectMapper.readValue(streamSink.getExtParams(), PulsarSinkDTO.class); + } catch (JsonProcessingException e) { + LOGGER.error("parse id error", e); + return params; + } + params.put(PulsarParamsConfig.TOPIC, pulsarSinkDTO.getTopic()); + params.put(PulsarParamsConfig.KEY_SEPARATOR, inlongStreamEntity.getDataSeparator()); Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
