MapReduce文件合并与去重

该博客展示了如何使用Hadoop MapReduce编写Java程序来合并文件。代码中定义了`MergeMapper`和`MergeReducer`类,分别实现了map和reduce阶段的功能。mapper将输入文件的每一行作为key-value对的value输出,而reducer则将所有相同的key(即所有行)合并在一起。最终,程序将所有输入文件的内容合并到一个输出文件中。

import java.io.IOException;



import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class Merge {


    //map

    public static class MergeMapper extends Mapper<Object,Text,Text,Text>{
        public void map(Object key,Text value,Mapper<Object,Text,Text,Text>.Context context)throws IOException,InterruptedException{

            context.write(value,new Text(""));

        }
    }


    //reduce

    public static class MergeReducer extends Reducer<Text,Text,Text,Text>{

        public void reduce(Text key,Iterable<Text> values,Reducer<Text,Text,Text,Text>.Context context)throws IOException,InterruptedException{

            context.write(key,new Text(""));
        }
    }


    //main

    public static void main(String[] args)throws Exception{

        Configuration conf=new Configuration();

        Job job=Job.getInstance(conf,"merge");

        job.setJarByClass(Merge.class);

        job.setMapperClass(MergeMapper.class);

        job.setReducerClass(MergeReducer.class);

        job.setOutputKeyClass(Text.class);

        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job,new Path("input"));

        FileOutputFormat.setOutputPath(job,new Path("output"));

        System.exit(job.waitForCompletion(true)?0:1);

    }

}

代码部分完成后运行程序,运行框会显示报错,不需要管,进行文件打包。

打包步骤如下:

 

 按照要求在usr/local/hadoop目录下创建两个文本文件a.txt  b.txt ,之后进行如下操作:

在Python中,可以使用以下步骤来合并根目录下的所有txt文件: 1. 遍历根目录下的所有文件,筛选出txt文件。 2. 打开每个txt文件,读取其中的内容。 3. 将每个文件中的内容添加到一个列表中。 4. 使用集合(set)数据结构对列表进行操作。 5. 将后的内容写入一个新的txt文件中。 以下是一个示例代码,用于实现这个功能: ```python import os root_dir = "根目录路径" output_file = "合并后的文件.txt" file_content_set = set() # 遍历根目录下的所有文件 for root, dirs, files in os.walk(root_dir): for file in files: # 筛选出txt文件 if file.endswith(".txt"): file_path = os.path.join(root, file) # 打开文件并读取内容 with open(file_path, "r", encoding="utf-8") as f: content = f.read() # 将内容添加到集合中 file_content_set.update(content.splitlines()) # 将后的内容写入新的txt文件 with open(output_file, "w", encoding="utf-8") as f: f.write("\n".join(file_content_set)) ``` 以上代码会遍历根目录下的所有txt文件,并将它们的内容合并,最终将后的内容写入一个新的txt文件中。请根据实际情况修改代码中的根目录路径和输出文件名。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [python3.7.2下多个txt文件合并](https://download.csdn.net/download/qq_41650481/11007723)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [使用python对文件夹里的所有表格合并](https://blog.csdn.net/weixin_47970003/article/details/121792711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值