用scala来写mapreduce做数据去重

1、数据准备


file1数据:

mapper
reduce
by
scala

file2数据:

scala
done
it

2、程序

import java.io.IOException
import java.lang.Iterable

import org.apache.hadoop.conf.Configuration
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.mapreduce.lib.input.FileInputFormat
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat

/**
  * Created by legotime on 10/16/16.
  */
object scalaMapReduce {

  class myScalaMapper extends Mapper[Object, Text, Text, Text] {
    private var line: Text = new Text

    override def map(key: Object, value: Text, context: Mapper[Object, Text, Text, Text]#Context) {
      try {
        line = value
        context.write(line, new Text(""))
      }
      catch {
        case e: InterruptedException => e.printStackTrace()
        case e: IOException => e.printStackTrace()
      }
    }
  }
  class myScalaReducer extends Reducer[Text, Text, Text, Text]{
    override def reduce(key: Text, value: Iterable[Text], context: Reducer[Text, Text, Text, Text]#Context) {
      try {
        context.write(key, new Text(""))
      }
      catch {
        case e: InterruptedException => e.printStackTrace()
        case e: IOException => e.printStackTrace()
      }
    }
  }



  def main(args: Array[String]) {
    val conf = new Configuration()
    val job = new Job(conf,"Data Deduplication by scala")

    //如果是打包成jar文件,那么下面就需要
    //job.setJarByClass(this.getClass)

    //设置Map,Combine,Reduce处理类
    job.setMapperClass(classOf[scalaMapReduce.myScalaMapper])
    job.setCombinerClass(classOf[scalaMapReduce.myScalaReducer])
    job.setReducerClass(classOf[scalaMapReduce.myScalaReducer])

    //设置输出类型
    job.setOutputKeyClass(classOf[Text])
    job.setOutputValueClass(classOf[Text])

    //设置输入和输出目录
    FileInputFormat.addInputPath(job, new Path("/hadoop/mapreduce/DedupData/"))
    FileOutputFormat.setOutputPath(job, new Path("/hadoop/mapreduce/DedupOutDatabyScala/"))

    System.exit(if (job.waitForCompletion(true)) 0 else 1)



  }
}


3、结果:



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值