david-streamlio commented on a change in pull request #2869: Added Pulsar IO connector for local files URL: https://github.com/apache/pulsar/pull/2869#discussion_r230146251
########## File path: pulsar-io/file/src/main/java/org/apache/pulsar/io/file/FileConsumerThread.java ########## @@ -0,0 +1,115 @@ +/** + * 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.pulsar.io.file; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Optional; +import java.util.concurrent.BlockingQueue; +import java.util.stream.Stream; + +import lombok.Data; + +import org.apache.pulsar.functions.api.Record; +import org.apache.pulsar.io.core.PushSource; +import org.apache.pulsar.io.file.utils.GZipFiles; +import org.apache.pulsar.io.file.utils.ZipFiles; + +/** + * Worker thread that consumes the contents of the files + * and publishes them to a Pulsar topic. + */ +public class FileConsumerThread extends Thread { + + private final PushSource<byte[]> source; + private final BlockingQueue<File> workQueue; + private final BlockingQueue<File> inProcess; + private final BlockingQueue<File> recentlyProcessed; + + public FileConsumerThread(PushSource<byte[]> source, + BlockingQueue<File> workQueue, + BlockingQueue<File> inProcess, + BlockingQueue<File> recentlyProcessed) { + this.source = source; + this.workQueue = workQueue; + this.inProcess = inProcess; + this.recentlyProcessed = recentlyProcessed; + } + + public void run() { + try { + while (true) { + File file = workQueue.take(); + + boolean added = false; + do { + added = inProcess.add(file); + } while (!added); + + consumeFile(file); + } + } catch (InterruptedException ie) { + // just terminate + } + } + + private void consumeFile(File file) { + try (Stream<String> lines = getLines(file)) { + lines.forEachOrdered(line -> process(line)); + } catch (IOException e) { + e.printStackTrace(); + } finally { + + boolean removed = false; + do { + removed = inProcess.remove(file); + } while (!removed); + + boolean added = false; + do { + added = recentlyProcessed.add(file); + } while (!added); + } + } + + private Stream<String> getLines(File file) throws IOException { + if (file == null) { + return null; + } else if (GZipFiles.isGzip(file)) { + return GZipFiles.lines(Paths.get(file.getAbsolutePath())); + } else if (ZipFiles.isZip(file)) { + return ZipFiles.lines(Paths.get(file.getAbsolutePath())); + } else { + return Files.lines(Paths.get(file.getAbsolutePath()), Charset.defaultCharset()); + } + } + + private void process(String line) { + source.consume(new FileRecord(null, line.getBytes())); + } + + @Data + static private class FileRecord implements Record<byte[]> { Review comment: If the user wishes to process the contents of a local file exactly once, then they will need to ensure that they configure the "keepFile" property to false when they submit the function. That way, after the file is successfully processed, it will be deleted and never processed again. I made this configuration optional to support a test scenario in which a user wants to use a single source file to ingest the same data into the topic in support of testing down-stream pulsar functions, etc. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
