spark二次排序,两列数据第一列排,如果第一列相等,看第二列

#!/usr/bin/env python3

from operator import gt
from pyspark import SparkContext, SparkConf

class SecondarySortKey():#参数是k,other,自带了参数self,不写
def__init__(self,k):#构造函数
self.column1 = k[0]
self.column2 = k[1]
def__gt__(self,other):
if other.column1 = self.column1:
return gt(self.column2,other.column2)#python3自带函数https://blog.csdn.net/xc_zhou/article/details/81416742
else:
return gt(self.column1,other.column1)

def main:
conf = SparkConf().serAppNmae(‘Spark_sort’).setMaster(‘local[1]’)
sc = SparkContext(conf=conf)
file=“file:///usr/local/saprk/mycode/rdd/secondarysort/file4.txt”
rdd1 = sc.textFile(file)
rdd2 = rdd1.filter(lambda x:len(x.strip())>0))
rdd3 = rdd2.map(lambda x:((int(x.split(" “)[0]),int(x.split(” ")[1])),x))
rdd4 = rdd3.map(lambda x:(SecondarySortKey(x[0]),x[1]))
rdd5 = rdd4.sortByKey(False)
rdd6 =rdd5.map(lambda x:x[1])
rdd6.foreach(print)

if__name__==‘main’:
main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值