受此啓發question,我編寫了一些代碼來存儲RDD(從Parquet文件中讀取),其中包含(photo_id,data)的Schema(成對),並由製表符分隔,以及只是作爲一個詳細基地64編碼,就像這樣:閱讀分佈式製表符分隔的CSV
def do_pipeline(itr):
...
item_id = x.photo_id
def toTabCSVLine(data):
return '\t'.join(str(d) for d in data)
serialize_vec_b64pkl = lambda x: (x[0], base64.b64encode(cPickle.dumps(x[1])))
def format(data):
return toTabCSVLine(serialize_vec_b64pkl(data))
dataset = sqlContext.read.parquet('mydir')
lines = dataset.map(format)
lines.saveAsTextFile('outdir')
所以,現在的關注點:如何讀取數據集和打印,例如它的反序列化的數據?
我正在使用Python 2.6.6。
我的企圖就在這裏,在這裏只是證實一切可以做到的,我寫了這個代碼:
deserialize_vec_b64pkl = lambda x: (x[0], cPickle.loads(base64.b64decode(x[1])))
base64_dataset = sc.textFile('outdir')
collected_base64_dataset = base64_dataset.collect()
print(deserialize_vec_b64pkl(collected_base64_dataset[0].split('\t')))
這就要求collect(),這對於測試是確定的,但在現實世界方案將難以...
編輯:
當我試圖zero323的建議:
foo = (base64_dataset.map(str.split).map(deserialize_vec_b64pkl)).collect()
我得到這個錯誤,這歸結爲:
PythonRDD[2] at RDD at PythonRDD.scala:43
16/08/04 18:32:30 WARN TaskSetManager: Lost task 4.0 in stage 0.0 (TID 4, gsta31695.tan.ygrid.yahoo.com): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/grid/0/tmp/yarn-local/usercache/gsamaras/appcache/application_1470212406507_56888/container_e04_1470212406507_56888_01_000009/pyspark.zip/pyspark/worker.py", line 98, in main
command = pickleSer._read_with_length(infile)
File "/grid/0/tmp/yarn-local/usercache/gsamaras/appcache/application_1470212406507_56888/container_e04_1470212406507_56888_01_000009/pyspark.zip/pyspark/serializers.py", line 164, in _read_with_length
return self.loads(obj)
File "/grid/0/tmp/yarn-local/usercache/gsamaras/appcache/application_1470212406507_56888/container_e04_1470212406507_56888_01_000009/pyspark.zip/pyspark/serializers.py", line 422, in loads
return pickle.loads(obj)
UnpicklingError: NEWOBJ class argument has NULL tp_new
at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:166)
at org.apache.spark.api.python.PythonRunner$$anon$1.<init>(PythonRDD.scala:207)
at org.apache.spark.api.python.PythonRunner.compute(PythonRDD.scala:125)
at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:70)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:270)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
at org.apache.spark.scheduler.Task.run(Task.scala:89)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:227)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
16/08/04 18:32:30 ERROR TaskSetManager: Task 12 in stage 0.0 failed 4 times; aborting job
16/08/04 18:32:31 WARN TaskSetManager: Lost task 14.3 in stage 0.0 (TID 38, gsta31695.tan.ygrid.yahoo.com): TaskKilled (killed intentionally)
16/08/04 18:32:31 WARN TaskSetManager: Lost task 13.3 in stage 0.0 (TID 39, gsta31695.tan.ygrid.yahoo.com): TaskKilled (killed intentionally)
16/08/04 18:32:31 WARN TaskSetManager: Lost task 16.3 in stage 0.0 (TID 42, gsta31695.tan.ygrid.yahoo.com): TaskKilled (killed intentionally)
---------------------------------------------------------------------------
Py4JJavaError Traceback (most recent call last)
/homes/gsamaras/code/read_and_print.py in <module>()
17 print(base64_dataset.map(str.split).map(deserialize_vec_b64pkl))
18
---> 19 foo = (base64_dataset.map(str.split).map(deserialize_vec_b64pkl)).collect()
20 print(foo)
/home/gs/spark/current/python/lib/pyspark.zip/pyspark/rdd.py in collect(self)
769 """
770 with SCCallSiteSync(self.context) as css:
--> 771 port = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
772 return list(_load_from_socket(port, self._jrdd_deserializer))
773
/home/gs/spark/current/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
811 answer = self.gateway_client.send_command(command)
812 return_value = get_return_value(
--> 813 answer, self.gateway_client, self.target_id, self.name)
814
815 for temp_arg in temp_args:
/home/gs/spark/current/python/lib/py4j-0.9-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
306 raise Py4JJavaError(
307 "An error occurred while calling {0}{1}{2}.\n".
--> 308 format(target_id, ".", name), value)
309 else:
310 raise Py4JError(
Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
爲什麼'base64_dataset.map(str.split).map(deserialize_vec_b64pkl)'? – zero323
@ zero323我不知道我們可以使用'str.split',但我仍然對此感到陌生,所以請和我一起裸露,我非常肯定,如果有人解釋我將能夠相處之後..所以你提出的建議應該是RDD ..所以爲了確保一切正常,我如何查看第一個元素?我試圖「收集()」你說的,但是導致了一個錯誤('Py4JJavaError:調用z:org.apache.spark.api.python.PythonRDD.collectAndServe.'時發生錯誤)。也許它可以幫助,如果我瞭解RDD的數據佈局.. – gsamaras
@ zero323我使用Python 2,它將足以覆蓋,我的意思是從那裏我可以得到Python 3,如果需要的話! – gsamaras