hadoop -- topk

需求

对100万条在0~1000万之间的数据取 TopK
输入数据:
8995149,5191755,2093544,9816608,4360204,5507730,1289204,6166586,8955325,9567003,
8264570,5202810,5353570,279129,9281133,6205171,5684360,1666876,4727056,2383250,
9176282,2815703,5145176,7514591,8648912,4633139,4176111,8954834,1702148,7978927,

最小堆

最小堆,是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于其左子节点和右子节点的值。

最大堆

最大堆要求根节点的数据值既大于或等于左子树的数据值,又大于或等于右子树的数据值。
图片来自百度

求topk:最简单版

在每个map中建一个包含K个元素的数组,添加元素时,都和数组中最小/最大的元素比较,大于/小于就替换。缺点:速度慢。时间复杂度:O(n*K)

求topk:改进版

在每个map中建一个包含K个元素的最小堆/最大堆,添加元素时,都和数组中堆顶的元素比较,大于/小于就添加。时间复杂度:O(n*logK),最大topk使用最小堆,最小topk使用最大堆。

最大1000个元素的java代码实现

// An highlighted block

public class Topk extends Configured implements Tool {

	private static final NullWritable NULL = NullWritable.get();
	private static final int N = 1000;

	public static class Map extends Mapper<LongWritable, Text, IntWritable, NullWritable> {

		private static final String SPLIT = ",";
		PriorityQueue<Integer> priorityQueue = new PriorityQueue(N);

		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			String line = value.toString();
			String[] ss = line.split(SPLIT);
			for (int i = 0; i < ss.length; i++) {
				Integer number = Integer.parseInt(ss[i]);

				Integer p = priorityQueue.peek();
				if (priorityQueue.size() < N || number >= p) {
					priorityQueue.offer(number);
				}
				if (priorityQueue.size() > N) {
					priorityQueue.poll();
				}
			}
		}

		@Override
		protected void cleanup(Mapper<LongWritable, Text, IntWritable, NullWritable>.Context context)
				throws IOException, InterruptedException {
			for (Integer integer : priorityQueue) {
				context.write(new IntWritable(integer), NULL);
			}
			priorityQueue.clear();
			priorityQueue = null;
		}

	}

	public static class Reduce extends Reducer<IntWritable, NullWritable, IntWritable, NullWritable> {

		PriorityQueue<Integer> priorityQueue = new PriorityQueue(N);

		@Override
		protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context)
				throws IOException, InterruptedException {
			for (NullWritable nullWritable : values) {
				Integer number = key.get();

				Integer p = priorityQueue.peek();
				if (priorityQueue.size() < N || number >= p) {
					priorityQueue.offer(number);
				}
				if (priorityQueue.size() > N) {
					priorityQueue.poll();
				}
			}
		}

		@Override
		protected void cleanup(Reducer<IntWritable, NullWritable, IntWritable, NullWritable>.Context context)
				throws IOException, InterruptedException {
			for (Integer integer : priorityQueue) {
				context.write(new IntWritable(integer), NULL);
			}
			priorityQueue.clear();
			priorityQueue = null;
		}
	}

	public int run(String[] args) throws Exception {

		// create job
		Job job = Job.getInstance();
		// set job's name
		job.setJobName("mr topk");
		// set job's class
		job.setJarByClass(Topk.class);

		// job set map's class
		job.setMapperClass(Map.class);
		// job set reduce's class
		job.setReducerClass(Reduce.class);

		// job set class of map's output
		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(NullWritable.class);

		// job set class of output's key
		job.setOutputKeyClass(IntWritable.class);
		// job set class of output's value
		job.setOutputValueClass(NullWritable.class);

		// set class of inputFormat
		job.setInputFormatClass(TextInputFormat.class);
		// set class of outputFormat
		// job.setOutputFormatClass(TextOutputFormat.class);

		FileInputFormat.setInputPaths(job, args[0]);
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		job.setNumReduceTasks(1);

		boolean success = job.waitForCompletion(true);
		return success ? 0 : 1;
	}

	public static void main(String[] args) throws Exception {

		int run = ToolRunner.run(new Topk(), args);
		System.exit(run);
	}
}

结果

