hadoop之mapreduce实例

news/2024/7/6 5:08:25
例子很简单,我没有运行自带的wordcount,而是自己做了一个简单的例子。
实现的功能是从我们的nginx的access log里面计算url访问的次数。
access log文件:
10.2.112.22 - -  [ 11/Apr/2012:10:25:31 +0800 ] "GET /bf5bd91c/css/base/base_jiexi-all-min.css HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:31 +0800 ] "GET /bf5bd91c/js/lib/lib-min.js HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:31 +0800 ] "GET /image/jiexi/logo.png HTTP/1.1" 304 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:31 +0800 ] "GET /bf5bd91c/js/page/jiexi/index-min.js HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:31 +0800 ] "GET /bf5bd91c/css/page/jiexi/index-all-min.css HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/css/page/jiexi/index-all-min.css HTTP/1.1" 499 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/js/page/jiexi/index-min.js HTTP/1.1" 499 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/js/lib/lib-min.js HTTP/1.1" 499 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/css/base/base_jiexi-all-min.css HTTP/1.1" 499 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /bf5bd91c/css/page/jiexi/index-all-min.css HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /bf5bd91c/js/page/jiexi/index-min.js HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/js/page/jiexi/index-min.js HTTP/1.1" 499 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/css/page/jiexi/index-all-min.css HTTP/1.1" 499 0 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /bf5bd91c/css/base/base_jiexi-all-min.css HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /bf5bd91c/js/lib/lib-min.js HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /bf5bd91c/css/page/jiexi/index-all-min.css HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /bf5bd91c/js/page/jiexi/index-min.js HTTP/1.1" 302 161 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/js/page/jiexi/index-min.js HTTP/1.1" 200 56215 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/css/page/jiexi/index-all-min.css HTTP/1.1" 200 21254 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/css/base/base_jiexi-all-min.css HTTP/1.1" 200 22782 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
10.2.112.22 - -  [ 11/Apr/2012:10:25:32 +0800 ] "GET /release/js/lib/lib-min.js HTTP/1.1" 200 137514 "http://www.jiexi.com/home" "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
新建maven项目:
< 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 >com.jiexi </ groupId >
     < artifactId >jiexi-examples </ artifactId >
     < version >0.0.1-SNAPSHOT </ version >
     < dependencies >
         < dependency >
             < groupId >org.apache.hadoop </ groupId >
             < artifactId >hadoop-core </ artifactId >
             < version >1.0.2 </ version >
         </ dependency >
     </ dependencies >
</ project >
Mapper代码如下:
package com.jiexi.examples.hadoop;
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.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

