[Hadoop in Action] 第7章 细则手册

  • 向任务传递定制参数
  • 获取任务待定的信息
  • 生成多个输出
  • 与关系数据库交互
  • 让输出作全局排序
 
一、向任务传递做业定制的参数
 
     在编写Mapper和Reducer时,一般会想让一些地方能够配置。例如第5章的联结程序被固定地写为取第一个数据列做为联结键。若是用户能够在运行时指定某个列做为联结键,就会让程序更具普适性。hadoop自身使用一个配置对象来存储全部做业的配置属性。你也可使用这个对象将参数传递到Mapper和Reducer。
 
     咱们已经知道MapReduce的driver是如何用属性来配置JobConf对象的,这些属性包括输入格式、输出格式、Mapper类等。若要引入本身的属性,须要在这个配置对象中,给属性一个惟一的名称并设置它的值。这个配置对象会被传递给全部的TaskTracker,而后做业中的全部任务就可以看到配置对象中的属性。Mapper和Reducer也就能够读取该配置对象并获取它的属性值。
 
     Configuration类(JobConf的父类)有许多通用的setter方法。属性采用键/值对的形式,键必须是一个String,而值能够是经常使用类型的任意一个。经常使用setter方法的签名为:
     public void set(String name, String value);
     public void setBoolean(String name, Boolean value);
     public void setInt(String name, Int value);
     public void setLong(String name, Long value);
     public void setStrings(String name, String... values);
请注意在hadoop内部,全部的属性都存为字符串。在set(String, String)方法以外的全部其余方法都是它的便捷方法。
 
     Driver会首先设置配置对象中的属性,让它们在全部任务中可见。Mapper和Reducer能够访问configure()方法中的配置对象。任务初始化时会调用configure(),它已经被覆写为能够提取和存储你设置的属性。以后,map()和reduce()方法会访问这些属性的副本。示例,调用新的属性myjob.myproperty,用一个由用户指定的整数值:
     public int run(String[] args) throws Exception {
          Configuration conf = getConf();
          JobConf job = new JobConf(conf, MyJob.class);
          ...
          job.setInt(“myjob.myproperty”, Integer.parseInt(args[2]));
          JobClient.runJob(job);
          return 0;
     }
 
在MapClass中,configure()方法取出属性值,并将它存储在对象的范围中。Configuration类的getter方法须要指定默认的值,若是所请求的属性未在配置对象中设置,就会返回默认值。在这个例子中,咱们取默认值为0:
     public static class MapClass extends MapReduceBase
          implements Mapper<Text, Text, Text, Text> {
          int myproperty;
          public void configure(JobConf job) {
               myproperty = job.getInt(“myjob.myproperty”,0);
          }
          ...
     }
 
若是你但愿在Reducer中使用该属性,Reducer也必须检索这个属性:
     public static class Reduce extends MapReduceBase
          implements Reducer<Text, Text, Text, Text> {
          int myproperty;
          public void configure(JobConf job) {
               myproperty = job.getInt(“myjob.myproperty”,0);
          }
          ...
     }
 
Configuration类中getter方法的列表比setter方法更长,几乎全部的getter方法都须要将参数设置为默认值。惟一例外是get(String),若是没有设置特定的名称,它就会返回null:
     public String get(String name)
     public String get(String name, String defaultValue)
     public Boolean getBoolean(String name, Boolean defaultValue)
     public float getFloat(String name, Float defaultValue)
     public Int getInt(String name, Int defaultValue)
     public Long getLong(String name, Long defaultValue)
     public String[] getStrings(String name, String... defaultValue)
 
     既然咱们的job类实现了Tool接口并使用了ToolRunner,咱们还可让用户直接使用通用的选项来配置定制化的属性,方法与用户设置hadoop的配置属性相同:
     hadoop jar MyJob.jar MyJob -D myjob.myproperty=1 input output
 
     咱们能够将driver中老是须要用户经过参数来设定属性值的那行代码删掉。若是在大多数时间里默认值是可用的,这样作会让用户感受更加方便。当你容许用户设定属性时,在driver中最好对用户的输入进行验证:
     public int run(String[] args) throws Exception {
          Configuration conf = getConf();
          JobConf job = new JobConf(conf, MyJob.class);
          ...
          Int myproperty = job.getInt(“myjob.myproperty”, 0);
          if (my property < 0) {
               System.err.println(“Invalid myjob.myproperty”+myproperty);
                    System.exit(0);
          }
          JobClient.runJob(job);
          return 0;
     }
 
