大資料hadoop入門案例4–OutputFormat接口輸出不同檔案中
此部落格作為本文學習hadoop大資料内容,内容可能存在不夠全面或者存在偏差。
文章目錄
-
- 1.OutputFormat
- 2.代碼
-
- 2.1Mapper
- 2.2Reducer
- 2.3OutputFormat
- 2.4LogLogRecordWriter
- 2.5Driver
- 3.輸入+輸出
-
- 3.1輸入
- 3.2輸出
1.OutputFormat
MapReducer有預設的檔案輸出流,但是當需要根據輸入内容輸出到不同的檔案中,需要重新編寫OutputFormat。
輸出檔案繼承FileOutputFormat類,建立一個類繼承RecordWriter,還需藥傳入getConfiguration。
2.代碼

2.1Mapper
輸入索引和value輸出value和Null。
package com.root.logoutformat1;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class logmapper extends Mapper<LongWritable,Text,Text, NullWritable>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(value,NullWritable.get());
}
}
2.2Reducer
輸入,輸出都是value和Null。
package com.root.logoutformat1;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class reducer extends Reducer<Text, NullWritable,Text,NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
for (NullWritable value : values) {
context.write(key,value);
}
}
}
2.3OutputFormat
建立一個logoutput檔案繼承FileOutputFormat,向檔案輸出内容,但是還要實作RecordWriter類。
package com.root.logoutformat1;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class logoutput extends FileOutputFormat<Text, NullWritable> {
@Override
public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
LogRecordWriter lrw =new LogRecordWriter(job);
return lrw;
}
}
2.4LogLogRecordWriter
在LogRecordWriter中繼承RecordWriter,并建立輸出檔案的兩條流,在write類中根據相關條件輸出資料,close類中關閉流。
package com.root.logoutformat1;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import java.io.IOException;
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {
private FSDataOutputStream baidu;
private FSDataOutputStream other;
public LogRecordWriter(TaskAttemptContext job) {
//建立兩條流
try {
FileSystem fs = FileSystem.get(job.getConfiguration());
baidu = fs.create(new Path("E:\\testhadoop\\output\\outlogbaidu\\baidu.log"));
other = fs.create(new Path("E:\\testhadoop\\output\\outlogother1\\other1.log"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void write(Text key, NullWritable value) throws IOException, InterruptedException {
//寫檔案
String log = key.toString();
if (log.contains("baidu")){
baidu.writeBytes(log);
}else {
other.writeBytes(log+'\n');
}
}
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
//關流
IOUtils.closeStream(baidu);
IOUtils.closeStream(other);
}
}
2.5Driver
添加logoutput類
package com.root.logoutformat1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class logdriver {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//1.擷取job
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//2.設定jar包路徑
job.setJarByClass(logdriver.class);
//3.關聯mapper和reducer
job.setMapperClass(logmapper.class);
job.setReducerClass(reducer.class);
//4.設定最終輸出的KV類型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
//5.設定map輸出<k,v>類型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
job.setOutputFormatClass(logoutput.class);
//6.設定輸入路徑和輸出路徑
FileInputFormat.setInputPaths(job,new Path("E:\\testhadoop\\input\\inputlog"));
FileOutputFormat.setOutputPath(job,new Path("E:\\testhadoop\\output\\outputlog1"));
//7.送出job
boolean result =job.waitForCompletion(true);
System.exit(result ? 0:1);
}
}