星空网 > 软件开发 > 数据库

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

  • 向任务传递定制参数
  • 获取任务待定的信息
  • 生成多个输出
  • 与关系数据库交互
  • 让输出做全局排序
 
1、向任务传递作业定制的参数
 
     在编写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;
     }
 
2、探查任务特定信息
 
     除了获取自定义属性和全局配置之外,我们还可以使用配置对象上的getter方法获得当前任务和作业状态的一些信息:
     this.inputFile = job.get(“map.input.file”);    //获得当前map任务的文件路径
     this.inputTag = generateInputTag(this.inputFile);    //在data join软件包的DataJoinMapperBase中,configure()方法中用一个标签来表示数据源
 
在配置对象中可获得的任务特定状态信息:
 
属性
类型
描述
mapred.job.idString作业ID
mapred.jarString作业目录中jar的位置
job.local.dirString作业的本地空间
mapred.tip.idString任务ID
mapred.task.idString任务重试ID
mapred.task.is.mapBoolean标志量,表示是否为一个map任务
mapred.task.partitionInt作业内部的任务ID
map.input.fileStringMapper读取的文件路径
map.input.startLong当前Mapper输入分片的文件偏移量
map.input.lengthLong当前Mapper输入分片的字节数
mapred.work.output.dirString任务的工作(即临时)输出目录
 
3、划分为多个输出文件
 
     在有些有些场景中,输出多组文件或把一个数据集分为多个数据集更为方便。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 MapReduceBase33     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 PartitionByCountryMTOF44     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 ToolRunner58     Configuration conf = getConf();59 60     // Create a JobConf using the processed conf61     JobConf job = new JobConf(conf, MultiFile.class);62 63     // Process custom command-line options64     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 complete81     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 }


 


 
4、以数据库作为输入输出
 
     虽然有可能建立一个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);
          }
     }
 
5、保持输出的顺序
 
     请记住MapReduce框架并不能保证reducer输出的顺序,它只是已经排序好的输入以及reducer所执行的典型操作类型的一种副产品。对于某些应用,这种排序是没有必要的。
 
     Partitioner的任务是确定地为每个键分配一个reducer,相同键的所有记录都结成组并在reduce阶段被集中处理。Partitioner的一个重要设计需求是在reducer之间达到负载均衡。Partitioner默认使用散列函数来均匀、随机地将键分配给reducer。如果视线知道键是大致均匀分布的,我们就可以使用一个partitioner给每个reducer分配一个键的范围,仍然可以确保reducer的负载是相对均衡的。
 
TotalOrderPartitioner是一个可以保证在输入分区之间,而不仅仅是分区内部排序的partitioner。这种类利用一个排好序的分区键组读取一个序列文件,并进一步将不同区域的键分配到reducer上。 
 
 [转载请注明] http://www.cnblogs.com/zhengrunjian/ 



原标题:[Hadoop in Action] 第7章 细则手册

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
如何用摄影作品表现“芳草鲜美,落英缤纷”的:https://www.vstour.cn/a/411247.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流