二、探查任务特定信息
 
     除了获取自定义属性和全局配置以外,咱们还可使用配置对象上的getter方法得到当前任务和做业状态的一些信息:
     this.inputFile = job.get(“map.input.file”);    //得到当前map任务的文件路径
     this.inputTag = generateInputTag(this.inputFile);    //在data join软件包的DataJoinMapperBase中,configure()方法中用一个标签来表示数据源
 
在配置对象中可得到的任务特定状态信息:
 
属性
类型
描述
mapred.job.id String 做业ID
mapred.jar String 做业目录中jar的位置
job.local.dir String 做业的本地空间
mapred.tip.id String 任务ID
mapred.task.id String 任务重试ID
mapred.task.is.map Boolean 标志量,表示是否为一个map任务
mapred.task.partition Int 做业内部的任务ID
map.input.file String Mapper读取的文件路径
map.input.start Long 当前Mapper输入分片的文件偏移量
map.input.length Long 当前Mapper输入分片的字节数
mapred.work.output.dir String 任务的工做(即临时)输出目录
 
三、划分为多个输出文件
 
     在有些有些场景中,输出多组文件或把一个数据集分为多个数据集更为方便。MultipleOutputFormat提供了一个建党的方法,将类似的记录结组为不一样的数据集。在写每条记录以前,这个OutputFormat类调用一个内部方法来肯定要写入的文件名。更具体地说,你将扩展MultipleOutputFormat的某个特定子类,并实现generateFileNameForKeyValue()方法。你扩展的子类将决定输出的格式,例如MultipleTextOutputFormat将输出文本文件,而MultipleSequenceFileOutputFormat将输出序列文件。
 
     不管哪一种状况,你会覆写下面的方法以返回每一个输出键/值对的文件名:
     protected String generateFileNameForKeyValue(K key, V value, String name)
 

代码清单 根据国家将专利元数据分割到多个目录中
 
 1 import java.io.IOException;
 2 import java.util.Iterator;
 3  
 4 import org.apache.hadoop.conf.Configuration;
 5 import org.apache.hadoop.conf.Configured;
 6 import org.apache.hadoop.fs.Path;
 7 import org.apache.hadoop.io.IntWritable;
 8 import org.apache.hadoop.io.LongWritable;
 9 import org.apache.hadoop.io.NullWritable;
10 import org.apache.hadoop.io.Text;
11 import org.apache.hadoop.mapred.FileInputFormat;
12 import org.apache.hadoop.mapred.FileOutputFormat;
13 import org.apache.hadoop.mapred.SequenceFileInputFormat;
14 import org.apache.hadoop.mapred.SequenceFileOutputFormat;
15 import org.apache.hadoop.mapred.KeyValueTextInputFormat;
16 import org.apache.hadoop.mapred.TextInputFormat;
17 import org.apache.hadoop.mapred.TextOutputFormat;
18 import org.apache.hadoop.mapred.JobClient;
19 import org.apache.hadoop.mapred.JobConf;
20 import org.apache.hadoop.mapred.MapReduceBase;
21 import org.apache.hadoop.mapred.Mapper;
22 import org.apache.hadoop.mapred.OutputCollector;
23 import org.apache.hadoop.mapred.Reducer;
24 import org.apache.hadoop.mapred.Reporter;
25 import org.apache.hadoop.mapred.lib.MultipleTextOutputFormat;
26 import org.apache.hadoop.util.Tool;
27 import org.apache.hadoop.util.ToolRunner;
28  
29  
30 public class MultiFile extends Configured implements Tool {
31  
32     public static class MapClass extends MapReduceBase
33         implements Mapper<LongWritable, Text, NullWritable, Text> {
34  
35         public void map(LongWritable key, Text value,
36                         OutputCollector<NullWritable, Text> output,
37                         Reporter reporter) throws IOException {
38  
39             output.collect(NullWritable.get(), value);
40         }
41     }
42  
43     public static class PartitionByCountryMTOF
44         extends MultipleTextOutputFormat<NullWritable,Text>
45     {
46         protected String generateFileNameForKeyValue(NullWritable key,
47                                                      Text value,
48                                                      String inputfilename)
49         {
50             String[] arr = value.toString().split(",", -1);
51             String country = arr[4].substring(1,3);
52             return country+"/"+inputfilename;
53         }
54     }
55  
56     public int run(String[] args) throws Exception {
57         // Configuration processed by ToolRunner
58         Configuration conf = getConf();
59  
60         // Create a JobConf using the processed conf
61         JobConf job = new JobConf(conf, MultiFile.class);
62  
63         // Process custom command-line options
64         Path in = new Path(args[0]);
65         Path out = new Path(args[1]);
66         FileInputFormat.setInputPaths(job, in);
67         FileOutputFormat.setOutputPath(job, out);
68  
69         // Specify various job-specific parameters     
70         job.setJobName("MultiFile");
71         job.setMapperClass(MapClass.class);
72  
73         job.setInputFormat(TextInputFormat.class);
74         job.setOutputFormat(PartitionByCountryMTOF.class);
75         job.setOutputKeyClass(NullWritable.class);
76         job.setOutputValueClass(Text.class);
77  
78         job.setNumReduceTasks(0);
79  
80         // Submit the job, then poll for progress until the job is complete
81         JobClient.runJob(job);
82  
83         return 0;
84     }
85  
86     public static void main(String[] args) throws Exception {
87         // Let ToolRunner handle generic command-line options 
88         int res = ToolRunner.run(new Configuration(), new MultiFile(), args);
89  
90         System.exit(res);
91     }
92 }
 

 
     MutipleOutputFormat很简单,能够按行拆分输入数据,但若是想按列拆分会该怎样作呢?咱们能够在hadoop 0.19版本zhong引入的MutipleOutputs,以得到更强的能力。
     
     MutipleOutputs所采用的方法不一样于MutipleOutputFormat。它不是要求给每条记录请求文件名,而是建立多个OutputCollector,每一个OutputCollector能够有本身的OutputFormat和键/值对的类型。MapReduce程序将决定如何向每一个OutputCollector输出数据。
 

