【问题标题】:'list' object has no attribute 'map'“列表”对象没有属性“地图”
【发布时间】:2016-10-27 10:07:43
【问题描述】:

我知道这是因为map 是一个函数而不是列表方法。但是有没有办法可以使用 map 函数将数据传递给内部调用的函数。

这是我的代码:

def func1(lines):
    global newlst
    for line in lines:
        qtype = re.search("qtype=(\S+)",str(line))  
        ......
file = sc.textFile("C:\\TestLogs\\sample.log").cache()
result = file.map(lambda x: x.split("\n")).collect()
print(type(result)) #it is a list
lines = result.map(func1).collect() #I want to pass the contents of result to func1 through map function.

错误:

    lines = result.map(func1).collect()
AttributeError: 'list' object has no attribute 'map'

有没有其他方法可以将我的数据从results 传递到func1,但可以使用map 或火花中生成rdd 的任何概念?

【问题讨论】:

  • 不要.collect()

标签: apache-spark pyspark rdd


【解决方案1】:

问题在于您调用 collect 将 RDD 的结果存储在这里:

result = file.map(lambda x: x.split("\n")).collect()

此命令将返回给您一个列表,而不是 RDD。 如果您从该行中删除 collect(),如下所示:

result = file.map(lambda x: x.split("\n"))

这会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多