我是 Python 新手。我也是 pysaprk 的新手。我正在尝试运行采用 (kv[0], kv[1]) 的代码行,然后在 kv[1] 上运行 ngrams() 函数。
这里还有代码处理的 mentions 数据的示例布局:
Out[12]:
[{'_id': u'en.wikipedia.org/wiki/Kamchatka_Peninsula',
'source': 'en.wikipedia.org/wiki/Warthead_sculpin',
'span': (100, 119),
'text': u' It is native to the northern.'},
{'_id': u'en.wikipedia.org/wiki/Warthead_sculpin',
'source': 'en.wikipedia.org/wiki/Warthead_sculpin',
'span': (4, 20),
'text': u'The warthead sculpin ("Myoxocephalus niger").'}]
这是我正在使用的代码:
def build(self, mentions, idfs):
m = mentions\
.map(lambda (source, target, span, text): (target, text))
.flatMapValues(lambda v: ngrams(v, self.max_ngram))
.map(lambda v: (v, 1))
.reduceByKey(add)\
应该如何制定上一步的数据来解决这个错误? 任何帮助或指导将不胜感激。
我正在使用 python 2.7 和 pyspark 2.3.0。
谢谢,
请您参考如下方法:
mapValues 只能应用于 (key, value) 对的 RDD(RDD,其中每个元素都是一个 元组 等于 2,或者某些行为与 1 相同的对象 - How to determine if object is a valid key-value pair in PySpark )length 的
您的数据是字典,因此不符合条件。不清楚你在那里期望什么,但你怀疑你想要:
from operator import itemgetter
(mentions
.map(itemgetter("_id", "text"))
.flatMapValues(lambda v: ngrams(v, self.max_ngram))
.map(lambda v: (v, 1)))
