国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 中自定義OutputFormat的實例詳解

java 中自定義OutputFormat的實例詳解

2020-12-18 13:11woshisap Java教程

這篇文章主要介紹了java 中 自定義OutputFormat的實例詳解的相關資料,這里提供實例幫助大家學習理解這部分內容,希望通過本文能幫助到大家,需要的朋友可以參考下

java 中 自定義OutputFormat的實例詳解

實例代碼:

?
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
package com.ccse.hadoop.outputformat;
 
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.StringTokenizer;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.OutputCommitter;
import org.apache.hadoop.mapreduce.OutputFormat;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter;
 
 
public class MySelfOutputFormatApp {
   
  public final static String INPUT_PATH = "hdfs://chaoren1:9000/mapinput";
  public final static String OUTPUT_PATH = "hdfs://chaoren1:9000/mapoutput";
  public final static String OUTPUT_FILENAME = "/abc";
   
  public static void main(String[] args) throws IOException, URISyntaxException, 
    ClassNotFoundException, InterruptedException {
    Configuration conf = new Configuration();
    FileSystem fileSystem = FileSystem.get(new URI(OUTPUT_PATH), conf);
    fileSystem.delete(new Path(OUTPUT_PATH), true);
     
    Job job = new Job(conf, MySelfOutputFormatApp.class.getSimpleName());
    job.setJarByClass(MySelfOutputFormatApp.class);
     
    FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));
    job.setMapperClass(MyMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(LongWritable.class);
     
    job.setReducerClass(MyReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(LongWritable.class);
    job.setOutputFormatClass(MyselfOutputFormat.class);
     
    job.waitForCompletion(true);
  }
   
  public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
 
    private Text word = new Text();
    private LongWritable writable = new LongWritable(1);
     
    @Override
    protected void map(LongWritable key, Text value,
        Mapper<LongWritable, Text, Text, LongWritable>.Context context)
        throws IOException, InterruptedException {
      if (value != null) {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
          word.set(tokenizer.nextToken());
          context.write(word, writable);
        }
      }
    }
     
  }
   
  public static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
 
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values,
        Reducer<Text, LongWritable, Text, LongWritable>.Context context)
        throws IOException, InterruptedException {
      long sum = 0
      for (LongWritable value : values) {
        sum += value.get();
      }
      context.write(key, new LongWritable(sum));
    }
  }
 
  public static class MyselfOutputFormat extends OutputFormat<Text, LongWritable> {
 
    private FSDataOutputStream outputStream = null;
     
    @Override
    public RecordWriter<Text, LongWritable> getRecordWriter(
        TaskAttemptContext context) throws IOException,
        InterruptedException {
      try {
        FileSystem fileSystem = FileSystem.get(new URI(MySelfOutputFormatApp.OUTPUT_PATH), context.getConfiguration());
        //指定文件的輸出路徑
        final Path path = new Path(MySelfOutputFormatApp.OUTPUT_PATH 
                     + MySelfOutputFormatApp.OUTPUT_FILENAME);
        this.outputStream = fileSystem.create(path, false);
      } catch (URISyntaxException e) {
        e.printStackTrace();
      }
      return new MySelfRecordWriter(outputStream);
    }
 
    @Override
    public void checkOutputSpecs(JobContext context) throws IOException,
        InterruptedException {
    }
 
    @Override
    public OutputCommitter getOutputCommitter(TaskAttemptContext context)
        throws IOException, InterruptedException {
      return new FileOutputCommitter(new Path(MySelfOutputFormatApp.OUTPUT_PATH), context);
    }
     
  }
   
  public static class MySelfRecordWriter extends RecordWriter<Text, LongWritable> {
 
    private FSDataOutputStream outputStream = null;
     
    public MySelfRecordWriter(FSDataOutputStream outputStream) {
      this.outputStream = outputStream;
    }
     
    @Override
    public void write(Text key, LongWritable value) throws IOException,
        InterruptedException {
      this.outputStream.writeBytes(key.toString());
      this.outputStream.writeBytes("\t");
      this.outputStream.writeLong(value.get());
    }
 
    @Override
    public void close(TaskAttemptContext context) throws IOException,
        InterruptedException {
      this.outputStream.close();
    }
     
  }
   
}

 2.OutputFormat是用于處理各種輸出目的地的。

2.1 OutputFormat需要寫出去的鍵值對,是來自于Reducer類,是通過RecordWriter獲得的。

2.2 RecordWriter中的write(...)方法只有k和v,寫到哪里去哪?這要通過單獨傳入OutputStream來處理。write就是把k和v寫入到OutputStream中的。

2.3 RecordWriter類位于OutputFormat中的。因此,我們自定義的OutputFromat必須繼承OutputFormat類型。那么,流對象必須在getRecordWriter(...)方法中獲得。

以上就是java 中自定義OutputFormat的實例,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/woshisap/article/details/42320129

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: www.中文字幕.com | 欧美日韩一区二区三区不卡视频 | 影音先锋网址 | 欧美黄色a视频 | 欧美 亚洲 一区 | 黄色小视频在线免费观看 | 亚洲色图网站 | 日韩在线播放一区二区三区 | 日韩免费视频一区二区 | 91精品国产乱码久久久久久久久 | 免费观看福利视频 | 免费观看一区二区三区毛片 | 香蕉成人啪国产精品视频综合网 | 亚洲成人精品在线观看 | 久久久精品综合 | 波多野结衣福利电影 | 久久精国产| 成人免费在线观看视频 | 国产乱码精品一区二区三区中文 | 精品欧美日韩 | 成人在线视频网址 | 欧美在线 | 亚洲免费视频网站 | 精品国产欧美一区二区 | 看av片 | 日本久久影视 | 免费裸体无遮挡黄网站免费看 | 中文字幕在线一区二区三区 | 精品福利一区二区三区 | 一区二区在线不卡 | 国产免费成人 | 福利社午夜影院 | 欧美全黄| 国产欧美综合视频 | 亚洲自拍偷拍综合 | 亚洲精品久久久久久动漫 | 成人免费网视频 | 国产精一区| 精品一二三四区 | 中文字幕久久久 | 在线视频se|