python中的火花:通过使用numpy.fromfile加载二进制数据来创建rdd

Posted

技术标签:

【中文标题】python中的火花:通过使用numpy.fromfile加载二进制数据来创建rdd【英文标题】:spark in python: creating an rdd by loading binary data with numpy.fromfile 【发布时间】:2015-10-09 11:53:45 【问题描述】:

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

如果我试图操纵它,它会不断抛出错误。

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

更新:错误消息提示节点找不到原始文件(如下)。当我将所有文件复制到我的主目录时,这种方法确实非常有效。


这里是错误信息的详细信息。

例如,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$1.read(PythonRDD.scala:135)
    at org.apache.spark.api.python.PythonRDD$$anon$1.<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$1.read(PythonRDD.scala:135)
    at org.apache.spark.api.python.PythonRDD$$anon$1.<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$1.read(PythonRDD.scala:135)
    at org.apache.spark.api.python.PythonRDD$$anon$1.<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 012.\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$1.read(PythonRDD.scala:135)
at org.apache.spark.api.python.PythonRDD$$anon$1.<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$1.apply(DAGScheduler.scala:1193)
at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.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$1.apply(DAGScheduler.scala:693)
at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.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$1.run(EventLoop.scala:48)

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于python中的火花:通过使用numpy.fromfile加载二进制数据来创建rdd的主要内容,如果未能解决你的问题,请参考以下文章

创建步骤火花python,亚马逊hadoop

使用火花将每行中的值转换为新列

ImportError:运行火花时没有名为请求的模块

矩阵中的火花循环以运行线性回归

如何通过熊猫或火花数据框删除所有行中具有相同值的列?

如何将数据块中的数据从熊猫数据帧加载到火花数据帧