万能的最简单的利用MapReduce程序去重

map端:

package  distinct;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
//行偏移量    行数据     输出key   输出value
public class DistinctMapper extends Mapper<LongWritable, Text, Text, Text>{
	    @Override
	    protected void map(LongWritable key, Text value,Context context)
	    		throws IOException, InterruptedException {
	    		context.write(value, new Text(""));
	    	}
	    }
}

reduce端

package distinct;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
	public class DistinctReducer extends Reducer<Text, Text, Text, Text>{
		 @Override
		protected void reduce(Text key, Iterable<Text> value,Context context) 
						throws IOException, InterruptedException {
		           context.write(key, new Text(""));
		}
	}

Driver端

package com.qianfeng.wordcount;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class DistinctDriver {
	public static void main(String[] args) throws Exception {
		// 创建Configuration
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		// 设置jar加载路径""com.distinct.DistinctDriver"
		job.setJarByClass(DistinctDriver.class);
		// 设置map和reduce//DistinctReducer.class
		job.setMapperClass(DistinctMapper.class);
		job.setReducerClass(DistinctReducer.class);
		// 设置map输出
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		// 设置reduce输出
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		// 设置输入输出路径
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		// 提交任务
		boolean result = job.waitForCompletion(true);
		System.out.println(result);
	}
}

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>distinct</groupId>
  <artifactId>Distinct</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <dependencies>
  <dependency> 
     <groupId>org.apache.hadoop</groupId>
     <artifactId>hadoop-mapreduce-client-core</artifactId>
     <version>2.7.2</version>
  </dependency>
  <dependency>
       <groupId>org.apache.hadoop</groupId>
       <artifactId>hadoop-common</artifactId>
       <version>2.7.2</version>
  </dependency>
  <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
     </dependency>
     <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-common -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-common</artifactId>
        <version>2.7.2</version>
    </dependency>
       <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
     </dependency>
     
     
  </dependencies>  
  
   <build>
  <!-- 配置了很多插件 -->
		<plugins>
   <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.8</source>  
                    <target>1.8</target>  
                </configuration>  
            </plugin> 
            </plugins>
            </build> 
</project>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值