【Java】使用MapReduce程序统计UV数量

1.UV的概念
UV:unique view—》一天内访问网站的用户数
下面是统计UV的代码:
首先是MapReduce类的代码

package com.huadian.bigdata.webloguv07;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class UVWebLogMapReduce extends Configured implements Tool {

    @Override
    public int run(String[] args) throws Exception {

        //1、创建job
        Job job = Job.getInstance( this.getConf(), "UVWebLogMapReduce" );
        job.setJarByClass( UVWebLogMapReduce.class );
        //2、input
        Path inputPath = new Path( args[0] );
        FileInputFormat.setInputPaths( job,inputPath );

        //3.mapper
        job.setMapperClass( UVWebLogMapper.class );
        job.setMapOutputKeyClass( Text.class );
        job.setMapOutputValueClass(  Text.class );
         //job.setNumReduceTasks(2);

        //5.reduce
        job.setReducerClass( UVWebLogReducer.class  );
        job.setOutputKeyClass( Text.class );
        job.setOutputValueClass( IntWritable.class );

        //6.output
        Path outputPath = new Path( args[1] );
        //如果该路径存在,先删除
        FileSystem hdfs = FileSystem.get( this.getConf() );
        if(hdfs.exists( outputPath )){
            //boolean delete(Path f, boolean recursive)
            hdfs.delete(outputPath,true);
        }
        FileOutputFormat.setOutputPath( job,outputPath );

        boolean isSuccess = job.waitForCompletion( true );
        return isSuccess?0:1;
    }

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        try {
            //public static int run(Configuration conf, Tool tool, String[] args)
            int status = ToolRunner.run( configuration, new UVWebLogMapReduce(), args );
            System.exit( status );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

下面是map类的代码:

package com.huadian.bigdata.webloguv07;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class UVWebLogMapper extends Mapper <LongWritable, Text, Text, Text> {

    private Text outputKey = new Text(  );
    private Text outputValue = new Text( );
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        /**
         * (1)分割    36个字段,如果数组长度<36,该条数据丢弃
         * (2)guid字段,下标:5
         * (3)第25个cityID,下标24
         * 返回:<cityID,1>
         */
        String valueStr = value.toString();
        String[] items = valueStr.split( "\t" );
       context.getCounter("user defined","allCount").increment(1L);
        if(items.length >=36){
           context.getCounter("user defined","count1").increment(1L);
           if(StringUtils.isBlank( items[5] )){
               return;
           }
            outputKey.set( items[24] );
            outputValue.set(items[5]);
           context.write( outputKey ,outputValue);

        }else {
           context.getCounter("user defined","count2").increment(1L);
            return;
        }
    }
}

下面是reduce类的代码:

package com.huadian.bigdata.webloguv07;

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

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class UVWebLogReducer extends Reducer<Text, Text, Text, IntWritable> {
    private IntWritable outputValue = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        //实现业务
        Set<String> set = new HashSet<>();
        for (Text value : values) {
            set.add(value.toString());
        }
        outputValue.set(set.size());
        context.write(key, outputValue);
    }
}

我将这个MapReduce程序分为三个类来写的,便于代码的维护与查看的清晰。这个根据数据的不同统计的,uv就是根据用户的id与访问的ip数来统计的。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用MapReduce统计数据可以借助Hadoop框架来实现。下面是一个简单的示例代码,演示了如何使用MapReduce统计数据: 首先,定义Mapper类,它会将输入数据分割为键值对并进行初步处理: ```java 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 MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = line.split(" "); // 根据空格分割单词 for (String word : words) { this.word.set(word); context.write(this.word, one); } } } ``` 然后,定义Reducer类,它会对Mapper输出的键值对进行汇总处理: ```java import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable value : values) { sum += value.get(); } context.write(key, new IntWritable(sum)); } } ``` 最后,编写主程序来配置和运行MapReduce作业: ```java 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 WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(MyMapper.class); job.setCombinerClass(MyReducer.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上代码是一个简单的WordCount示例,它会统计输入文件中每个单词的出现次数。你可以根据具体需求修改Mapper和Reducer的实现逻辑,以实现其他类型的数据统计

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值