我遇到了此鏈接Spark Scala Array of String lines to pairRDD,有人可以幫助我如何在 Pyspark 中做到這一點嗎?
由于我是 PySpark 的新手,有人可以幫助我了解如何在 PySpark 中撰寫以下代碼嗎?
rdd.map(_.split(", "))
.flatMap(x => x.tail.grouped(2).map(y => (x.head, y.head)))
uj5u.com熱心網友回復:
雖然您可以在 python 代碼下面的鏈接中閱讀對 scala 代碼的解釋,但將為您提供相同的輸出
cq="""Row-Key-001, K1, 10, A2, 20, K3, 30, B4, 42, K5, 19, C20, 20
Row-Key-002, X1, 20, Y6, 10, Z15, 35, X16, 42
Row-Key-003, L4, 30, M10, 5, N12, 38, O14, 41, P13, 8"""
with open("path of file","w") as r:
r.write(cq) # writing data into file
r.close()
v=sc.textFile("path of file") # reading above data as RDD
v.map(lambda x : x.split(", ")).flatMap(lambda x:[(x[0],a) for a in x[1:]]).collect()
#output
[('Row-Key-001', 'K1'),
('Row-Key-001', '10'),
('Row-Key-001', 'A2'),
('Row-Key-001', '20'),
('Row-Key-001', 'K3'),
('Row-Key-001', '30'),
('Row-Key-001', 'B4'),
('Row-Key-001', '42'),
('Row-Key-001', 'K5'),
('Row-Key-001', '19'),
('Row-Key-001', 'C20'),
('Row-Key-001', '20'),
('Row-Key-002', 'X1'),
('Row-Key-002', '20'),
('Row-Key-002', 'Y6'),
('Row-Key-002', '10'),
('Row-Key-002', 'Z15'),
('Row-Key-002', '35'),
('Row-Key-002', 'X16'),
('Row-Key-002', '42'),
('Row-Key-003', 'L4'),
('Row-Key-003', '30'),
('Row-Key-003', 'M10'),
('Row-Key-003', '5'),
('Row-Key-003', 'N12'),
('Row-Key-003', '38'),
('Row-Key-003', 'O14'),
('Row-Key-003', '41'),
('Row-Key-003', 'P13'),
('Row-Key-003', '8')]
首先,我使用 split 方法為每一行創建一個串列
然后我將該串列的第一個元素映射到所有其他元素并使用 flatmap 來擁有單數串列而不是子串列
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/488050.html
