I'm trying to use spark to implement some algorithm on provenance. The first step I want to do is store prov document objects into spark.
text = spark.read.text("./282.json")
rdd = text.rdd.map(lambda x: ProvDocument.deserialize(content=x))
print(rdd.take(1))
The JSON file is simple a prov-JSON file and it works as expected in the local environment. And this gives me following error:
21/12/10 11:33:16 ERROR Executor: Exception in task 0.0 in stage 58.0 (TID 56)
org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/sql/types.py", line 1573, in __getattr__
idx = self.__fields__.index(item)
ValueError: 'decode' is not in list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 619, in main
process()
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 609, in process
out_iter = func(split_index, iterator)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
[Previous line repeated 1 more time]
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 417, in func
return f(iterator)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 916, in processPartition
for x in iterator:
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/util.py", line 74, in wrapper
show more (open the raw output data in a text editor) ...
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
21/12/10 11:33:16 ERROR TaskSetManager: Task 0 in stage 58.0 failed 1 times; aborting job
Py4JJavaError Traceback (most recent call last)
/var/folders/2b/br_h_zhx20z2l8fj98qxp9lr0000gn/T/ipykernel_30931/2320541898.py in <module>
1 rdd = text.rdd.map(lambda x: ProvDocument.deserialize(content=x))
----> 2 rdd.foreach(lambda x: ProvDocument.get_provn(x))
~/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py in foreach(self, f)
917 f(x)
918 return iter([])
--> 919 self.mapPartitions(processPartition).count() # Force evaluation
920
921 def foreachPartition(self, f):
~/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py in count(self)
1235 3
1236 """
-> 1237 return self.mapPartitions(lambda i: [sum(1 for _ in i)]).sum()
1238
1239 def stats(self):
~/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py in sum(self)
1224 6.0
1225 """
-> 1226 return self.mapPartitions(lambda x: [sum(x)]).fold(0, operator.add)
1227
1228 def count(self):
~/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py in fold(self, zeroValue, op)
1078 # zeroValue provided to each partition is unique from the one provided
1079 # to the final reduce call
-> 1080 vals = self.mapPartitions(func).collect()
1081 return reduce(op, vals, zeroValue)
1082
~/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py in collect(self)
948 """
949 with SCCallSiteSync(self.context) as css:
--> 950 sock_info = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
951 return list(_load_from_socket(sock_info, self._jrdd_deserializer))
952
~/Library/Python/3.8/lib/python/site-packages/py4j/java_gateway.py in __call__(self, *args)
1307
1308 answer = self.gateway_client.send_command(command)
-> 1309 return_value = get_return_value(
1310 answer, self.gateway_client, self.target_id, self.name)
1311
~/Library/Python/3.8/lib/python/site-packages/pyspark/sql/utils.py in deco(*a, **kw)
109 def deco(*a, **kw):
110 try:
--> 111 return f(*a, **kw)
112 except py4j.protocol.Py4JJavaError as e:
113 converted = convert_exception(e.java_exception)
~/Library/Python/3.8/lib/python/site-packages/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
324 value = OUTPUT_CONVERTER[type](answer[2:], gateway_client)
325 if answer[1] == REFERENCE_TYPE:
--> 326 raise Py4JJavaError(
327 "An error occurred while calling {0}{1}{2}.\n".
328 format(target_id, ".", name), value)
Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 58.0 failed 1 times, most recent failure: Lost task 0.0 in stage 58.0 (TID 56) (usernamedembp executor driver): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/sql/types.py", line 1573, in __getattr__
idx = self.__fields__.index(item)
ValueError: 'decode' is not in list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 619, in main
process()
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 609, in process
out_iter = func(split_index, iterator)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
[Previous line repeated 1 more time]
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 417, in func
return f(iterator)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 916, in processPartition
for x in iterator:
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/util.py", line 74, in wrapper
return f(*args, **kwargs)
File "/var/folders/2b/br_h_zhx20z2l8fj98qxp9lr0000gn/T/ipykernel_30931/2320541898.py", line 1, in <lambda>
File "/Users/username/Library/Python/3.8/lib/python/site-packages/prov/model.py", line 2527, in deserialize
content if isinstance(content, str) else content.decode()
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/sql/types.py", line 1578, in __getattr__
raise AttributeError(item)
AttributeError: decode
at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:545)
at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:703)
at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:685)
at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:498)
at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
at scala.collection.Iterator.foreach(Iterator.scala:943)
at scala.collection.Iterator.foreach$(Iterator.scala:943)
at org.apache.spark.InterruptibleIterator.foreach(InterruptibleIterator.scala:28)
at scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
at scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
at scala.collection.TraversableOnce.to(TraversableOnce.scala:366)
at scala.collection.TraversableOnce.to$(TraversableOnce.scala:364)
at org.apache.spark.InterruptibleIterator.to(InterruptibleIterator.scala:28)
at scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:358)
at scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:358)
at org.apache.spark.InterruptibleIterator.toBuffer(InterruptibleIterator.scala:28)
at scala.collection.TraversableOnce.toArray(TraversableOnce.scala:345)
at scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:339)
at org.apache.spark.InterruptibleIterator.toArray(InterruptibleIterator.scala:28)
at org.apache.spark.rdd.RDD.$anonfun$collect$2(RDD.scala:1030)
at org.apache.spark.SparkContext.$anonfun$runJob$5(SparkContext.scala:2254)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
at org.apache.spark.scheduler.Task.run(Task.scala:131)
at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Driver stacktrace:
at org.apache.spark.scheduler.DAGScheduler.failJobAndIndependentStages(DAGScheduler.scala:2403)
at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2(DAGScheduler.scala:2352)
at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2$adapted(DAGScheduler.scala:2351)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:2351)
at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1(DAGScheduler.scala:1109)
at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1$adapted(DAGScheduler.scala:1109)
at scala.Option.foreach(Option.scala:407)
at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:1109)
at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2591)
at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2533)
at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2522)
at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:49)
at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:898)
at org.apache.spark.SparkContext.runJob(SparkContext.scala:2214)
at org.apache.spark.SparkContext.runJob(SparkContext.scala:2235)
at org.apache.spark.SparkContext.runJob(SparkContext.scala:2254)
at org.apache.spark.SparkContext.runJob(SparkContext.scala:2279)
at org.apache.spark.rdd.RDD.$anonfun$collect$1(RDD.scala:1030)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
at org.apache.spark.rdd.RDD.withScope(RDD.scala:414)
at org.apache.spark.rdd.RDD.collect(RDD.scala:1029)
at org.apache.spark.api.python.PythonRDD$.collectAndServe(PythonRDD.scala:180)
at org.apache.spark.api.python.PythonRDD.collectAndServe(PythonRDD.scala)
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.ClientServerConnection.waitForCommands(ClientServerConnection.java:182)
at py4j.ClientServerConnection.run(ClientServerConnection.java:106)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/sql/types.py", line 1573, in __getattr__
idx = self.__fields__.index(item)
ValueError: 'decode' is not in list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 619, in main
process()
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 609, in process
out_iter = func(split_index, iterator)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 2918, in pipeline_func
return func(split, prev_func(split, iterator))
[Previous line repeated 1 more time]
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 417, in func
return f(iterator)
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/rdd.py", line 916, in processPartition
for x in iterator:
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/util.py", line 74, in wrapper
return f(*args, **kwargs)
File "/var/folders/2b/br_h_zhx20z2l8fj98qxp9lr0000gn/T/ipykernel_30931/2320541898.py", line 1, in <lambda>
File "/Users/username/Library/Python/3.8/lib/python/site-packages/prov/model.py", line 2527, in deserialize
content if isinstance(content, str) else content.decode()
File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyspark/python/lib/pyspark.zip/pyspark/sql/types.py", line 1578, in __getattr__
raise AttributeError(item)
AttributeError: decode
at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:545)
at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:703)
at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:685)
at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:498)
at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
at scala.collection.Iterator.foreach(Iterator.scala:943)
at scala.collection.Iterator.foreach$(Iterator.scala:943)
at org.apache.spark.InterruptibleIterator.foreach(InterruptibleIterator.scala:28)
at scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
at scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
at scala.collection.TraversableOnce.to(TraversableOnce.scala:366)
at scala.collection.TraversableOnce.to$(TraversableOnce.scala:364)
at org.apache.spark.InterruptibleIterator.to(InterruptibleIterator.scala:28)
at scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:358)
at scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:358)
at org.apache.spark.InterruptibleIterator.toBuffer(InterruptibleIterator.scala:28)
at scala.collection.TraversableOnce.toArray(TraversableOnce.scala:345)
at scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:339)
at org.apache.spark.InterruptibleIterator.toArray(InterruptibleIterator.scala:28)
at org.apache.spark.rdd.RDD.$anonfun$collect$2(RDD.scala:1030)
at org.apache.spark.SparkContext.$anonfun$runJob$5(SparkContext.scala:2254)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
at org.apache.spark.scheduler.Task.run(Task.scala:131)
at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:506)
at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1462)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:509)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
... 1 more
So my question is: is object operation possible in pyspark? If not, am I suggested to use java or scala?
CodePudding user response:
RDDs are exactly for manipulating Python objects. I think your problem is that each row of your data frame contains a single line from the json file. Try to read the complete file directly to an RDD:
file_and_path_rdd = spark.sparkContext.wholeTextFiles("./282.json") # use "./*.json" to read all json files in the dir
only_file_rdd = rdd.map(lambda x: x[1]) # discard the path part of the tupple
rdd = only_file_rdd.map(lambda x: ProvDocument.deserialize(content=x)) # Deserialize all files loaded
print(rdd.take(1)) # Print out one deserialized object
The rdd file_and_path_rdd
contains (path, filecontent) tuples. The first map discards the path part and returns the filecontent. Then you are ready to deserialize the objects with the next map.