- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 Hadoop + Spark 的 VirtualMachine 设置,我正在从我的 HDFS 中读取一个文本文件“words.txt”,然后调用 map()、flatmap()、reduceByKey() 并尝试获取前 10 个最常见的单词及其出现。我已经完成了大部分代码,然后聚合了元组列表,但我只需要一种方法来找到前 10 个。我知道我需要简单地遍历元组中的值(键是实际的 str 词,但值是该词在 words.txt 文件中出现的次数的整数)并且只需要一个计数器来计算顶部10. (K,V) 值对是 Key = words.txt 中的单词,Value = 文件中出现次数的整数聚合值。下面这张截图是在调用 reduceByKey() 之后,你可以看到 'the' 出现了 40 次(右边是屏幕截图的结尾)
到目前为止,这是我的代码:
from pyspark import SparkcConf, SparkContext
# Spark set-up
conf = SparkConf()
conf.setAppName("Word count App")
sc = SparkContext(conf=conf)
# read from text file words.txt on HDFS
rdd = sc.textFile("/user/spark/words.txt")
# flatMap() to output multiple elements for each input value, split on space and make each word lowercase
rdd = rdd.flatMap(lamda x: x.lower().split(' '))
# Map a tuple and append int 1 for each word in words.txt
rdd = rdd.map(lamda x: (x,1))
# Perform aggregation (sum) all the int values for each unique key)
rdd = rdd.reduceByKey(lamda x, y: x+y)
# This is where I need a function or lambda to sort by descending order so I can grab the top 10 outputs, then print them out below with for loop
# for item in out:
print(item[0], '\t:\t', str(item[1]))
我知道我通常只会创建一个名为“max”的变量,并且只有在列表或元组中找到最大值时才更新它,但让我感到困惑的是我正在处理 Spark 和 RDD,所以我一直在错误,因为我对 RDD 在执行 map、flatmap、reduceByKey 等操作时返回的内容有些困惑...
非常感谢任何帮助
最佳答案
你可以在 reduce 之后反转 K,V
这样你就可以使用 sortByKey
函数:
rdd.map(lambda (k,v): (v,k)).sortByKey(False).take(10)
对于 Python 3:(因为不再支持在 lambda 表达式中解包元组)
rdd.map(lambda x: (x[1], x[0])).sortByKey(False).take(10)
关于python - Spark/Python,reduceByKey() 然后找到前 10 个最常见的单词和频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240504/
我一直试图让它工作一段时间,但每次都失败了。我有 2 个文件。有一个名字列表: Name1 Name2 Name3 Name4 另一个是与几年中一年中每一天的名称关联的值列表: ['0.1,0.2,0
如果我有一个像这样的 K,V 对: (K, (v1, v2)) (K, (v3, v4)) 我怎样才能总结出我得到的值(k, (v1 + v3, v2 + v4)) ? 最佳答案 reduceByKe
我正在 Spark 上编写一个程序,我只是通过键进行聚合。该程序非常简单。我的输入数据只有 2GB,在设置为 local[2] 的多核服务器(8 核,32GB RAM)上运行。那就是使用两个内核进行并
我想使用 RDD 对 Tuple2 ,但是byte[]内容相同的s被认为是不同的值,因为它们的引用值不同。 我没有看到任何要传递给自定义比较器的东西。我可以转换 byte[]进入 String使用显式
嗨,我有代码只是从文档中获取字数。在生成输出之前,我还需要使用 map 来查找数据值。这是代码。 requests .filter(_.description.exists(_.leng
我的输入数据集看起来像 id1, 10, v1 id2, 9, v2 id2, 34, v3 id1, 6, v4 id1, 12, v5 id2, 2, v6 我想要输出 id1; 6,v4 | 1
我的问题类似于PySpark reduceByKey on multiple values但有一个关键的区别。我是 PySpark 的新手,所以我肯定错过了一些明显的东西。 我有一个具有以下结构的 R
我正在使用 Spark 1.5/1.6,我想在 DataFrame 中执行 reduceByKey 操作,我不想将 df 转换为 rdd。 每行看起来像,我有多个 id1 行。 id1, id2, s
我有一个 RDD,其中包含某些对象的计数,然后我在其上应用 reduceByKey(),对所有元素求和(如字数统计示例中所示)。我已将 reduceByKey 转换的输出保存到文本文件中,并且获得了每
为什么 Spark 强制从元组列表构建 RDD,以防进行 reducebykey 转换? reduce_rdd = sc.parallelize([{'k1': 1}, {'k2': 2}, {'k1
我有一个这样的rdd // Structure List[Tuple(x1, x2, value), Tuple(x1, x2, value)] data = [('23', '98', 34), (
我有一个像这样的 rdd: rdd = [((uid1), (product1, price1, date1)), ((uid1), (product2, price2, date2))
我确定这是非常简单的事情,但我没有找到与此相关的任何内容。 我的代码很简单: ... stream = stream.map(mapper) stream = stream.reduceByKey(r
我想对每个键的值列表进行分组,并且正在做这样的事情: sc.parallelize(Array(("red", "zero"), ("yellow", "one"), ("red", "two")))
我正在使用 Spark 版本 2.4.7-amzn-1 和 Scala 版本 2.11.12(OpenJDK 64 位服务器虚拟机,Java 1.8.0_302)开发 AWS EMR。 我想通过键减少
我正在将 spark 与 scala 一起使用,并且我有一个充满 tuple2 的 RDD,其中包含一个复杂对象作为键和一个 double 对象。如果对象相同,则目的是添加双倍(频率)。 为此,我将我
在对一些稀疏向量进行聚类后,我需要在每个聚类中找到相交向量。为此,我尝试减少 MLlib 向量,如下例所示: import org.apache.spark.SparkConf import org.
最近我有一个场景将数据存储在 keyValue Pair 中,并遇到了一个函数 reduceByKey(_ ++ _) .这更多是速记语法。我无法理解这实际上意味着什么。 例如:reduceBykey
作为我学习过程的一部分,我正在研究现有的 Spark 代码,我遇到了以下代码: enPages.map(lambda x: x.split(" "))\ .map(lambda x:
除了 RDDs 之外,我正在尝试学习更多地使用 DataFrames 和 DataSets。对于 RDD,我知道我可以做 someRDD.reduceByKey((x,y) => x + y) ,但我
我是一名优秀的程序员,十分优秀!