比较 Spark 中的两种不同方法:减少和排序
Comparing two different methods in Spark: for reducing and sorting
假设我有以下 RDD:
alist = [('a',[['1',2]]),('b',[['2',3]]),('b',[['8',5]]),('b',[['8',5]]),('c',[['4',22]]),('a',[['5',22]])]
anRDD = sc.parallelize(alist)
我的任务是从每个字符串字母中获取具有最高 int 值的列表(列表的索引 1)。如果数据量很大,不同的键(字符串字母)很多,推荐使用以下哪种方法?
方法一:
import operator
def sortAndTake(alistoflists):
alistoflists.sort(key=operator.itemgetter(1),reverse=True)
return alistoflists[0]
reducedRDD = anRDD.reduceByKey(lambda a,b:a+b)
finalRDD = reducedRDD.map(lambda x: (x[0],sortAndTake(x[1])))
finalRDD.collect()
方法二:
def partitioner(n):
def partitioner_(x):
return portable_hash(x[0]) % n
return partitioner_
def sortIterator(iterator):
oldKey = None
cnt = 0
for item in iterator:
if item[0] != oldKey:
oldKey = item[0]
yield item
partitioned = anRDD.keyBy(lambda kv:(kv[0],kv[1][0][1]))
partitioned.repartitionAndSortWithinPartitions(
numPartitions=2,
partitionFunc=partitioner(2),ascending=False)
.map(lambda x: x[1])
.mapPartitions(sortIterator)
(方法 2 的灵感来自于我之前提出的一个问题的已接受答案(by zero323):)
根据我对第一种方法的理解,如果我们得到一个巨大的不同键值,reduceByKey
中的工作人员之间会有很多洗牌,这可以使方法 2 更快(我不确定是否在 method2).
中使用 repartitionAndSortWithinPartitions
时也会发生同样的情况
有什么见解吗?谢谢:)
My task is from each string letter get the list with the highest int value(index 1 of the list).
如果是这种情况,这两种方法都非常低效。而只是 reduceByKey
和 max
:
from operator import itemgetter
from functools import partial
anRDD.mapValues(itemgetter(0)).reduceByKey(partial(max, key=itemgetter(1)))
关于两个提议的方法:
- 两者随机播放相同数量的数据。
- 第一个效率较低
groupByKey
。
假设我有以下 RDD:
alist = [('a',[['1',2]]),('b',[['2',3]]),('b',[['8',5]]),('b',[['8',5]]),('c',[['4',22]]),('a',[['5',22]])]
anRDD = sc.parallelize(alist)
我的任务是从每个字符串字母中获取具有最高 int 值的列表(列表的索引 1)。如果数据量很大,不同的键(字符串字母)很多,推荐使用以下哪种方法?
方法一:
import operator
def sortAndTake(alistoflists):
alistoflists.sort(key=operator.itemgetter(1),reverse=True)
return alistoflists[0]
reducedRDD = anRDD.reduceByKey(lambda a,b:a+b)
finalRDD = reducedRDD.map(lambda x: (x[0],sortAndTake(x[1])))
finalRDD.collect()
方法二:
def partitioner(n):
def partitioner_(x):
return portable_hash(x[0]) % n
return partitioner_
def sortIterator(iterator):
oldKey = None
cnt = 0
for item in iterator:
if item[0] != oldKey:
oldKey = item[0]
yield item
partitioned = anRDD.keyBy(lambda kv:(kv[0],kv[1][0][1]))
partitioned.repartitionAndSortWithinPartitions(
numPartitions=2,
partitionFunc=partitioner(2),ascending=False)
.map(lambda x: x[1])
.mapPartitions(sortIterator)
(方法 2 的灵感来自于我之前提出的一个问题的已接受答案(by zero323):
根据我对第一种方法的理解,如果我们得到一个巨大的不同键值,reduceByKey
中的工作人员之间会有很多洗牌,这可以使方法 2 更快(我不确定是否在 method2).
repartitionAndSortWithinPartitions
时也会发生同样的情况
有什么见解吗?谢谢:)
My task is from each string letter get the list with the highest int value(index 1 of the list).
如果是这种情况,这两种方法都非常低效。而只是 reduceByKey
和 max
:
from operator import itemgetter
from functools import partial
anRDD.mapValues(itemgetter(0)).reduceByKey(partial(max, key=itemgetter(1)))
关于两个提议的方法:
- 两者随机播放相同数量的数据。
- 第一个效率较低
groupByKey
。