Github user tdas commented on a diff in the pull request:
https://github.com/apache/spark/pull/11034#discussion_r52372901
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/FileStreamSource.scala
---
@@ -0,0 +1,233 @@
+/*
+ * 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.spark.sql.execution.streaming
+
+import java.io._
+
+import scala.collection.mutable.{ArrayBuffer, HashMap}
+import scala.io.Codec
+
+import com.google.common.base.Charsets.UTF_8
+import org.apache.hadoop.fs.{FileStatus, FileSystem, Path}
+
+import org.apache.spark.Logging
+import org.apache.spark.sql.{DataFrame, SQLContext}
+import org.apache.spark.sql.types.{StringType, StructType}
+import org.apache.spark.util.collection.OpenHashSet
+
+/**
+ * A very simple source that reads text files from the given directory as
they appear.
+ *
+ * TODO Clean up the metadata files periodically
+ */
+class FileStreamSource(
+ sqlContext: SQLContext,
+ metadataPath: String,
+ path: String,
+ dataSchema: Option[StructType],
+ providerName: String,
+ dataFrameBuilder: Array[String] => DataFrame) extends Source with
Logging {
+
+ private val fs =
FileSystem.get(sqlContext.sparkContext.hadoopConfiguration)
+ private var maxBatchId = -1
+ private val seenFiles = new OpenHashSet[String]
+
+ /** Cache files for each batch. The content of this map is also stored
in the disk. */
+ private val batchToMetadata = new HashMap[Long, Seq[String]]
+
+ {
+ // Restore statues from the metadata files
+ val existingBatchFiles = fetchAllBatchFiles()
+ if (existingBatchFiles.nonEmpty) {
+ val existingBatchIds =
existingBatchFiles.map(_.getPath.getName.toInt)
+ maxBatchId = existingBatchIds.max
+ // Recover "batchToMetadata" and "seenFiles" from existing metadata
files.
+ existingBatchIds.sorted.foreach { batchId =>
+ val files = readBatch(batchId)
+ if (files.isEmpty) {
+ // Assert that the corrupted file must be the latest metadata
file.
+ require(batchId == maxBatchId, "Invalid metadata files")
+ maxBatchId = maxBatchId - 1
+ } else {
+ batchToMetadata(batchId) = files
+ files.foreach(seenFiles.add)
+ }
+ }
+ }
+ }
+
+ /** Returns the schema of the data from this source */
+ override lazy val schema: StructType = {
+ dataSchema.getOrElse {
+ val filesPresent = fetchAllFiles()
+ if (filesPresent.isEmpty) {
+ if (providerName == "text") {
+ // Add a default schema for "text"
+ new StructType().add("value", StringType)
+ } else {
+ throw new IllegalArgumentException("No schema specified")
+ }
+ } else {
+ // There are some existing files. Use them to infer the schema
--- End diff --
nit: "." at the end.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]