Hadoop之——数据类型

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46039055

一.  Hadoop内置的数据类型

  • BooleanWritable:标准布尔型数值
  • ByteWritable:单字节数值
  • DoubleWritable:双字节数值
  • FloatWritable:浮点数
  • IntWritable:整型数
  • LongWritable:长整型数
  • Text:使用UTF8格式存储的文本
  • NullWritable:当<key, value>中的key或value为空时使用

二、Hadoop自定义数据类型实例

      把后面的URLString 封装成 URL类型。代码如下

[java]  view plain  copy
  1. import java.io.DataInput;  
  2. import java.io.DataOutput;  
  3. import java.io.IOException;  
  4. import java.net.MalformedURLException;  
  5. import java.net.URL;  
  6.   
  7. import org.apache.hadoop.io.Writable;  
  8. /** 
  9.  * @author liuyazhuang 
  10.  */  
  11. public class URLWritable implements Writable {  
  12.   
  13.     protected URL url;  
  14.       
  15.     public URLWritable() {  
  16.           
  17.     }  
  18.       
  19.     public URLWritable(URL url) {  
  20.         this.url = url;  
  21.     }  
  22.   
  23.     @Override  
  24.     public void write(DataOutput out) throws IOException {  
  25.         out.writeUTF(url.toString());  
  26.     }  
  27.   
  28.     @Override  
  29.     public void readFields(DataInput in) throws IOException {  
  30.         this.url = new URL(in.readUTF());  
  31.     }  
  32.       
  33.     public void set(String string) {  
  34.         try {  
  35.             this.url = new URL(string);  
  36.         } catch (MalformedURLException e) {  
  37.             throw new RuntimeException("Should not have happened " + e.toString());  
  38.         }  
  39.     }  
  40. }  