代码清单 将输入数据的不一样列提取为不一样文件的程序
 
  1 import java.io.IOException;
  2 import java.util.Iterator;
  3  
  4 import org.apache.hadoop.conf.Configuration;
  5 import org.apache.hadoop.conf.Configured;
  6 import org.apache.hadoop.fs.Path;
  7 import org.apache.hadoop.io.IntWritable;
  8 import org.apache.hadoop.io.LongWritable;
  9 import org.apache.hadoop.io.NullWritable;
 10 import org.apache.hadoop.io.Text;
 11 import org.apache.hadoop.mapred.FileInputFormat;
 12 import org.apache.hadoop.mapred.FileOutputFormat;
 13 import org.apache.hadoop.mapred.SequenceFileInputFormat;
 14 import org.apache.hadoop.mapred.SequenceFileOutputFormat;
 15 import org.apache.hadoop.mapred.KeyValueTextInputFormat;
 16 import org.apache.hadoop.mapred.TextInputFormat;
 17 import org.apache.hadoop.mapred.TextOutputFormat;
 18 import org.apache.hadoop.mapred.JobClient;
 19 import org.apache.hadoop.mapred.JobConf;
 20 import org.apache.hadoop.mapred.MapReduceBase;
 21 import org.apache.hadoop.mapred.Mapper;
 22 import org.apache.hadoop.mapred.OutputCollector;
 23 import org.apache.hadoop.mapred.Reducer;
 24 import org.apache.hadoop.mapred.Reporter;
 25 import org.apache.hadoop.mapred.lib.MultipleTextOutputFormat;
 26 import org.apache.hadoop.mapred.lib.MultipleOutputs;
 27 import org.apache.hadoop.util.Tool;
 28 import org.apache.hadoop.util.ToolRunner;
 29  
 30  
 31 public class MultiFile extends Configured implements Tool {
 32  
 33     public static class MapClass extends MapReduceBase
 34         implements Mapper<LongWritable, Text, NullWritable, Text> {
 35  
 36         private MultipleOutputs mos;
 37         private OutputCollector<NullWritable, Text> collector;
 38  
 39         public void configure(JobConf conf) {
 40             mos = new MultipleOutputs(conf);
 41         }
 42  
 43         public void map(LongWritable key, Text value,
 44                         OutputCollector<NullWritable, Text> output,
 45                         Reporter reporter) throws IOException {
 46  
 47             String[] arr = value.toString().split(",", -1);
 48             String chrono = arr[0] + "," + arr[1] + "," + arr[2];
 49             String geo    = arr[0] + "," + arr[4] + "," + arr[5];
 50  
 51             collector = mos.getCollector("chrono", reporter);
 52             collector.collect(NullWritable.get(), new Text(chrono));
 53             collector = mos.getCollector("geo", reporter);
 54             collector.collect(NullWritable.get(), new Text(geo));
 55         }
 56  
 57         public void close() throws IOException {
 58             mos.close();
 59         }
 60     }
 61  
 62     public int run(String[] args) throws Exception {
 63         // Configuration processed by ToolRunner
 64         Configuration conf = getConf();
 65  
 66         // Create a JobConf using the processed conf
 67         JobConf job = new JobConf(conf, MultiFile.class);
 68  
 69         // Process custom command-line options
 70         Path in = new Path(args[0]);
 71         Path out = new Path(args[1]);
 72         FileInputFormat.setInputPaths(job, in);
 73         FileOutputFormat.setOutputPath(job, out);
 74  
 75         // Specify various job-specific parameters     
 76         job.setJobName("MultiFile");
 77         job.setMapperClass(MapClass.class);
 78  
 79         job.setInputFormat(TextInputFormat.class);
 80 //        job.setOutputFormat(PartitionByCountryMTOF.class);
 81         job.setOutputKeyClass(NullWritable.class);
 82         job.setOutputValueClass(Text.class);
 83         job.setNumReduceTasks(0);
 84  
 85         MultipleOutputs.addNamedOutput(job,
 86                                        "chrono",
 87                                        TextOutputFormat.class,
 88                                        NullWritable.class,
 89                                        Text.class);
 90         MultipleOutputs.addNamedOutput(job,
 91                                        "geo",
 92                                        TextOutputFormat.class,
 93                                        NullWritable.class,
 94                                        Text.class);
 95  
 96         // Submit the job, then poll for progress until the job is complete
 97         JobClient.runJob(job);
 98  
 99         return 0;
100     }
101  
102     public static void main(String[] args) throws Exception {
103         // Let ToolRunner handle generic command-line options 
104         int res = ToolRunner.run(new Configuration(), new MultiFile(), args);
105  
106         System.exit(res);
107     }
108 }
 

 
四、以数据库做为输入输出
 
     虽然有可能创建一个MapReduce程序经过直接查询数据库来取得输入数据,而不是从HDFS中读取文件,但其性能不甚理想。更多时候,你须要将数据集从数据库复制到HDFS中。你能够很容易地经过标准的数据库工具dump,来取得一个flat文件,而后使用HDFS的shell文件put将它上传到HDFS中。可是有时更合理的作法是让MapReduce程序直接写入数据库。
 
     DBOutputFormat是用于访问数据库的关键类。你能够经过在DBConfiguration中的静态方法configureDB()作到这一点:
     public static void configureDB(Jobconf job, String driverClass, String dbUrl, String userName, String passwd)
 
     以后,你要指定将写入的表,以及那里有哪些字段。这是经过在DBOutputForamt中的静态setOutput()方法作到的:
     public static void setOutput(Jobconf job, String tableName, String… fieldNames)
 
     你的driver应该包含以下样式的几行代码:
     conf.setOutputFormat(DBOutputFormat.class);
     DBConfiguration.configureDB(job,
                                                     “com.mysql.jdbc.Driver”,
                                                     “jdbc.mysql://db.host.com/mydb”,
                                                     “username”,
                                                     “password" ) ;
     DBOutputFormat.setOutput(job, “Events”, “event_id”, “time");
 
使用DBOutputFormat将强制你输出的键实现DBWritable接口。只有这个键会被写入到数据库中。一般,键必须实现Writable接口。在Writable中write()方法用DataOutput,而DBWritable中的write()方法用PreparedStatement。相似的,用在Writable中的readFields()方法采用DataInput,而DBWritable中的readFields()采用ResultSet。除非你打算使用DBInputFormat直接从数据库中读取输入的数据,不然在DBWritable中的readFields()将永远不会被调用。
    
     public class EventsDBWritable implements Writable, DBWritable {
          private int id;
          private long timestamp;
 
          public void write(DataOutput out) throws IOException {
               out.writeInt(id);
               out.writeLong(timestamp);
          }
 
          public void readFields(DataInput in) throws IOException {
               id = in.readInt();
               timestamp = in.readLong();
          }
 
     public void write(PreparedStatement statement) throws IOException {
               statement.setInt(1, id);
               statement.setLong(2, timestamp);
          }
 
     public void readFields(ResultSet resultSet) throws IOException {
               id = resultSet.getInt(1);
               timestamp = resultSet.getLong(2);
          }
     }
 
五、保持输出的顺序
 
     请记住MapReduce框架并不能保证reducer输出的顺序,它只是已经排序好的输入以及reducer所执行的典型操做类型的一种副产品。对于某些应用,这种排序是没有必要的。
 
     Partitioner的任务是肯定地为每一个键分配一个reducer,相同键的全部记录都结成组并在reduce阶段被集中处理。Partitioner的一个重要设计需求是在reducer之间达到负载均衡。Partitioner默认使用散列函数来均匀、随机地将键分配给reducer。若是视线知道键是大体均匀分布的,咱们就可使用一个partitioner给每一个reducer分配一个键的范围,仍然能够确保reducer的负载是相对均衡的。
 
TotalOrderPartitioner是一个能够保证在输入分区之间,而不只仅是分区内部排序的partitioner。这种类利用一个排好序的分区键组读取一个序列文件,并进一步将不一样区域的键分配到reducer上。 
 
 [转载请注明] http://www.cnblogs.com/zhengrunjian/ 
相关文章
相关标签/搜索