Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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

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在列上应用自定义函数_Python_Apache Spark_Dataframe_Pyspark_Spark Dataframe - Fatal编程技术网

Python PySpark在列上应用自定义函数

Python PySpark在列上应用自定义函数,python,apache-spark,dataframe,pyspark,spark-dataframe,Python,Apache Spark,Dataframe,Pyspark,Spark Dataframe,我想在dataframe列上运行自定义函数。该列有一个长字符串,其中包含一些电子邮件。字符串格式如下所示: "Don Joe<abc@bankatfirst.com>, Matt Scheurer <def@bankatfirst.com>, Dan Lawler <ghi@bankatfirst.com>" all_names = set() def get_distinct_users(userlist): global all_names

我想在dataframe列上运行自定义函数。该列有一个长字符串,其中包含一些电子邮件。字符串格式如下所示:

"Don Joe<abc@bankatfirst.com>,  Matt Scheurer <def@bankatfirst.com>, Dan Lawler <ghi@bankatfirst.com>"
all_names = set()

def get_distinct_users(userlist):
    global all_names
    for email in re.findall('\<\S*\>',userlist):
        all_names.add(email)

get_distinct_users_udf = udf(get_distinct_users,StringType())
users = users.withColumn("user_count",get_distinct_users_udf(users["users"]))
“唐·乔、马特·舍勒、丹·劳勒”
我必须运行regex来提取电子邮件,然后我必须找到整个专栏中有多少独特的电子邮件

我可以用python编写正则表达式并创建唯一的电子邮件列表。但是我不知道如何在spark数据帧上应用这个函数。我试过这样做:

"Don Joe<abc@bankatfirst.com>,  Matt Scheurer <def@bankatfirst.com>, Dan Lawler <ghi@bankatfirst.com>"
all_names = set()

def get_distinct_users(userlist):
    global all_names
    for email in re.findall('\<\S*\>',userlist):
        all_names.add(email)

get_distinct_users_udf = udf(get_distinct_users,StringType())
users = users.withColumn("user_count",get_distinct_users_udf(users["users"]))
all_name=set()
def get_distinct_用户(用户列表):
全局所有名称
对于re.findall('\',userlist)中的电子邮件:
所有_名称。添加(电子邮件)
get\u distinct\u users\u udf=udf(get\u distinct\u users,StringType())
users=users.withColumn(“user\u count”,获取不同的用户\u udf(users[“users”]))
但是gloabl变量all_name没有得到更新。我应该应用map函数而不是创建UDF,还是应该使用reduce,因为它是一种聚合函数


有什么想法吗?

一种方法是使用
flatMap
函数提取列上的电子邮件地址列表,例如

import re

def get_email(x):
  return re.findall("\<\S*\>", x)

uniqueEmails = users.select("users").rdd\
  .flatMap(lambda x: get_email(x[0]))\
  .distinct()
重新导入
def get_电子邮件(x):
返回关于findall(“\”,x)
uniqueEmails=users.select(“users”).rdd\
.flatMap(lambda x:get_电子邮件(x[0]))\
.distinct()

将是不同电子邮件地址的RDD

这当然行不通。每个执行人都有自己的
所有_名称的副本
;其他执行者无法访问它…如果我将
所有的\u名称
设置为累加器会怎么样?只有数字类型的累加器,但您可以自己设置。