如何使用Python在Spark中使用键计算记录数?
我的数据显示了一些单词对以及这对单词出现的次数。例如:如何使用Python在Spark中使用键计算记录数?,python,apache-spark,pyspark,Python,Apache Spark,Pyspark,我的数据显示了一些单词对以及这对单词出现的次数。例如: [("('best', 'it')", 3), ("('best', 'of')", 4), ("('best', 'the')", 3), ("('best', 'was')", 3), ("('it', 'of')", 11), ("('it', 'the')", 11)] 我的目标是计算一个单词,它存在多少对。例如,我想得到: best 4 it 3 一件棘手的事情是,“它”不仅发生在 ("('it', 'of')", 11),
[("('best', 'it')", 3), ("('best', 'of')", 4), ("('best', 'the')", 3), ("('best', 'was')", 3), ("('it', 'of')", 11), ("('it', 'the')", 11)]
我的目标是计算一个单词,它存在多少对。例如,我想得到:
best 4
it 3
一件棘手的事情是,“它”不仅发生在
("('it', 'of')", 11), ("('it', 'the')", 11)
('best', 'it')", 3)
但也发生在
("('it', 'of')", 11), ("('it', 'the')", 11)
('best', 'it')", 3)
因此,程序需要以某种方式确定这一点
我应该如何使用Python在Spark中实现这一点?我是新手,非常感谢你的帮助 首先,从数据创建pyspark数据帧
df = sql.createDataFrame(
[("('best', 'it')", 3),\
("('best', 'of')", 4),\
("('best', 'the')", 3),\
("('best', 'was')", 3),\
("('it', 'of')", 11),\
("('it', 'the')", 11)],
['text', 'count'])
df.show()
+---------------+-----+
| text|count|
+---------------+-----+
| ('best', 'it')| 3|
| ('best', 'of')| 4|
|('best', 'the')| 3|
|('best', 'was')| 3|
| ('it', 'of')| 11|
| ('it', 'the')| 11|
+---------------+-----+
然后,转换数组中文本的字符串
,分解文本
和分组依据
import pyspark.sql.functions as F
import ast
convert_udf = F.udf(lambda x: ast.literal_eval(x), ArrayType(StringType()) )
df = df.withColumn('text', convert_udf('text'))\
.withColumn('text', F.explode('text'))\
.groupby('text').count()
df.show()
+----+-----+
|text|count|
+----+-----+
| was| 1|
| it| 3|
| the| 2|
| of| 2|
|best| 4|
+----+-----+
首先,从数据创建pyspark dataframe
df = sql.createDataFrame(
[("('best', 'it')", 3),\
("('best', 'of')", 4),\
("('best', 'the')", 3),\
("('best', 'was')", 3),\
("('it', 'of')", 11),\
("('it', 'the')", 11)],
['text', 'count'])
df.show()
+---------------+-----+
| text|count|
+---------------+-----+
| ('best', 'it')| 3|
| ('best', 'of')| 4|
|('best', 'the')| 3|
|('best', 'was')| 3|
| ('it', 'of')| 11|
| ('it', 'the')| 11|
+---------------+-----+
然后,转换数组中文本的字符串
,分解文本
和分组依据
import pyspark.sql.functions as F
import ast
convert_udf = F.udf(lambda x: ast.literal_eval(x), ArrayType(StringType()) )
df = df.withColumn('text', convert_udf('text'))\
.withColumn('text', F.explode('text'))\
.groupby('text').count()
df.show()
+----+-----+
|text|count|
+----+-----+
| was| 1|
| it| 3|
| the| 2|
| of| 2|
|best| 4|
+----+-----+
如果您使用的是RDD,那么在这种情况下可以使用reduceByKey
>>> rdd.collect()
[("('best', 'it')", 3), ("('best', 'of')", 4), ("('best', 'the')", 3), ("('best', 'was')", 3), ("('it', 'of')", 11), ("('it', 'the')", 11)]
>>> rddMap = rdd.map(lambda x: x[0][1:-1].split(',')).flatMap(lambda x: [(i.replace("'","").strip(),1) for i in x])
>>> rddMap.collect()
[('best', 1), ('it', 1), ('best', 1), ('of', 1), ('best', 1), ('the', 1), ('best', 1), ('was', 1), ('it', 1), ('of', 1), ('it', 1), ('the', 1)]
>>> rddReduce = rddMap.reduceByKey(lambda x,y: x+y).map(lambda x: x[0]+','+str(x[1]))
>>> for i in rddReduce.collect(): print(i)
...
best,4
it,3
of,2
the,2
was,1
如果您使用的是RDD,那么在这种情况下可以使用reduceByKey
>>> rdd.collect()
[("('best', 'it')", 3), ("('best', 'of')", 4), ("('best', 'the')", 3), ("('best', 'was')", 3), ("('it', 'of')", 11), ("('it', 'the')", 11)]
>>> rddMap = rdd.map(lambda x: x[0][1:-1].split(',')).flatMap(lambda x: [(i.replace("'","").strip(),1) for i in x])
>>> rddMap.collect()
[('best', 1), ('it', 1), ('best', 1), ('of', 1), ('best', 1), ('the', 1), ('best', 1), ('was', 1), ('it', 1), ('of', 1), ('it', 1), ('the', 1)]
>>> rddReduce = rddMap.reduceByKey(lambda x,y: x+y).map(lambda x: x[0]+','+str(x[1]))
>>> for i in rddReduce.collect(): print(i)
...
best,4
it,3
of,2
the,2
was,1
为了使它与spark一起工作(不确定您的配对列表是否足够大以证明spark的使用是合理的),您应该从列表中创建一个数据帧。之后,它应该是dataframe中的某种groupBy,以使其与spark一起工作(不确定您的配对列表是否足够大以证明spark的使用是合理的),您应该从列表中创建一个dataframe。之后,它应该是数据帧中的某种groupBy