访问RDD pyspark中的元组

时间:2018-09-30 09:06:07

标签: pyspark

输入:

('MechanicalKeyboards', 2, 'ForgetfulDoryFish')
('MechanicalKeyboards', 1, 'cheshire26')
('MechanicalKeyboards', 1, 'Sygaldry')
('scala', 5, 'hyperforce')
('xkcd', 3, 'brinjal66')
('MechanicalKeyboards', 1, 'Sygaldry')
('MechanicalKeyboards', 1, 'DzyDzyDino')

这是我的RDD。

With-e = lines.filter(lambda x: 'e' in lines[0])

所需的输出:

('MechanicalKeyboards', 2, 'ForgetfulDoryFish')
('MechanicalKeyboards', 1, 'cheshire26')
('MechanicalKeyboards', 1, 'Sygaldry')
('MechanicalKeyboards', 1, 'Sygaldry')
('MechanicalKeyboards', 1, 'DzyDzyDino')

我正在尝试过滤掉RDD元组的第一个元素中不包含'e'的所有元素,这可能吗?

1 个答案:

答案 0 :(得分:0)

我想你可以做到以下

>>> rdd = sc.parallelize([
...     ('MechanicalKeyboards', 2, 'ForgetfulDoryFish'),
...     ('MechanicalKeyboards', 1, 'cheshire26'),
...     ('MechanicalKeyboards', 1, 'Sygaldry'),
...     ('scala', 5, 'hyperforce'),
...     ('xkcd', 3, 'brinjal66'),
...     ('MechanicalKeyboards', 1, 'Sygaldry'),
...     ('MechanicalKeyboards', 1, 'DzyDzyDino')
...     ])
>>> 
>>> rdd.filter(lambda x: True if 'e' in x[0] else False).collect()
[('MechanicalKeyboards', 2, 'ForgetfulDoryFish'), ('MechanicalKeyboards', 1, 'cheshire26'), ('MechanicalKeyboards', 1, 'Sygaldry'),     ('MechanicalKeyboards', 1, 'Sygaldry'), ('MechanicalKeyboards', 1, 'DzyDzyDino')]