9989631
9989634
9989661
9989664
9989723
9989726
9989742
9989755
9989765
9989770
9989772
9989782
9989786
9989791
9989803
9989821
9989831
9989831
9989835
9989855
9989871
9989873
9989879
9989882
9989884
9989891
9989896
9989903
9989908
9989909
9989909
9989917
9989917
9989926
9989926
9989968
9989971
9989987
9990008
9990011
9990015
9990020
9990025
9990027
9990032
9990035
9990036
9990042
9990051
9990061
9990063
9990097
9990097
9990105
9990107
9990107
9990110
9990118
9990129
9990164
9990199
9990213
9990214
9990224
9990236
9990239
9990240
9990264
9990293
9990295
9990308
9990310
9990334
9990364
9990366
9990384
9990439
9990452
9990453
9990455
9990456
9990481
9990483
9990489
9990500
9990524
9990549
9990549
9990559
9990576
9990579
9990594
9990609
9990618
9990618
9990622
9990626
9990629
9990633
9990635
9990639
9990642
9990665
9990688
9990690
9990703
9990708
9990717
9990719
9990729
9990770
9990780
9990791
9990797
9990798
9990802
9990808
9990830
9990839
9990842
9990855
9990857
9990887
9990926
9990928
9990941
9990947
9990959
9990976
9990977
9990998
9991006
9991009
9991015
9991022
9991034
9991042
9991068
9991070
9991077
9991090
9991095
9991100
9991123
9991126
9991128
9991129
9991140
9991141
9991144
9991148
9991173
9991177
9991177
9991182
9991190
9991202
9991214
9991215
9991216
9991218
9991226
9991234
9991235
9991250
9991252
9991272
9991278
9991282
9991318
9991329
9991330
9991336
9991351
9991354
9991399
9991399
9991405
9991419
9991423
9991424
9991435
9991444
9991457
9991471
9991489
9991490
9991508
9991517
9991542
9991542
9991565
9991583
9991588
9991603
9991647
9991655
9991680
9991682
9991689
9991715
9991720
9991721
9991725
9991739
9991747
9991752
9991760
9991769
9991789
9991798
9991826
9991833
9991833
9991841
9991861
9991879
9991890
9991898
9991909
9991910
9991921
9991921
9991931
9991935
9991938
9991948
9991962
9991970
9992009
9992014
9992015
9992020
9992032
9992056
9992066
9992066
9992075
9992098
9992099
9992106
9992117
9992120
9992122
9992126
9992130
9992135
9992136
9992138
9992139
9992157
9992165
9992172
9992172
9992183
9992204
9992227
9992238
9992243
9992246
9992254
9992255
9992255
9992271
9992279
9992281
9992284
9992308
9992317
9992321
9992328
9992333
9992334
9992338
9992368
9992369
9992387
9992399
9992410
9992411
9992411
9992429
9992429
9992435
9992444
9992445
9992456
9992462
9992490
9992498
9992501
9992508
9992524
9992540
9992541
9992556
9992563
9992567
9992568
9992576
9992581
9992587
9992601
9992603
9992625
9992642
9992644
9992688
9992692
9992696
9992716
9992748
9992771
9992777
9992784
9992786
9992798
9992801
9992829
9992830
9992837
9992852
9992853
9992904
9992908
9992955
9992990
9993009
9993023
9993028
9993032
9993059
9993067
9993074
9993078
9993082
9993095
9993098
9993102
9993112
9993114
9993128
9993130
9993138
9993171
9993180
9993192
9993201
9993204
9993214
9993214
9993226
9993236
9993261
9993272
9993278
9993283
9993285
9993291
9993300
9993308
9993313
9993325
9993326
9993343
9993361
9993362
9993373
9993377
9993377
9993378
9993392
9993392
9993397
9993400
9993400
9993412
9993421
9993423
9993426
9993434
9993450
9993470
9993476
9993481
9993491
9993503
9993509
9993511
9993526
9993534
9993534
9993541
9993545
9993563
9993573
9993577
9993582
9993616
9993619
9993619
9993624
9993637
9993639
9993644
9993648
9993654
9993664
9993681
9993690
9993715
9993715
9993715
9993722
9993724
9993743
9993755
9993765
9993781
9993784
9993791
9993797
9993845
9993862
9993874
9993880
9993889
9993899
9993914
9993938
9993940
9993944
9993946
9993959
9993987
9993992
9994006
9994012
9994035
9994038
9994039
9994053
9994084
9994090
9994097
9994099
9994138
9994146
9994152
9994207
9994215
9994220
9994221
9994221
9994236
9994240
9994244
9994247
9994275
9994281
9994284
9994287
9994305
9994306
9994314
9994317
9994318
9994325
9994328
9994350
9994360
9994373
9994378
9994381
9994390
9994418
9994420
9994443
9994453
9994456
9994461
9994476
9994477
9994479
9994493
9994500
9994514
9994517
9994521
9994522
9994524
9994542
9994547
9994554
9994563
9994568
9994587
9994592
9994595
9994619
9994639
9994668
9994672
9994682
9994683
9994685
9994698
9994701
9994716
9994757
9994759
9994771
9994776
9994794
9994796
9994803
9994826
9994829
9994836
9994841
9994851
9994875
9994882
9994905
9994907
9994929
9994941
9994942
9994943
9994968
9994983
9995019
9995023
9995054
9995061
9995072
9995080
9995080
9995100
9995103
9995107
9995108
9995109
9995115
9995147
9995158
9995158
9995193
9995232
9995233
9995258
9995286
9995310
9995315
9995319
9995320
9995328
9995334
9995334
9995349
9995351
9995376
9995395
9995411
9995426
9995457
9995481
9995493
9995514
9995525
9995540
9995543
9995545
9995551
9995553
9995561
9995564
9995565
9995567
9995570
9995582
9995582
9995592
9995595
9995616
9995616
9995617
9995624
9995642
9995647
9995673
9995675
9995699
9995780
9995791
9995793
9995801
9995810
9995814
9995815
9995829
9995837
9995839
9995841
9995849
9995859
9995863
9995914
9995916
9995927
9995928
9995930
9995933
9995959
9995964
9995977
9996007
9996016
9996041
9996048
9996057
9996068
9996069
9996079
9996105
9996105
9996105
9996123
9996129
9996135
9996148
9996155
9996159
9996160
9996184
9996191
9996223
9996224
9996237
9996238
9996240
9996248
9996252
9996260
9996300
9996308
9996329
9996335
9996338
9996339
9996360
9996373
9996388
9996393
9996393
9996409
9996422
9996432
9996447
9996452
9996467
9996474
9996477
9996491
9996502
9996507
9996507
9996519
9996525
9996526
9996533
9996533
9996580
9996592
9996596
9996597
9996618
9996619
9996636
9996636
9996641
9996679
9996681
9996684
9996691
9996698
9996713
9996724
9996740
9996741
9996750
9996759
9996768
9996770
9996772
9996774
9996779
9996784
9996809
9996818
9996820
9996845
9996850
9996855
9996880
9996894
9996894
9996903
9996903
9996909
9996917
9996926
9996934
9996935
9996945
9996965
9997022
9997047
9997055
9997082
9997093
9997104
9997111
9997114
9997124
9997146
9997152
9997160
9997182
9997197
9997202
9997210
9997221
9997233
9997240
9997251
9997252
9997266
9997266
9997268
9997268
9997269
9997284
9997291
9997325
9997326
9997329
9997338
9997353
9997357
9997358
9997370
9997390
9997391
9997394
9997408
9997410
9997412
9997420
9997458
9997468
9997474
9997477
9997526
9997526
9997531
9997531
9997537
9997538
9997546
9997557
9997562
9997568
9997575
9997577
9997619
9997628
9997632
9997646
9997652
9997659
9997665
9997668
9997678
9997704
9997708
9997725
9997755
9997793
9997812
9997820
9997840
9997858
9997896
9997916
9997922
9997945
9997951
9997968
9997990
9998023
9998042
9998051
9998062
9998066
9998074
9998075
9998092
9998097
9998123
9998127
9998137
9998141
9998156
9998159
9998160
9998166
9998173
9998180
9998199
9998226
9998241
9998244
9998265
9998276
9998282
9998286
9998301
9998326
9998328
9998329
9998332
9998354
9998363
9998364
9998366
9998390
9998391
9998399
9998407
9998411
9998421
9998422
9998428
9998434
9998443
9998454
9998459
9998459
9998467
9998470
9998485
9998495
9998496
9998498
9998498
9998499
9998499
9998505
9998573
9998573
9998584
9998588
9998609
9998612
9998614
9998616
9998618
9998620
9998625
9998643
9998650
9998653
9998662
9998665
9998677
9998687
9998695
9998712
9998716
9998768
9998768
9998770
9998779
9998784
9998785
9998796
9998801
9998816
9998822
9998831
9998832
9998846
9998859
9998887
9998895
9998922
9998959
9998967
9998971
9999000
9999003
9999037
9999057
9999075
9999089
9999090
9999092
9999097
9999117
9999144
9999150
9999170
9999192
9999193
9999200
9999202
9999215
9999223
9999255
9999275
9999275
9999283
9999307
9999312
9999313
9999314
9999327
9999327
9999350
9999350
9999354
9999354
9999371
9999387
9999387
9999393
9999404
9999414
9999419
9999436
9999450
9999473
9999496
9999498
9999502
9999508
9999525
9999525
9999527
9999533
9999539
9999542
9999552
9999560
9999595
9999608
9999619
9999622
9999626
9999634
9999644
9999651
9999658
9999658
9999662
9999662
9999665
9999674
9999703
9999707
9999718
9999724
9999744
9999767
9999777
9999803
9999806
9999807
9999815
9999821
9999823
9999825
9999828
9999831
9999844
9999847
9999849
9999854
9999875
9999898
9999954
9999968
9999968
9999971
9999972
9999983
9999988
9999988
9999993
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值