Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在PySpark中,如何向函数发送RDD以比较一个值并返回另一个RDD?_Python_Apache Spark_Pyspark - Fatal编程技术网

Python 在PySpark中,如何向函数发送RDD以比较一个值并返回另一个RDD?

Python 在PySpark中,如何向函数发送RDD以比较一个值并返回另一个RDD?,python,apache-spark,pyspark,Python,Apache Spark,Pyspark,假设我们有下一个样本数据: 1,John,Martinez,North Lauderdale,20160101,1 2,John,Martinez,Plantation,20170101,2 3,John,Martinez,North Lauderdale,20161022,1 4,John,Martinez,Pembroke Pines,20181231,0 5,John,Martinez,Plantation,20190101,3 6,John,Martinez,Plantation,202

假设我们有下一个样本数据:

1,John,Martinez,North Lauderdale,20160101,1
2,John,Martinez,Plantation,20170101,2
3,John,Martinez,North Lauderdale,20161022,1
4,John,Martinez,Pembroke Pines,20181231,0
5,John,Martinez,Plantation,20190101,3
6,John,Martinez,Plantation,20200101,1
7,John,Martinez,Plantation,20210101,9
我想检查示例文件中每一行的最后一个值,例如1、2、3、0、3、1、9

def func(input):
    if str(input[5]) is "1":
        rdd_trdln = input.map(lambda line: (line, "A"))
    else:
        rdd_trdln = input.map(lambda line: (line, "O"))
        return rdd_trdln
input = sc.textFile("file.txt").map(lambda line: line.split('\t'))
return_FirstFunc = input.map(firstFunc)
我得到的错误是:

AttributeError:“列表”对象没有属性“映射”


从纯Python的角度来看,如果希望映射到标准列表,可以使用:


请注意,Python3会生成不同的结果类型,将迭代器映射到Python2列表。

Spark RDD.map和常规Python map函数之间存在差异

当您有sc.textFilefile.txt.maplambda line:line.split'\t'时,您创建了Python列表的RDD。因此,当您调用input.mapfunc时,func需要接受列表,而不是RDD

因此,input.map是您的错误

“列表”对象没有属性“映射”

这是Python错误,不是Spark错误

如果您只想在列表中添加一个字符,那么您的代码将是

def func(input):
    if input[5] == "1":
        input.append("A")
    else:
        input.append("O")
    return input
或者,更像蟒蛇

def func(input):
    input.append("A" if input[5] == "1" else "O")
    return input
或者您可以定义函数,将整行作为字符串并在其中拆分。 有一个RDD的列表会让工作变得混乱,有时很难记住

def convert_func(line):
    """
    This is not returning an RDD. It returns a Python string
    """
    splits = line.split(',') # Your lines are not tab-delimited
    splits.append("A" if splits[5] == "1" else "O")
    return ",".join(splits)

lines = sc.textFile("file.txt")
converted_lines = lines.map(convert_func)
你可以这样测试

for line in converted_lines.collect():
    print(line)

但是如果我有多个步骤的多个函数。。。我必须在每个函数中进行拆分吗?不清楚您想要什么输出,但目前您有一个RDD行,A/0对。如果您需要从行中提取数据,是的,您需要再次拆分。无论如何,我可以发送RDD或已拆分的数据吗?当然可以。返回拆分而不是返回行。再一次如果您的问题在预期输出中更清楚,我将编辑我的答案此代码有效。。。。非常感谢@cricket\u 007。。。。我已经将此标记为我问题的解决方案。。。我不能投赞成票,因为我的名声还不到15。。。但这是答案。。。。再次非常感谢!!!!sc.textFilefile.txt不像普通列表那样是Python可移植文件。我认为这样做行不通。这里有一条建议,请努力阅读官方文档中的快速入门指南。这个问题缺乏研究,非常糟糕。对不起,这是给@eliasah的
for line in converted_lines.collect():
    print(line)