package org.apache.hadoop.examples;
import java.util.HashMap;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class A_formatSameString {
public A_formatSameString() {
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String[] otherArgs = new String[]{"input","output"};
if(otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
} Job job = Job.getInstance(conf, "clearSame");
job.setJarByClass(A_formatSameString.class);
job.setMapperClass(A_formatSameString.TokenizerMapper.class);
job.setCombinerClass(A_formatSameString.IntSumReducer.class);
job.setReducerClass(A_formatSameString.IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); for(int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
} FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true)?0:1);
} public static class IntSumReducer extends Reducer<Text, Text, Text, Text> {
private Text word2 = new Text(); public IntSumReducer() {
} public void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
Map dict = new HashMap();
for(Iterator i$ = values.iterator(); i$.hasNext(); ) {
Text value = (Text)i$.next();
if(!dict.containsKey(value)){
dict.put(value,1);
this.word2.set(value);
context.write(key, this.word2);
} }
//System.out.println(key.toString()+"\n"+result.toString());
}
} public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {
private static final IntWritable one = new IntWritable(1);
private Text word = new Text();
private Text word2 = new Text();
public TokenizerMapper() {
} public void map(Object key, Text value, Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
//System.out.println(itr.toString());
while(itr.hasMoreTokens()) {
String tmpstr = itr.nextToken();
String tmpstr2 = itr.nextToken();
this.word.set(tmpstr);
this.word2.set(tmpstr2);
//System.out.println(tmpstr);
context.write(this.word, this.word2);
} }
}
}

对于两个输入文件,即文件A 和文件B ,请编写MapReduce程序,对两个文件进行合并排除其中重复的内容,得到一个新的输出文件C。的更多相关文章

  1. 编写Java程序,在硬盘中选取一个 txt 文件,读取该文档的内容后,追加一段文字“[ 来自新华社 ]”,保存到一个新的 txt 文件内

    查看本章节 查看作业目录 需求说明: 在硬盘中选取一个 txt 文件,读取该文档的内容后,追加一段文字"[ 来自新华社 ]",保存到一个新的 txt 文件内 实现思路: 创建 Sa ...

  2. 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\41c191fd\ff9345c5\App_Web_login.cshtml.65793277

    本地开发环境没问题,但是发布到服务器出现问题或则直接在IIS上修改东西就给我抛出以下错误: 未能写入输出文件"c:\Windows\Microsoft.NET\Framework64 \v4 ...

  3. C#与dotNET项目想要另存为一个新项目sln文件丢了怎么办

    如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到. 很简单的一个思路是反正sln是多余的 ...

  4. Laptop Issue Letter (读取Excel中指定内容,然后生成新的Excel文件)

    $xl = New-Object -ComObject "Excel.Application" $cmdbwb = $xl.Workbooks.Open("F:\Ivan ...

  5. 一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用

    这个命令行神器的下载地址:https://stedolan.github.io/jq/# Windows和Linux版本均只有两个可执行文件,大小不过2MB多. 以Windows版本为例,介绍其用法. ...

  6. CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问

    aspx 常见错误 CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/... ...

  7. Win7下:编译器错误信息: CS0016: 未能写入输出文件

    错误如下: "/"应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS ...

  8. 编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v2.0.50727/....dll”--“拒绝访问。

    错误如下: “/”应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS0016: 未能写入 ...

  9. 访问svc 文件,编译器错误消息: CS0016,未能写入输出文件

    编译错误              说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码.             编译器错误消息: CS0016: 未 ...

随机推荐

  1. Android驱动学习-APP操作新硬件的两种方法(支持添加的驱动)

    在给Android添加新的驱动后,app要如何使用呢? 正常的使用一个设备,需要getService.但是像LED等我们自己添加的硬件驱动,Android源代码根本没有我们自己添加的服务. 第一种: ...

  2. 如果对象为空,java函数String.valueOf(Object obj)返回"null"字符串

    源码如下: public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toStr ...

  3. java数组之基本语义

    A[] a; B[] b=new B[5];print(b)print(b.length)a={new A(),new A()}//聚合初始化:隐式使用new在堆中创建A[] d=new A[];a= ...

  4. 多媒体开发(4):在视频上显示文字或图片--ffmpeg命令

    小白:我录了段视频,里面用的音乐是有版权的,而且快过期了,能把音乐去掉吗? 小程拿到视频后,一个快捷键打开命令终端,快速打下一行命令: ffmpeg -i 小白.flv -vcodec copy -a ...

  5. 对HTTP请求接口资源下载时间过长的问题分析

    问题描述 我司某产品线有指定业务接口customQuery在线上环境中,与首页一起打开时下载数据的时间明显过长(平均可以达到2s) 注: "与首页一起打开" 的含义是指用户进入WE ...

  6. 四、hive安装

    一.安装方式(内嵌模式,本地模式远程模式) 安装环境以及前提说明: Hive是依赖于hadoop系统的,因此在运行Hive之前需要保证已经搭建好hadoop集群环境. 本例中使用的hadoop版本为2 ...

  7. python之logging 模块(下篇)

    四.日志处理流程(第二种日志使用方式) 上面简单配置的方法例子中我们了解到了logging.debug().logging.info().logging.warning().logging.error ...

  8. Redis缓存篇(二)淘汰机制:缓存满了怎么办?

    上一讲提到,缓存的容量总是小于后端数据库的.随着业务系统的使用,缓存数据会撑满内存空间,该怎么处理呢? 本节我们来学习内存淘汰机制.在Redis 4.0之前有6种内存淘汰策略,之后又增加2种,一共8种 ...

  9. linux的 复制 删除 解压 压缩 打包

    liunx 删除 删除文件夹实例: rm -rf /var/log/httpd/access 将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 2 删除文件使用实例: r ...

  10. nacos服务注册与发现原理解析

    前言:nacos 玩过微服务的想必不会陌生,它是阿里对于springcloud孵化出来的产品,用来完成服务之间的注册发现和配置中心,其核心作用我就不废话了 大致流程:每个服务都会有一个nacos cl ...