xwm1992 commented on code in PR #4455: URL: https://github.com/apache/eventmesh/pull/4455#discussion_r1334153752
########## 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: 这里如果是System.out的话依然是正常的输出数据,只不过是打印到控制台,不会影响正常逻辑,不过对于`openWithNewFile`这个方法的逻辑,我补充了判断后返回`System.out`的逻辑 -- 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]
