-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhadoop-mr.txt
2061 lines (1949 loc) · 79.8 KB
/
hadoop-mr.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
通过MR实现好友推荐
更新时间:2017-11-08 15:32:55
本页目录
实验介绍
操作步骤
社交网络是现如今影响力巨大的信息平台,社交网站中,您可以通过 可能感兴趣的人 途径增加交友方式。可能感兴趣的人 也称作 好友推荐, 它主要是通过查找两个非好友之间的共同好友情况来实现的。本文将通过一个示例,简单介绍如何通过 MapReduce 的方式实现好友推荐功能。
实验介绍
A,B,C,D,E 五个人的好友关系如下图所示,其中实线表示互为好友关系。那么,如何获取两个不是好友的两个人之间的好友数,并以此为参考,向用户推荐陌生人呢?
图片名称
主要通过以下几个步骤实现:
将好友关系分配到两个 Map 进行处理,其中每个 Map 包含 3 条好友关系。对每一条好友关系进行拆分,若 Key 中的两个人为朋友,则记录 value 值为0,否则 value 值为 1。将拆分的结果进行排序,其中(A B)和(B A)作为同一个 key(A B)。
图片名称
分别对两个 Map 处理的记录进行初步合并,若两个记录的 Key 值相同且每条记录的 Value 都不为 0,则 Value 值加 1。
注意:
在 Combine 阶段,必须保留 Value 为 0 的记录,否则,在 Reduce 阶段,获取的结果会出错。
图片名称
通过 Reduce 方式,合并两个 Map 处理的 Combine 结果。
若两个记录的 Key 值相同且每条记录的 Value 都不为 0,则 Value 值加 1。
将 Value 值为 0 的记录删除。
获取不为好友的两个用户之间的公共好友数:Key 为两个不为好友的用户,Value 是两个不是好友的用户之间的共同好友数。社交网站或者 APP 可以根据这个数值对不是好友的两个用户进行推荐。
图片名称
操作步骤
新建数据表
登录 DataWorks 管理控制台,单击相应项目空间后的 进入工作区。
单击顶部导航栏中的 数据开发,进入数据开发首页后单击 新建 > 新建脚本文件 或 新建脚本。
图片名称
配置新建脚本文件弹出框中的相关信息,填写文件名称,选择类型为 ODPS SQL 后,单击提交。如下图所示:
图片名称
输入建表语句,如下所示:
drop table if exists dual;--创建系统dual
create table dual(id bigint);--如project中不存在此伪表,则需创建并初始化数据
insert overwrite table dual select count(*)from dual;--向系统伪表初始化数据
---创建好友推荐MR的数据输入表.其中uid表示某个用户;friends表示uid用户的好友
create table friends_in (uid string, friends string);
---创建好友推荐MR的数据输出表.其中userA表示某个用户;userB表示不是userA的用户,cnt表示userA和userB之间的共同好友数。
create table friends_out (userA string, userB string, cnt bigint);
单击 运行,直至日志信息返回成功表示目标表创建成功。
图片名称
单击 保存,保存输入的 SQL 建表语句。
导入本地数据
单击顶部功能栏中的 导入 > 导入本地数据,打开本地保存的文件 friends_in_data.csv(点此下载)。
所有配置均设为默认,并查看导入的数据。完成后,单击 下一步。
注意:
在真实的工作环境中,数据必须以txt或csv的文件类型导入。
图片名称
在本地数据导入页面的 导入至表 中,输入 friends_in,即将本次实验的测试数据,导入到好友推荐的输入表 friends_in 中,确定 目标字段 与 源字段 匹配。完成后单击 导入。
图片名称
由于数据量较大,请等待1-2分钟。
数据导入完成后,可输入语句进行查询、确认。如下图所示:
1
添加 MR 资源
单击左侧导航栏中的 资源管理,单击列表右上角的 上传资源。
1
配置资源上传弹出框中的信息,选择需要上传的文件 Friends_MR。如下图所示:
图片名称
单击 提交。
在左侧导航栏的 资源管理 下,即可看到上传成功的 Jar 包 friends_mr.jar。
测试并验证好友推荐
单击顶部导航栏中的 新建 > 新建任务,开始创建本次实验的 MR 任务。
在弹出的对话框中,选择新建任务的 任务类型 为 节点任务,配置如下图所示:
图片名称
单击 创建。
在任务页面中输入各配置信息,如下图所示:
图片名称
配置项说明:
MRJar 包:单击文本框,选择 friends_mr.jar。
资源:默认设置为 friends_mr.jar。
输入表:输入 friends_in。
mapper:输入 friends_mr_odps.FriendsMapper,此为 Jar 包中 Mapper 的 class 全名。
reducer:输入 friends_mr_odps.FriendsReducer,此为 Jar 包中 Reducer 的 class 全名。
combiner:输入 friends_mr_odps.FriendsCombiner,此为 Jar 包中 Combiner 的 class 全名。
输出表:输入 friends_out。
输出 Key:输入 userA:String,userB:String。
输出 Val:输入 cnt:Bigint。
保存 并 运行 配置的 OPEN MR 任务,可在底部的 日志 中,查看运行状态和运行结果。如下图所示:
图片名称
在脚本文件中输入如下的 SQL 命令,并单击 运行,查询共同好友超过 2 个的数据信息。
SELECT * FROM friends_out WHERE cnt>2 order by cnt desc limit 100;
===================================
WordCount示例
更新时间:2019-03-07 16:31:37
编辑 ·
· 我的收藏
本页目录
测试准备
测试步骤
预期结果
代码示例
本文向您介绍MapReduce WordCount示例程序。
测试准备
准备好测试程序的Jar包,假设名字为mapreduce-examples.jar,本地存放路径为data\resources。
创建测试表。
试用
create table wc_in (key string, value string);
create table wc_out(key string, cnt bigint);
添加测试资源。
试用
add jar data\resources\mapreduce-examples.jar -f;
准备好WordCount测试表和资源。
使用tunnel导入数据。
试用
tunnel upload data wc_in;
导入wc_in表的数据文件data的内容,如下所示:
试用
hello,odps
测试步骤
在odpscmd中执行WordCount,如下所示:
试用
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.WordCount wc_in wc_out
预期结果
作业成功结束后,输出表wc_out中的内容,如下所示:
试用
+------------+------------+
| key | cnt |
+------------+------------+
| hello | 1 |
| odps | 1 |
+------------+------------+
代码示例
试用
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import java.util.Iterator;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.TableInfo;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.ReducerBase;
import com.aliyun.odps.mapred.TaskContext;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.mapred.utils.SchemaUtils;
public class WordCount {
public static class TokenizerMapper extends MapperBase {
private Record word;
private Record one;
@Override
public void setup(TaskContext context) throws IOException {
word = context.createMapOutputKeyRecord();
one = context.createMapOutputValueRecord();
one.set(new Object[] { 1L });
System.out.println("TaskID:" + context.getTaskID().toString());
}
@Override
public void map(long recordNum, Record record, TaskContext context)
throws IOException {
for (int i = 0; i < record.getColumnCount(); i++) {
word.set(new Object[] { record.get(i).toString() });
context.write(word, one);
}
}
}
/**
* A combiner class that combines map output by sum them.
**/
public static class SumCombiner extends ReducerBase {
private Record count;
@Override
public void setup(TaskContext context) throws IOException {
count = context.createMapOutputValueRecord();
}
//combiner实现的接口和reducer一样,可以立即为在mapper本地执行的一个reduce,作用是减少mapper的输出量
@Override
public void reduce(Record key, Iterator<Record> values, TaskContext context)
throws IOException {
long c = 0;
while (values.hasNext()) {
Record val = values.next();
c += (Long) val.get(0);
}
count.set(0, c);
context.write(key, count);
}
}
/**
* A reducer class that just emits the sum of the input values.
**/
public static class SumReducer extends ReducerBase {
private Record result = null;
@Override
public void setup(TaskContext context) throws IOException {
result = context.createOutputRecord();
}
@Override
public void reduce(Record key, Iterator<Record> values, TaskContext context)
throws IOException {
long count = 0;
while (values.hasNext()) {
Record val = values.next();
count += (Long) val.get(0);
}
result.set(0, key.get(0));
result.set(1, count);
context.write(result);
}
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: WordCount <in_table> <out_table>");
System.exit(2);
}
JobConf job = new JobConf();
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(SumCombiner.class);
job.setReducerClass(SumReducer.class);
//设置mapper中间结果的key和value的schema, mapper的中间结果输出也是record的形式
job.setMapOutputKeySchema(SchemaUtils.fromString("word:string"));
job.setMapOutputValueSchema(SchemaUtils.fromString("count:bigint"));
//设置输入和输出的表信息
InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), job);
OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
JobClient.runJob(job);
}
}
MapOnly示例
更新时间:2018-12-12 10:52:47
编辑 ·
· 我的收藏
本页目录
测试准备
测试步骤
预期结果
代码示例
对于MapOnly的作业,Map直接将<Key,Value>信息输出到MaxCompute的表中,您只需要指定输出表即可,不需指定Map输出的Key/Value元信息。
测试准备
准备好测试程序的Jar包,假设名字为mapreduce-examples.jar,本地存放路径为data\resources。
准备好MapOnly的测试表和资源。
创建测试表。
试用
create table wc_in (key string, value string);
create table wc_out(key string, cnt bigint);
添加测试资源。
试用
add jar data\resources\mapreduce-examples.jar -f;
使用tunnel导入数据。
试用
tunnel upload data wc_in;
导入wc_in表的数据文件data的内容,如下所示:
试用
hello,odps
hello,odps
测试步骤
在odpscmd中执行MapOnly,如下所示:
试用
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.MapOnly wc_in wc_out map
预期结果
作业成功结束后,输出表wc_out中的内容,如下所示:
试用
+------------+------------+
| key | cnt |
+------------+------------+
| hello | 1 |
| hello | 1 |
+------------+------------+
代码示例
试用
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.SchemaUtils;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.data.TableInfo;
public class MapOnly {
public static class MapperClass extends MapperBase {
@Override
public void setup(TaskContext context) throws IOException {
boolean is = context.getJobConf().getBoolean("option.mapper.setup", false);
//Main函数在jobconf里设置了option.mapper.setup为true,才会执行下面的逻辑
if (is) {
Record result = context.createOutputRecord();
result.set(0, "setup");
result.set(1, 1L);
context.write(result);
}
}
@Override
public void map(long key, Record record, TaskContext context) throws IOException {
boolean is = context.getJobConf().getBoolean("option.mapper.map", false);
//Main函数在jobconf里设置了option.mapper.map为true,才会执行下面的逻辑
if (is) {
Record result = context.createOutputRecord();
result.set(0, record.get(0));
result.set(1, 1L);
context.write(result);
}
}
@Override
public void cleanup(TaskContext context) throws IOException {
boolean is = context.getJobConf().getBoolean("option.mapper.cleanup", false);
//Main函数在jobconf里设置了option.mapper.cleanup为true,才会执行下面的逻辑
if (is) {
Record result = context.createOutputRecord();
result.set(0, "cleanup");
result.set(1, 1L);
context.write(result);
}
}
}
public static void main(String[] args) throws Exception {
if (args.length != 2 && args.length != 3) {
System.err.println("Usage: OnlyMapper <in_table> <out_table> [setup|map|cleanup]");
System.exit(2);
}
JobConf job = new JobConf();
job.setMapperClass(MapperClass.class);
//对于MapOnly的作业,必须显式设置reducer的个数为0
job.setNumReduceTasks(0);
//设置输入输出的表信息
InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), job);
OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
if (args.length == 3) {
String options = new String(args[2]);
//jobconf中可以设置自定义的key,value值,在mapper中通过context的getJobConf可以获取到相关的设置
if (options.contains("setup")) {
job.setBoolean("option.mapper.setup", true);
}
if (options.contains("map")) {
job.setBoolean("option.mapper.map", true);
}
if (options.contains("cleanup")) {
job.setBoolean("option.mapper.cleanup", true);
}
}
JobClient.runJob(job);
}
}
-----------------------
多路输入输出示例
本页目录
测试准备
测试步骤
预期结果
代码示例
测试准备
准备好测试程序的Jar包,假设名字为mapreduce-examples.jar,本地存放路径为data\resources。
准备好多路输入输出的测试表和资源。
创建测试表。
试用
create table wc_in1(key string, value string);
create table wc_in2(key string, value string);
create table mr_multiinout_out1 (key string, cnt bigint);
create table mr_multiinout_out2 (key string, cnt bigint) partitioned by (a string, b string);
alter table mr_multiinout_out2 add partition (a='1', b='1');
alter table mr_multiinout_out2 add partition (a='2', b='2');
添加测试资源。
试用
add jar data\resources\mapreduce-examples.jar -f;
使用tunnel导入数据。
试用
tunnel upload data1 wc_in1;
tunnel upload data2 wc_in2;
导入wc_in1表的数据文件data的内容,如下所示:
试用
hello,odps
导入wc_in2表的数据文件data的内容,如下所示:
试用
hello,world
测试步骤
在odpscmd中执行MultipleInOut,如下所示:
试用
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.MultipleInOut wc_in1,wc_in2 mr_multiinout_out1,mr_multiinout_out2|a=1/b=1|out1,mr_multiinout_out2|a=2/b=2|out2;
预期结果
作业成功结束后,mr_multiinout_out1中的内容,如下所示:
试用
+------------+------------+
| key | cnt |
+------------+------------+
| default | 1 |
+------------+------------+
mr_multiinout_out2中内容,如下所示:
试用
+--------+------------+---+---+
| key | cnt | a | b |
+--------+------------+---+---+
| odps | 1 | 1 | 1 |
| world | 1 | 1 | 1 |
| out1 | 1 | 1 | 1 |
| hello | 2 | 2 | 2 |
| out2 | 1 | 2 | 2 |
+--------+------------+---+---+
代码示例
试用
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.TableInfo;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.ReducerBase;
import com.aliyun.odps.mapred.TaskContext;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.mapred.utils.SchemaUtils;
/**
* Multi input & output example.
**/
public class MultipleInOut {
public static class TokenizerMapper extends MapperBase {
Record word;
Record one;
@Override
public void setup(TaskContext context) throws IOException {
word = context.createMapOutputKeyRecord();
one = context.createMapOutputValueRecord();
one.set(new Object[] { 1L });
}
@Override
public void map(long recordNum, Record record, TaskContext context)
throws IOException {
for (int i = 0; i < record.getColumnCount(); i++) {
word.set(new Object[] { record.get(i).toString() });
context.write(word, one);
}
}
}
public static class SumReducer extends ReducerBase {
private Record result;
private Record result1;
private Record result2;
@Override
public void setup(TaskContext context) throws IOException {
//对于不同的输出需要创建不同的record,通过label来区分
result = context.createOutputRecord();
result1 = context.createOutputRecord("out1");
result2 = context.createOutputRecord("out2");
}
@Override
public void reduce(Record key, Iterator<Record> values, TaskContext context)
throws IOException {
long count = 0;
while (values.hasNext()) {
Record val = values.next();
count += (Long) val.get(0);
}
long mod = count % 3;
if (mod == 0) {
result.set(0, key.get(0));
result.set(1, count);
//不指定label,输出的默认(default)输出
context.write(result);
} else if (mod == 1) {
result1.set(0, key.get(0));
result1.set(1, count);
context.write(result1, "out1");
} else {
result2.set(0, key.get(0));
result2.set(1, count);
context.write(result2, "out2");
}
}
@Override
public void cleanup(TaskContext context) throws IOException {
Record result = context.createOutputRecord();
result.set(0, "default");
result.set(1, 1L);
context.write(result);
Record result1 = context.createOutputRecord("out1");
result1.set(0, "out1");
result1.set(1, 1L);
context.write(result1, "out1");
Record result2 = context.createOutputRecord("out2");
result2.set(0, "out2");
result2.set(1, 1L);
context.write(result2, "out2");
}
}
//将分区字符串如"ds=1/pt=2"转为map的形式
public static LinkedHashMap<String, String> convertPartSpecToMap(
String partSpec) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
if (partSpec != null && !partSpec.trim().isEmpty()) {
String[] parts = partSpec.split("/");
for (String part : parts) {
String[] ss = part.split("=");
if (ss.length != 2) {
throw new RuntimeException("ODPS-0730001: error part spec format: "
+ partSpec);
}
map.put(ss[0], ss[1]);
}
}
return map;
}
public static void main(String[] args) throws Exception {
String[] inputs = null;
String[] outputs = null;
if (args.length == 2) {
inputs = args[0].split(",");
outputs = args[1].split(",");
} else {
System.err.println("MultipleInOut in... out...");
System.exit(1);
}
JobConf job = new JobConf();
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(SumReducer.class);
job.setMapOutputKeySchema(SchemaUtils.fromString("word:string"));
job.setMapOutputValueSchema(SchemaUtils.fromString("count:bigint"));
//解析用户的输入表字符串
for (String in : inputs) {
String[] ss = in.split("\\|");
if (ss.length == 1) {
InputUtils.addTable(TableInfo.builder().tableName(ss[0]).build(), job);
} else if (ss.length == 2) {
LinkedHashMap<String, String> map = convertPartSpecToMap(ss[1]);
InputUtils.addTable(TableInfo.builder().tableName(ss[0]).partSpec(map).build(), job);
} else {
System.err.println("Style of input: " + in + " is not right");
System.exit(1);
}
}
//解析用户的输出表字符串
for (String out : outputs) {
String[] ss = out.split("\\|");
if (ss.length == 1) {
OutputUtils.addTable(TableInfo.builder().tableName(ss[0]).build(), job);
} else if (ss.length == 2) {
LinkedHashMap<String, String> map = convertPartSpecToMap(ss[1]);
OutputUtils.addTable(TableInfo.builder().tableName(ss[0]).partSpec(map).build(), job);
} else if (ss.length == 3) {
if (ss[1].isEmpty()) {
LinkedHashMap<String, String> map = convertPartSpecToMap(ss[2]);
OutputUtils.addTable(TableInfo.builder().tableName(ss[0]).partSpec(map).build(), job);
} else {
LinkedHashMap<String, String> map = convertPartSpecToMap(ss[1]);
OutputUtils.addTable(TableInfo.builder().tableName(ss[0]).partSpec(map)
.label(ss[2]).build(), job);
}
} else {
System.err.println("Style of output: " + out + " is not right");
System.exit(1);
}
}
JobClient.runJob(job);
}
}
多任务示例
更新时间:2018-12-12 10:54:39
编辑 ·
· 我的收藏
本页目录
测试准备
测试步骤
预期结果
代码示例
测试准备
准备好测试程序的Jar包,假设名字为mapreduce-examples.jar,本地存放路径为data\resources。
准备好MultiJobs测试表和资源。
创建测试表。
试用
create table mr_empty (key string, value string);
create table mr_multijobs_out (value bigint);
添加测试资源。
试用
add table mr_multijobs_out as multijobs_res_table -f;
add jar data\resources\mapreduce-examples.jar -f;
测试步骤
在odpscmd中执行MultiJobs,如下所示:
试用
jar -resources mapreduce-examples.jar,multijobs_res_table -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.MultiJobs mr_multijobs_out;
预期结果
作业成功结束后,输出表mr_multijobs_out中的内容,如下所示:
试用
+------------+
| value |
+------------+
| 0 |
+------------+
代码示例
试用
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import java.util.Iterator;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.TableInfo;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.RunningJob;
import com.aliyun.odps.mapred.TaskContext;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.mapred.utils.SchemaUtils;
/**
* MultiJobs
*
* Running multiple job
*
**/
public class MultiJobs {
public static class InitMapper extends MapperBase {
@Override
public void setup(TaskContext context) throws IOException {
Record record = context.createOutputRecord();
long v = context.getJobConf().getLong("multijobs.value", 2);
record.set(0, v);
context.write(record);
}
}
public static class DecreaseMapper extends MapperBase {
@Override
public void cleanup(TaskContext context) throws IOException {
//从JobConf中获取main函数中定义的变量值
long expect = context.getJobConf().getLong("multijobs.expect.value", -1);
long v = -1;
int count = 0;
//读取资源表里面的数据,这个表是上一个job的输出表
Iterator<Record> iter = context.readResourceTable("multijobs_res_table");
while (iter.hasNext()) {
Record r = iter.next();
v = (Long) r.get(0);
if (expect != v) {
throw new IOException("expect: " + expect + ", but: " + v);
}
count++;
}
if (count != 1) {
throw new IOException("res_table should have 1 record, but: " + count);
}
Record record = context.createOutputRecord();
v--;
record.set(0, v);
context.write(record);
//设置counter,counter在作业成功结束后,可以在main函数中获取到
context.getCounter("multijobs", "value").setValue(v);
}
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: TestMultiJobs <table>");
System.exit(1);
}
String tbl = args[0];
long iterCount = 2;
System.err.println("Start to run init job.");
JobConf initJob = new JobConf();
initJob.setLong("multijobs.value", iterCount);
initJob.setMapperClass(InitMapper.class);
InputUtils.addTable(TableInfo.builder().tableName("mr_empty").build(), initJob);
OutputUtils.addTable(TableInfo.builder().tableName(tbl).build(), initJob);
initJob.setMapOutputKeySchema(SchemaUtils.fromString("key:string"));
initJob.setMapOutputValueSchema(SchemaUtils.fromString("value:string"));
//maponly作业需要显式设置reducer的数目为0
initJob.setNumReduceTasks(0);
JobClient.runJob(initJob);
while (true) {
System.err.println("Start to run iter job, count: " + iterCount);
JobConf decJob = new JobConf();
decJob.setLong("multijobs.expect.value", iterCount);
decJob.setMapperClass(DecreaseMapper.class);
InputUtils.addTable(TableInfo.builder().tableName("mr_empty").build(), decJob);
OutputUtils.addTable(TableInfo.builder().tableName(tbl).build(), decJob);
//maponly作业需要显式设置reducer的数目为0
decJob.setNumReduceTasks(0);
RunningJob rJob = JobClient.runJob(decJob);
iterCount--;
//如果迭代次数已经达到,则退出循环
if (rJob.getCounters().findCounter("multijobs", "value").getValue() == 0) {
break;
}
}
if (iterCount != 0) {
throw new IOException("Job failed.");
}
}
}
二次排序示例
更新时间:2018-12-12 10:55:21
编辑 ·
· 我的收藏
本页目录
测试准备
测试步骤
预期结果
代码示例
测试准备
准备好测试程序的Jar包,假设名字为mapreduce-examples.jar,本地存放路径为data\resources。
准备好SecondarySort的测试表和资源。
创建测试表。
试用
create table ss_in(key bigint, value bigint);
create table ss_out(key bigint, value bigint)
添加测试资源。
试用
add jar data\resources\mapreduce-examples.jar -f;
使用tunnel导入数据。
试用
tunnel upload data ss_in;
导入ss_in表的数据文件data的内容,如下所示:
试用
1,2
2,1
1,1
2,2
测试步骤
在odpscmd中执行SecondarySort,如下所示:
试用
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.SecondarySort ss_in ss_out;
预期结果
作业成功结束后,输出表ss_out中的内容,如下所示:
试用
+------------+------------+
| key | value |
+------------+------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
+------------+------------+
代码示例
试用
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import java.util.Iterator;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.ReducerBase;
import com.aliyun.odps.mapred.TaskContext;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.SchemaUtils;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.data.TableInfo;
/**
*
* This is an example ODPS Map/Reduce application. It reads the input table that
* must contain two integers per record. The output is sorted by the first and
* second number and grouped on the first number.
*
**/
public class SecondarySort {
/**
* Read two integers from each line and generate a key, value pair as ((left,
* right), right).
**/
public static class MapClass extends MapperBase {
private Record key;
private Record value;
@Override
public void setup(TaskContext context) throws IOException {
key = context.createMapOutputKeyRecord();
value = context.createMapOutputValueRecord();
}
@Override
public void map(long recordNum, Record record, TaskContext context)
throws IOException {
long left = 0;
long right = 0;
if (record.getColumnCount() > 0) {
left = (Long) record.get(0);
if (record.getColumnCount() > 1) {
right = (Long) record.get(1);
}
key.set(new Object[] { (Long) left, (Long) right });
value.set(new Object[] { (Long) right });
context.write(key, value);
}
}
}
/**
* A reducer class that just emits the sum of the input values.
**/
public static class ReduceClass extends ReducerBase {
private Record result = null;
@Override
public void setup(TaskContext context) throws IOException {
result = context.createOutputRecord();
}
@Override
public void reduce(Record key, Iterator<Record> values, TaskContext context)
throws IOException {
result.set(0, key.get(0));
while (values.hasNext()) {
Record value = values.next();
result.set(1, value.get(0));
context.write(result);
}
}
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: secondarysrot <in> <out>");
System.exit(2);
}
JobConf job = new JobConf();
job.setMapperClass(MapClass.class);
job.setReducerClass(ReduceClass.class);
//将多列设置为Key
//compare first and second parts of the pair
job.setOutputKeySortColumns(new String[] { "i1", "i2" });
//partition based on the first part of the pair
job.setPartitionColumns(new String[] { "i1" });
//grouping comparator based on the first part of the pair
job.setOutputGroupingColumns(new String[] { "i1" });
//the map output is LongPair, Long
job.setMapOutputKeySchema(SchemaUtils.fromString("i1:bigint,i2:bigint"));
job.setMapOutputValueSchema(SchemaUtils.fromString("i2x:bigint"));
InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), job);
OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
JobClient.runJob(job);
System.exit(0);
}
}
使用资源示例
更新时间:2018-12-12 10:56:02
编辑 ·
· 我的收藏
本页目录
测试准备
测试步骤
预期结果
代码示例
测试准备
准备好测试程序的Jar包,假设名字为mapreduce-examples.jar,本地存放路径为data\resources。
准备好测试表和资源。
创建测试表。
试用
create table mr_upload_src(key bigint, value string);
添加测试资源。
试用
add jar data\resources\mapreduce-examples.jar -f;
add file data\resources\import.txt -f;
import.txt的数据内容,如下所示:
试用
1000,odps
测试步骤
在odpscmd中执行Upload,如下所示:
试用
jar -resources mapreduce-examples.jar,import.txt -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.Upload import.txt mr_upload_src;
预期结果
作业成功结束后,输出表mr_upload_src中的内容,如下所示:
试用
+------------+------------+
| key | value |
+------------+------------+
| 1000 | odps |
+------------+------------+
代码示例
试用
package com.aliyun.odps.mapred.open.example;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.TableInfo;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.TaskContext;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.mapred.utils.SchemaUtils;
/**
* Upload
*
* Import data from text file into table
*
**/
public class Upload {
public static class UploadMapper extends MapperBase {
@Override
public void setup(TaskContext context) throws IOException {
Record record = context.createOutputRecord();
StringBuilder importdata = new StringBuilder();
BufferedInputStream bufferedInput = null;
try {
byte[] buffer = new byte[1024];
int bytesRead = 0;
String filename = context.getJobConf().get("import.filename");
bufferedInput = context.readResourceFileAsStream(filename);
while ((bytesRead = bufferedInput.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
importdata.append(chunk);
}
String lines[] = importdata.toString().split("\n");
for (int i = 0; i < lines.length; i++) {
String[] ss = lines[i].split(",");
record.set(0, Long.parseLong(ss[0].trim()));
record.set(1, ss[1].trim());
context.write(record);
}
} catch (FileNotFoundException ex) {
throw new IOException(ex);
} catch (IOException ex) {
throw new IOException(ex);
} finally {
}
}
@Override
public void map(long recordNum, Record record, TaskContext context)
throws IOException {
}
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: Upload <import_txt> <out_table>");
System.exit(2);
}
JobConf job = new JobConf();
job.setMapperClass(UploadMapper.class);
//设置资源名字, 可以在map中通过jobconf获取到