How to process new files in HDFS directory once their writing has eventually finished?

丶灬走出姿态 提交于 2020-04-11 11:41:48

问题


In my scenario I have CSV files continuously uploaded to HDFS.

As soon as a new file gets uploaded I'd like to process the new file with Spark SQL (e.g., compute the maximum of a field in the file, transform the file into parquet). i.e. I have a one-to-one mapping between each input file and a transformed/processed output file.

I was evaluating Spark Streaming to listen to the HDFS directory, then to process the "streamed file" with Spark.

However, in order to process the whole file I would need to know when the "file stream" completes. I'd like to apply the transformation to the whole file in order to preserve the end-to-end one-to-one mapping between files.

How can I transform the whole file and not its micro-batches?

As far as I know, Spark Streaming can only apply transformation to batches (DStreams mapped to RDDs) and not to the whole file at once (when its finite stream has completed).

Is that correct? If so, what alternative should I consider for my scenario?


回答1:


I may have misunderstood your question the first try...

As far as I know, Spark Streaming can only apply transformation to batches (DStreams mapped to RDDs) and not to the whole file at once (when its finite stream has completed).

Is that correct?

No. That's not correct.

Spark Streaming will apply transformation to the whole file at once as was written to HDFS at the time Spark Streaming's batch interval elapsed.

Spark Streaming will take the current content of a file and start processing it.


As soon as a new file gets uploaded I need to process the new file with Spark/SparkSQL

Almost impossible with Spark due to its architecture which takes some time from the moment "gets uploaded" and Spark processes it.

You should consider using a brand new and shiny Structured Streaming or (soon obsolete) Spark Streaming.

Both solutions support watching a directory for new files and trigger Spark job once a new file gets uploaded (which is exactly your use case).

Quoting Structured Streaming's Input Sources:

In Spark 2.0, there are a few built-in sources.

  • File source - Reads files written in a directory as a stream of data. Supported file formats are text, csv, json, parquet. See the docs of the DataStreamReader interface for a more up-to-date list, and supported options for each file format. Note that the files must be atomically placed in the given directory, which in most file systems, can be achieved by file move operations.

See also Spark Streaming's Basic Sources:

Besides sockets, the StreamingContext API provides methods for creating DStreams from files as input sources.

File Streams: For reading data from files on any file system compatible with the HDFS API (that is, HDFS, S3, NFS, etc.), a DStream can be created as:

streamingContext.fileStream[KeyClass, ValueClass, InputFormatClass](dataDirectory)

Spark Streaming will monitor the directory dataDirectory and process any files created in that directory (files written in nested directories not supported).

One caveat though given your requirement:

I would need to know when the "file stream" completes.

Don't do this with Spark.

Quoting Spark Streaming's Basic Sources again:

  • The files must be created in the dataDirectory by atomically moving or renaming them into the data directory.

  • Once moved, the files must not be changed. So if the files are being continuously appended, the new data will not be read.

Wrapping up...you should only move the files to the directory that Spark watches when the files are complete and ready for processing using Spark. This is outside the scope of Spark.




回答2:


You can use DFSInotifyEventInputStream to watch Hadoop dir and then execute Spark job programmatically when file is created.

See this post: HDFS file watcher



来源:https://stackoverflow.com/questions/44375980/how-to-process-new-files-in-hdfs-directory-once-their-writing-has-eventually-fin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!