[java]  view plain  copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.commons.lang.StringUtils;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.io.Text;  
  6. import org.apache.hadoop.mapreduce.InputSplit;  
  7. import org.apache.hadoop.mapreduce.RecordReader;  
  8. import org.apache.hadoop.mapreduce.TaskAttemptContext;  
  9. import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;  
  10. /** 
  11.  * @author liuyazhuang 
  12.  */  
  13. public class TimeUrlLineRecordReader extends RecordReader<Text, URLWritable> {  
  14.     public static final String Time_URL_SEPERATOR =   
  15.         "mapreduce.input.keyvaluelinerecordreader.key.value.separator";  
  16.       
  17.     private final LineRecordReader lineRecordReader;  
  18.       
  19.     private byte separator = (byte'\t';  
  20.       
  21.     private Text innerValue;  
  22.         
  23.     private Text key;  
  24.         
  25.     private URLWritable value;  
  26.       
  27.     public static int findSeparator(byte[] utf, int start, int length, byte sep) {  
  28.         for (int i = start; i < (start + length); i++) {  
  29.             if (utf[i] == sep) {  
  30.                 return i;  
  31.             }  
  32.         }  
  33.         return -1;  
  34.     }  
  35.       
  36.     public static void setKeyValue(Text key, URLWritable value, byte[] line,  
  37.             int lineLen, int pos) {  
  38.         if (pos == -1) {  
  39.             key.set(line, 0, lineLen);  
  40.             value.set(StringUtils.EMPTY);  
  41.         } else {  
  42.             key.set(line, 0, pos);  
  43.             String url = null;  
  44.             System.arraycopy(line, pos + 1,url , 0, lineLen - pos - 1);  
  45.             value.set(url);  
  46.         }  
  47.     }  
  48.   
  49.     public TimeUrlLineRecordReader(Configuration conf) throws IOException {  
  50.         lineRecordReader = new LineRecordReader();  
  51.         String sepStr = conf.get(Time_URL_SEPERATOR, "\t");  
  52.         this.separator = (byte) sepStr.charAt(0);  
  53.     }  
  54.       
  55.     @Override  
  56.     public void initialize(InputSplit split, TaskAttemptContext context)  
  57.             throws IOException, InterruptedException {  
  58.          lineRecordReader.initialize(split, context);  
  59.     }  
  60.   
  61.     @Override  
  62.     public boolean nextKeyValue() throws IOException, InterruptedException {  
  63.         byte[] line = null;  
  64.         int lineLen = -1;  
  65.         if (lineRecordReader.nextKeyValue()) {  
  66.             innerValue = lineRecordReader.getCurrentValue();  
  67.             line = innerValue.getBytes();  
  68.             lineLen = innerValue.getLength();  
  69.         } else {  
  70.             return false;  
  71.         }  
  72.         if (line == null) {  
  73.             return false;  
  74.         }  
  75.         if (key == null) {  
  76.             key = new Text();  
  77.         }  
  78.         if (value == null) {  
  79.             value = new URLWritable();  
  80.         }  
  81.         int pos = findSeparator(line, 0, lineLen, this.separator);  
  82.         setKeyValue(key, value, line, lineLen, pos);  
  83.         return true;  
  84.     }  
  85.   
  86.     @Override  
  87.     public Text getCurrentKey() throws IOException, InterruptedException {  
  88.         return key;  
  89.     }  
  90.   
  91.     @Override  
  92.     public URLWritable getCurrentValue() throws IOException,  
  93.             InterruptedException {  
  94.         return value;  
  95.     }  
  96.   
  97.     @Override  
  98.     public float getProgress() throws IOException, InterruptedException {  
  99.         return lineRecordReader.getProgress();  
  100.     }  
  101.   
  102.     @Override  
  103.     public void close() throws IOException {  
  104.         lineRecordReader.close();  
  105.     }  
  106. }  
[java]  view plain  copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.fs.Path;  
  3. import org.apache.hadoop.io.Text;  
  4. import org.apache.hadoop.io.compress.CompressionCodec;  
  5. import org.apache.hadoop.io.compress.CompressionCodecFactory;  
  6. import org.apache.hadoop.mapreduce.InputSplit;  
  7. import org.apache.hadoop.mapreduce.JobContext;  
  8. import org.apache.hadoop.mapreduce.RecordReader;  
  9. import org.apache.hadoop.mapreduce.TaskAttemptContext;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. /** 
  12.  * @author liuyazhuang 
  13.  */  
  14. public class TimeUrlTextInputFormat extends FileInputFormat<Text, URLWritable>{  
  15.   
  16.     @Override  
  17.     protected boolean isSplitable(JobContext context, Path file) {  
  18.         final CompressionCodec codec = new CompressionCodecFactory(  
  19.                 context.getConfiguration()).getCodec(file);  
  20.         return codec == null;  
  21.     }  
  22.   
  23.     @Override  
  24.     public RecordReader<Text, URLWritable> createRecordReader(InputSplit split,  
  25.             TaskAttemptContext context) throws IOException, InterruptedException {  
  26.         context.setStatus(split.toString());  
  27.         return new TimeUrlLineRecordReader(context.getConfiguration());  
  28.     }  
  29. }  
[java]  view plain  copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configured;  
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.io.Text;  
  6. import org.apache.hadoop.mapreduce.Job;  
  7. import org.apache.hadoop.mapreduce.Mapper;  
  8. import org.apache.hadoop.mapreduce.Reducer;  
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  10. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  12. import org.apache.hadoop.util.Tool;  
  13. import org.apache.hadoop.util.ToolRunner;  
  14. /** 
  15.  * @author liuyazhuang 
  16.  */  
  17. public class CustomTimeUrl extends Configured implements Tool {  
  18.   
  19.     public static class CustomTimeUrlMapper extends Mapper<Text, URLWritable, Text, URLWritable> {  
  20.   
  21.         @Override  
  22.         protected void map(Text key, URLWritable value, Context context)  
  23.                 throws IOException, InterruptedException {  
  24.             context.write(key, value);  
  25.         }  
  26.           
  27.     }  
  28.       
  29.     public static class CustomTimeUrlReducer extends Reducer<Text, URLWritable, Text, URLWritable> {  
  30.   
  31.         @Override  
  32.         protected void reduce(Text key, Iterable<URLWritable> values,Context context)throws IOException, InterruptedException {  
  33.             for (URLWritable value : values) {  
  34.                 context.write(key, value);  
  35.             }  
  36.         }  
  37.           
  38.     }  
  39.       
  40.         @Override  
  41.          public int run(String[] args) throws Exception {  
  42.         Job job = new Job(getConf());  
  43.         job.setJarByClass(getClass());  
  44.         job.setJobName("CustomTimeUrl");  
  45.           
  46.         job.setInputFormatClass(TimeUrlTextInputFormat.class);  
  47.         job.setOutputFormatClass(TextOutputFormat.class);  
  48.           
  49.           
  50.         job.setOutputKeyClass(Text.class);  
  51.         job.setOutputValueClass(URLWritable.class);  
  52.           
  53.         job.setMapperClass(CustomTimeUrlMapper.class);  
  54.         job.setReducerClass(CustomTimeUrlReducer.class);  
  55.           
  56.         FileInputFormat.setInputPaths(job, new Path("/timeurl/input/"));  
  57.         FileOutputFormat.setOutputPath(job, new Path("/timeurl/output"));  
  58.           
  59.         boolean success = job.waitForCompletion(true);  
  60.         return success ? 0 : 1;  
  61.     }  
  62.       
  63.     public static void main(String[] args) throws Exception {  
  64.         int result = ToolRunner.run(new TimeUrl(), args);  
  65.         System.exit(result);  
  66.     }  
  67. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值