job.setJarByClass(Merge.class);
时间: 2024-05-31 14:11:58 浏览: 8
这行代码是在 Hadoop MapReduce 程序中设置执行的 JAR 包的类。它告诉 Hadoop 这个 MapReduce 程序运行的主类是哪个,以便在集群中运行任务时,能够找到并执行相应的代码。在这行代码中,`Merge.class` 是执行 MapReduce 的主类,`job` 是一个 `Job` 对象,用来配置和运行 MapReduce 任务。
相关问题
Path inputPath=new Path("/class 202/data/mrexcise_data/small_files); Path outputPath=new Path("/user/manager/mr_excise_out/merge_res"); Configuration conf=new Configuration() conf.set("fs.defaultFS"."hdfs://10.1.22.210:9000); FileSystemfs=FileSystem.get(conf); fs.delete(outputPath,true); Job job =Job.getinstance(conf job.setJarByClass(SmallFileMergeMain.class); job.setMapperClass(SmallFileMergeMapper.class) job.setNumReduceTasks(0); job.setMapOutputKeyClassext.class): job.setMapOutputValueClass(NullWritable.class); job.setlnputFormatClass(CombineTextinputFormat.class): CombineTextinputFormat.setMinInputSplitSize(job,1024*1024*128); FilelnputFormat.addinputPathjob,inputPath) FileOutputFormat.setOutputPath(job,outputPath); if (job.waitForCompletion(true)){ return;}想要在结果压缩后输出需要添加什么
的代码?
在结果压缩后输出,需要在程序中添加以下代码:
job.setOutputFormatClass(GzipOutputFormat.class);
GzipOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
其中,GzipOutputFormat.class是输出格式,GzipCodec.class是压缩解码器,可以根据需要选择不同的压缩格式和解码器。
为什么这串代码无法实现文件的去重: public static void main(String[] args) throws Exception{ System.setProperty("username","hadoop234"); Configuration conf = new Configuration(); conf.set("fs.default.name", "hdfs://localhost:9000"); FileSystem fs = FileSystem.get(conf); int len = args.length; if(fs.exists(new Path(args[len-1]))) { fs.delete(new Path(args[len-1]),true); } Job job = Job.getInstance(conf,"Merge and duolicate removal"); job.setJarByClass(quchong.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); for(int i=0;i<len-1;++i) { FileInputFormat.addInputPath(job, new Path(args[i])); } FileOutputFormat.setOutputPath(job, new Path(args[len-1])); System.exit(job.waitForCompletion(true) ? 0 : 1); fs.close(); }
这段代码中并没有实现去重,因为在 MapReduce 任务中实现去重通常需要借助 Map 和 Reduce 的聚合功能。而这段代码只是设置了一个 Map 和 Reduce 的类,但并没有在类中实现去重逻辑。
如果想要实现文件的去重,需要在 MyMapper 和 MyReducer 类中添加代码来实现。具体来说,MyMapper 类中需要实现将输入文件中的每一行数据作为 key,value 为 null 的键值对输出。然后在 MyReducer 类中,可以利用 MapReduce 框架自动将相同 key 的数据聚合在一起的特性,只输出一个 key,将 value 设置为 null,这样就实现了去重。
这里提供一个简单的伪代码实现:
```java
public class MyMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 将每一行数据作为 key,value 为 null 的键值对输出
context.write(value, NullWritable.get());
}
}
public class MyReducer extends Reducer<Text, NullWritable, Text, NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
// 直接将 key 输出,value 设置为 null,实现去重
context.write(key, NullWritable.get());
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)