python 中的 spark:通过使用 numpy.fromfile 加载二进制数据创建一个 rdd

spark in python: creating an rdd by loading binary data with numpy.fromfile

spark python api 目前对加载大型二进制数据文件的支持有限,所以我试图让 numpy.fromfile 帮助我。

我首先得到了我想要加载的文件名列表,例如:

In [9] filenames
Out[9]: 
['A0000.dat',
 'A0001.dat',
 'A0002.dat',
 'A0003.dat',
 'A0004.dat']

我可以通过粗略的迭代联合加载这些文件而不会出现问题,

for i in range(len(filenames)):
    rdd = sc.parallelize([np.fromfile(filenames[i], dtype="int16", count=-1, sep='')])
    if i==0:
        allRdd = rdd;
    else:
        allRdd = allRdd.union(rdd);

一次加载所有文件并加载到多个节点中会很棒。我尝试按如下方式执行此操作,

filenameRdd = sc.parallelize(filenames)
allRdd2 = filenameRdd.map(lambda x: np.fromfile(x, dtype="int16", count=-1, sep=''))

但这并没有奏效。我找回了一些 RDD

In [20]: allRdd2
Out[20]: PythonRDD[13] at RDD at PythonRDD.scala:43

如果我尝试操作它,它会不断抛出错误。

我的方法理论上可行吗? 如果没有,什么是好的选择?

Update: 报错提示节点找不到原文件(下图)。事实上,当我将所有文件复制到我的主目录时,这种方法非常有效。


以下是错误消息的详细信息。

例如,collect() 使用第一种方法,

allRdd.collect()

[array([87, 52, 82, ..., 96, 25, 20], dtype=int16),
 array([20, 72, 13, ..., 53, 41, 99], dtype=int16),
 array([97, 63, 17, ..., 38, 89, 13], dtype=int16),
 array([88, 66, 97, ..., 22, 93, 93], dtype=int16),
 array([99, 14, 42, ..., 33, 34, 20], dtype=int16)]

但是第二种方法不行,

allRdd2.collect()

15/10/09 08:21:58 ERROR Executor: Exception in task 12.0 in stage 4.0 (TID 113)
org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/usr/local/spark-current/python/pyspark/worker.py", line 101, in main
    process()
  File "/usr/local/spark-current/python/pyspark/worker.py", line 96, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "/usr/local/spark-current/python/pyspark/serializers.py", line 236, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-6-58733c66cd22>", line 3, in <lambda>
IOError: [Errno 2] No such file or directory: 'A0003.dat'

    at org.apache.spark.api.python.PythonRDD$$anon.read(PythonRDD.scala:135)
    at org.apache.spark.api.python.PythonRDD$$anon.<init>(PythonRDD.scala:176)
    at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:94)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:61)
    at org.apache.spark.scheduler.Task.run(Task.scala:64)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:203)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
15/10/09 08:21:58 ERROR Executor: Exception in task 9.0 in stage 4.0 (TID 110)
org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/usr/local/spark-current/python/pyspark/worker.py", line 101, in main
    process()
  File "/usr/local/spark-current/python/pyspark/worker.py", line 96, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "/usr/local/spark-current/python/pyspark/serializers.py", line 236, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-6-58733c66cd22>", line 3, in <lambda>
IOError: [Errno 2] No such file or directory: 'A0002.dat'

    at org.apache.spark.api.python.PythonRDD$$anon.read(PythonRDD.scala:135)
    at org.apache.spark.api.python.PythonRDD$$anon.<init>(PythonRDD.scala:176)
    at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:94)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:61)
    at org.apache.spark.scheduler.Task.run(Task.scala:64)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:203)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
15/10/09 08:21:58 ERROR Executor: Exception in task 6.0 in stage 4.0 (TID 107)
org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/usr/local/spark-current/python/pyspark/worker.py", line 101, in main
    process()
  File "/usr/local/spark-current/python/pyspark/worker.py", line 96, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "/usr/local/spark-current/python/pyspark/serializers.py", line 236, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-6-58733c66cd22>", line 3, in <lambda>
IOError: [Errno 2] No such file or directory: 'A0001.dat'

    at org.apache.spark.api.python.PythonRDD$$anon.read(PythonRDD.scala:135)
    at org.apache.spark.api.python.PythonRDD$$anon.<init>(PythonRDD.scala:176)
    at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:94)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:61)
    at org.apache.spark.scheduler.Task.run(Task.scala:64)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:203)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
15/10/09 08:21:58 ERROR TaskSetManager: Task 12 in stage 4.0 failed 1 times; aborting job
---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
<ipython-input-11-16f16ee2a9b8> in <module>()
----> 1 allRdd2.collect()

/usr/local/spark-current/python/pyspark/rdd.py in collect(self)
    711         """
    712         with SCCallSiteSync(self.context) as css:
--> 713             port = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
    714         return list(_load_from_socket(port, self._jrdd_deserializer))
    715 

/usr/local/spark-current/python/lib/py4j-0.8.2.1-src.zip/py4j/java_gateway.py in __call__(self, *args)
    536         answer = self.gateway_client.send_command(command)
    537         return_value = get_return_value(answer, self.gateway_client,
--> 538                 self.target_id, self.name)
    539 
    540         for temp_arg in temp_args:

/usr/local/spark-current/python/lib/py4j-0.8.2.1-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    298                 raise Py4JJavaError(
    299                     'An error occurred while calling {0}{1}{2}.\n'.
--> 300                     format(target_id, '.', name), value)
    301             else:
    302                 raise Py4JError(

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 12 in stage 4.0 failed 1 times, most recent failure: Lost task 12.0 in stage 4.0 (TID 113, localhost): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/usr/local/spark-current/python/pyspark/worker.py", line 101, in main
    process()
  File "/usr/local/spark-current/python/pyspark/worker.py", line 96, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "/usr/local/spark-current/python/pyspark/serializers.py", line 236, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-6-58733c66cd22>", line 3, in <lambda>
IOError: [Errno 2] No such file or directory: 'A0003.dat'

at org.apache.spark.api.python.PythonRDD$$anon.read(PythonRDD.scala:135)
at org.apache.spark.api.python.PythonRDD$$anon.<init>(PythonRDD.scala:176)
at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:94)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:61)
at org.apache.spark.scheduler.Task.run(Task.scala:64)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:203)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:
at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1204)
at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage.apply(DAGScheduler.scala:1193)
at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage.apply(DAGScheduler.scala:1192)
at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1192)
at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed.apply(DAGScheduler.scala:693)
at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed.apply(DAGScheduler.scala:693)
at scala.Option.foreach(Option.scala:236)
at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:693)
at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1393)
at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1354)
at org.apache.spark.util.EventLoop$$anon.run(EventLoop.scala:48)

解决方案非常简单:当您提供文件名的完整路径时,此方法有效。

fullfilenames = [fullpath + '/' + fname for fname in filenames]
filenameRdd = sc.parallelize(fullfilenames)
allRdd2 = filenameRdd.map(lambda x: np.fromfile(x, dtype="int16", count=-1, sep=''))