2

I am using a user-defined function (readByteUFF) to read file, perform transform the content and return a pyspark.sql Row.

I use this function on an rdd (which is a large collection of files that should follow the same pattern) in the following setup:

mapped_rdd = rdd.map(readByteUFF)

df = mapped_rdd.toDF()

df.write.format("delta").mode("append").option("mergeSchema", "true").option("path", "dbfs:/mnt/").saveAsTable("table")

This works well when the files follow the same pattern, but in situations where the the function returns an error or a None the entire thing throws me

org.apache.spark.SparkException: Job aborted.

---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
<command-3680885025756706> in <module>
      8 
      9 #df.createOrReplaceTempView("table")
---> 10 df.write.format("delta").mode("append").option("mergeSchema", "true").option("path", "dbfs:/mnt/").saveAsTable("table")


/databricks/spark/python/pyspark/sql/readwriter.py in saveAsTable(self, name, format, mode, partitionBy, **options)
    775         if format is not None:
    776             self.format(format)
--> 777         self._jwrite.saveAsTable(name)
    778 
    779     @since(1.4)

/databricks/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py in __call__(self, *args)
   1255         answer = self.gateway_client.send_command(command)
   1256         return_value = get_return_value(
-> 1257             answer, self.gateway_client, self.target_id, self.name)
   1258 
   1259         for temp_arg in temp_args:

/databricks/spark/python/pyspark/sql/utils.py in deco(*a, **kw)
     61     def deco(*a, **kw):
     62         try:
---> 63             return f(*a, **kw)
     64         except py4j.protocol.Py4JJavaError as e:
     65             s = e.java_exception.toString()

/databricks/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
--> 328                     format(target_id, ".", name), value)
    329             else:
    330                 raise Py4JError(

I would like to be able to flag the files which cause problems while processing the remaining files. Is there a way to handle this in the flatMap()-functionality?

Martin Petri Bagger
  • 2,187
  • 4
  • 17
  • 20

0 Answers0