public class AccessLogMapper extends MapReduceBase implements
        Mapper < LongWritable , Text, Text, IntWritable > {
    private final static IntWritable one = new IntWritable(1);
    private Text url = new Text();

    static String POST = "\"POST ";
    static String GET = "\"GET ";
    static String END = " HTTP/1.0";

    public void map(LongWritable key, Text value,
            OutputCollector < Text , IntWritable > output, Reporter reporter)
            throws IOException {
        String line = value.toString();
        String url1 = getUrl(line);

        url.set(url1);
        output.collect(url, one);
    }

    public static void main(String[] args) {
        String a = "10.2.112.34 - - [06/Mar/2012:18:05:41 +0800] \"GET /mine?originUrl= HTTP/1.0\" 302 -";
        String b = "10.2.112.34 - - [06/Mar/2012:15:02:42 +0800] \"POST /user/login?originUrl=http%3A%2F%2Fwww.jiexi.com%2Fhome HTTP/1.0\" 200 25";
 
//        System.out.println(getUrl(a));
//        System.out.println(getUrl(b));
        
        String s =" /user/register?originUrl=http%3A%2F%2Fwww.jiexi.com%2Fhome";
        
        System.out.println(s.substring(0,s.indexOf("?")));
    }

    private static String getUrl(String a) {
        // int len = POST.length();
        int begin = a.indexOf(POST);
        int get = a.indexOf(GET);
        if (get > -1) {
            begin = get;
            // len = GET.length();
        }

        int end = a.indexOf(END);

        String url = a.substring(begin + 1, end);

        if (url.indexOf("?") > 0) {
            return url.substring(0, url.indexOf("?"));
        }

        return url;
    }

}
Reducer代码如下:
package com.jiexi.examples.hadoop;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public  class AccessLogReducer  extends MapReduceBase  implements
        Reducer<Text, IntWritable, Text, IntWritable> {

     public  void reduce(Text key, Iterator<IntWritable> values,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
             throws IOException {
         int sum = 0;
         while (values.hasNext()) {
            sum += values.next().get();
        }
        output.collect(key,  new IntWritable(sum));
    }

}
job调用
package com.jiexi.examples.hadoop;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

public  class AccessLogPerDayJob {
     public  static  void main(String[] args)  throws Throwable {
        JobConf jobConf =  new JobConf(AccessLogPerDayJob. class);
        jobConf.setJobName("access_log");

        jobConf.setOutputKeyClass(Text. class);
        jobConf.setOutputValueClass(IntWritable. class);

        jobConf.setMapperClass(AccessLogMapper. class);
        jobConf.setCombinerClass(AccessLogReducer. class);
        jobConf.setReducerClass(AccessLogReducer. class);

        jobConf.setInputFormat(TextInputFormat. class);
        jobConf.setOutputFormat(TextOutputFormat. class);

        FileInputFormat.addInputPath(jobConf,  new Path(args[0]));
        FileOutputFormat.setOutputPath(jobConf,  new Path(args[1]));

        JobClient.runJob(jobConf);
    }
}
===============================================
mvn clean package 
丢到namenode的/opt/hadoop 下面
运行:
./bin/hadoop dfs -mkdir access_log_in
./bin/hadoop dfs -ls
#拷贝本地日志文件到hdfs中
./bin/hadoop dfs -put /opt/access_log/*  access_log_in
#运行,把access_log.jar拷贝到/opt/hadoop下面
./bin/hadoop jar access_log.jar com.jiexi.examples.hadoop.AccessLogPerDayJob    access_log_in  access_log_out
查看job运行情况:
http://10.2.112.31:50030/jobtracker.jsp  
查看data:

./bin/hadoop dfs -ls access_log_out

本文转自博客园沉睡森林@漂在北京的博客,原文链接:hadoop之mapreduce实例,如需转载请自行联系原博主。


http://www.niftyadmin.cn/n/1123766.html

相关文章

fib函数用迭代替换递归

fib函数递归实现&#xff1a; long Fib(long n) {if (n < 1) {return n; }else {var t1 Fib(n - 1);var t2 Fib(n - 2);return t1 t2; } } fib函数改为迭代&#xff1a; class Class1 {class Node …

Java四种线程池

线程池的好处 1、线程的创建需要消耗的&#xff0c;用完了马上就扔了比较可惜&#xff0c;所以把它缓存起来&#xff0c;以后还能再用&#xff1b; 2、可以根据实际情况调整线程池的大小&#xff0c;防止线程太多&#xff1b; 3、有些场合可以用线程池来做同步&#xff08;比如…

HDU-2896 病毒侵袭(AC自动姬)

病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 28243 Accepted Submission(s): 6538 Problem Description当太阳的光辉逐渐被月亮遮蔽&#xff0c;世界失去了光明&#xff0c;大地迎来最黑暗的时刻。…

Aruba无线网络学习(二)

说明&#xff1a;工作过程中接触到了Aruba无线网络设备&#xff0c;并在其网站上下载了技术文档。文档是英文的&#xff0c;看起来有一点费劲。只好一边翻译&#xff0c;一边学习&#xff0c;一边记笔记。水平有限&#xff0c;难免有错误的地方&#xff0c;请大家帮助指正。七、…

四种ABAP数据对象(转)

在ABAP/4中可以使用四种数据对象 1、内部数据对象 创建内部数据对象供在特定的程序中使用&#xff0c;在该程序之外无效&#xff0c;包括文字、常量、变量 &#xff08;1&#xff09;文字 文字是固定值&#xff0c;分为文本文字和数字文字。文本文字是单引号内的字母数字字符序…

Linux速成教程

2019独角兽企业重金招聘Python工程师标准>>> Linux操作系统最为有名的是它对初学者不友好&#xff01;当用户开始接触Linux会感觉到迷惑不解&#xff1a;"Linux凭什么得到广泛应用&#xff0c;还如此声名显赫&#xff1f;" 1.终端和shell 2.常见的使用Lin…

截取与分析日志文件的特定行数的操作

在进行操作系统和数据库系统管理时经常会遇到在日志文件中查找某个字符或者按照时间截取某个时间段的日志进行分析。今天早上就遇到一个MySQL数据库上的问题mysql数据库在0-3点的时候数据库会话连接tpscpu和iowait等都比平时大了许多。为了定位这个时间段内到底发生了那些慢查询…

如何在Linux中查看所有正在运行的进程

你可以使用ps命令。它能显示当前运行中进程的相关信息&#xff0c;包括进程的PID。Linux和UNIX都支持ps命令&#xff0c;显示所有运行中进程的相关信息。ps命令能提供一份当前进程的快照。如果你想状态可以自动刷新&#xff0c;可以使用top命令。 ps命令 输入下面的ps命令&…