MapReduce的运行

Map Reduce操作步骤:

1.启动hadoop集群 ./startall.sh
2.进入jar包所在文件夹Demo
3.在hadoop新建 input hadoop fs -mkdir -p /user/input
4.把分析文件put在input中 hadoop fs -put demo.txt /user/input
5. Hadoop jar TranConfig.jar com.job.Tranconfig config.properties
/ user/input/demo.txt /user/output/res
6.Hadoop jar jar包名 包.类.方法 /user/input/分析文件/user/output/Pv

Hadoop实例

PvUvJob

package com.map;

import java.io.IOException;
import java.util.Map;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;


public class PvUvMap extends Mapper<LongWritable, Text, Text, IntWritable>{
    IntWritable one = new IntWritable(1);   
    protected void map(LongWritable key, Text value,Context context)
            throws IOException, InterruptedException {
        String []res=value.toString().split("\t");
        String url=res[28];
        context.write(new Text(url),one);
                super.map(key, value, context);
    }


}

PvUvmap

package com.map;

import java.io.IOException;
import java.util.Map;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;


public class PvUvMap extends Mapper<LongWritable, Text, Text, IntWritable>{
    IntWritable one = new IntWritable(1);   
    protected void map(LongWritable key, Text value,Context context)
            throws IOException, InterruptedException {
        String []res=value.toString().split("\t");
        String url=res[28];
        context.write(new Text(url),one);
                super.map(key, value, context);
    }


}

PvUvReduce

package com.reduce;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class PvUvReduce extends Reducer<Text,IntWritable,Text,NullWritable> {
    protected void reduce(Text key,Iterable<IntWritable>values,Context context)
    throws IOException,InterruptedException{
        int sum = 0;
        for(IntWritable value:values){
            sum+=value.get();
        }
        context.write(new Text(key.toString()+":"+sum), NullWritable.get());
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值