使用pyspark将图像作为序列文件的值写入

svmlkihl  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(327)

我用pyspark写序列文件,键是image filename,值是用bytestring表示的图像

from PIL import Image

def get_image(filename):
 s = StringIO()
 im=io.imread(filename)
 io.imsave(s, im)
 return [(filename, s)]

rdd  =  sc.parallelize(filenames)
rdd.flatMap(get_image).saveAsSequenceFile("/user/myname/output")

但是pyspark抛出一个异常,表明pickle不支持该格式

Caused by: net.razorvine.pickle.InvalidOpcodeException: opcode not implemented: OBJ
    at net.razorvine.pickle.Unpickler.dispatch(Unpickler.java:224)
    at net.razorvine.pickle.Unpickler.load(Unpickler.java:85)
    at net.razorvine.pickle.Unpickler.loads(Unpickler.java:98)
    at org.apache.spark.api.python.SerDeUtil$$anonfun$pythonToJava$1$$anonfun$apply$1.apply(SerDeUtil.scala:151)
    at org.apache.spark.api.python.SerDeUtil$$anonfun$pythonToJava$1$$anonfun$apply$1.apply(SerDeUtil.scala:150)
    at scala.collection.Iterator$$anon$13.hasNext(Iterator.scala:371)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:308)
    at scala.collection.Iterator$class.foreach(Iterator.scala:727)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:47)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:273)
    at scala.collection.AbstractIterator.to(Iterator.scala:1157)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:265)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1157)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:252)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1157)
    at org.apache.spark.rdd.RDD$$anonfun$take$1$$anonfun$28.apply(RDD.scala:1298)
    at org.apache.spark.rdd.RDD$$anonfun$take$1$$anonfun$28.apply(RDD.scala:1298)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1850)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1850)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
    at org.apache.spark.scheduler.Task.run(Task.scala:88)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    ... 1 more
s4n0splo

s4n0splo1#

pickling的obj操作码在您试图对python类/对象进行编码/解码以进行序列化时使用。在我的例子中,我并不打算将一个对象写入序列文件,所以对我的修复只是修复这个bug。
至于整个生态系统,问题是spark使用的是pyrolite 4.13,但直到4.17版本,obj编码/解码才被引入pyrolite库。至于该怎么办,我想你有几个选择:
通过pull请求或github问题说服spark维护人员使用pyrolite的更高版本。
建立你自己的Spark版本,使用该版本的辉石
不要将类/对象写入序列文件。

相关问题