pandaapo commented on code in PR #4455: URL: https://github.com/apache/eventmesh/pull/4455#discussion_r1334113763
########## eventmesh-connectors/eventmesh-connector-file/src/main/java/org/apache/eventmesh/connector/file/sink/connector/FileSinkConnector.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.eventmesh.connector.file.sink.connector; + +import org.apache.eventmesh.connector.file.sink.config.FileSinkConfig; +import org.apache.eventmesh.openconnect.api.config.Config; +import org.apache.eventmesh.openconnect.api.connector.ConnectorContext; +import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext; +import org.apache.eventmesh.openconnect.api.sink.Sink; +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.time.LocalDateTime; +import java.util.Calendar; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicInteger; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class FileSinkConnector implements Sink { + + private static final AtomicInteger fileSize = new AtomicInteger(0); + + private String filePath; + + private String fileName; + + private int flushSize; + + private boolean hourlyFlushEnabled; + + private FileSinkConfig sinkConfig; + + private PrintStream outputStream; + + @Override + public Class<? extends Config> configClass() { + return FileSinkConfig.class; + } + + @Override + public void init(Config config) { + // init config for hdfs source connector + this.sinkConfig = (FileSinkConfig) config; + this.filePath = buildFilePath(); + this.fileName = buildFileName(); + this.flushSize = sinkConfig.getFlushSize(); + this.hourlyFlushEnabled = sinkConfig.isHourlyFlushEnabled(); + } + + @Override + public void init(ConnectorContext connectorContext) { + // init config for hdfs source connector + SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext; + this.sinkConfig = (FileSinkConfig) sinkConnectorContext.getSinkConfig(); + this.fileName = buildFileName(); + this.filePath = buildFilePath(); + this.flushSize = sinkConfig.getFlushSize(); + this.hourlyFlushEnabled = sinkConfig.isHourlyFlushEnabled(); + } + + + @Override + public void start() throws Exception { + if (fileName == null || fileName.length() == 0 || filePath == null || filePath.length() == 0) { + this.outputStream = System.out; + } else { + this.outputStream = + new PrintStream(Files.newOutputStream(Paths.get(filePath + fileName), StandardOpenOption.CREATE, StandardOpenOption.APPEND), + false, StandardCharsets.UTF_8.name()); + } + } + + @Override + public void commit(ConnectRecord record) { + + } + + @Override + public String name() { + return this.sinkConfig.getConnectorConfig().getConnectorName(); + } + + @Override + public void stop() { + outputStream.flush(); + outputStream.close(); + } + + @Override + public void put(List<ConnectRecord> sinkRecords) { Review Comment: During the start() phase, it is determined that if the file creation is not successful, `System.out` is used directly. However, during the put() process, there is no corresponding handling and it is all file operation. Is this appropriate? --- 在start()阶段,判断了当没有成功创建出文件时,直接采用`System.out`。但是put()的过程中没有相应的处理,全是文件操作,是不是不合适? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
