a . preparatory work
1.1 built Hadoop Distributed system, Blogger is a simple Hadoop distributed system built with 3 virtual machines .
Linux 5.5 - hadoop2.6.0
192.168.19.201 H1 (Master)
192.168.19.202 H2 (Slaver1)
192.168.19.203 H3 (Slaver2)
1.2 preparing website to access IP files
because it is an experiment, a simple txt file can be
such as:vim a.txt
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.2
10.0.0.5
10.0.0.1
10.0.0.5
10.0.0.1
putting data into HDFs
Hadoop fs-put A.txt/user
1.3 Prepare the JAVA compiler, import the required Hadoopjar package, do not repeat
two . mepreduce
2.1 first need to understand the relationship between Ip,pv,uv
in simple terms, PV is the site click- through rate, the same IP hits ten times, the calculation number is ten;
UV can be understood as access to the customer, the same IP no matter how many times you log in in a day, calculate 1 Times .
2.2 Java code
package com.mapreduce.pvuv;import java.io.ioexception;import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.path;import org.apache.hadoop.io.longwritable;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;public class IpUv {public static class map1 extends mapper<longwritable, text, text, text>{ Public static text line=new text (); @Overridepublic void map (longwritable Longwritable,text text,context context) throws ioexception, interruptedexception{ The map function in //mapper implements Line=text;context.write (Line, new text ("1"));}} Public static class reduce1 extends reducer<text, text, text, text>{@Overridepublic void reduce (Text text,iterable<text> values,context context) throws Ioexception, interruptedexception{context.write (Text,new text ("1"));}} public static class map2 extends mapper<longwritable, text, text, Text>{public static text line=new text (); @Overridepublic void map ( Longwritable longwritable,text text,context context) throws IOException, The map function in Interruptedexception{ //mapper implements the Line=text;context.write (New Text ("UV" ), new text ("1"));}} Public static class reduce2 extends reducer<text, text, text, text >{@Overridepublic void reduce (text text,iterable<text> values,context Context) throws ioexception,&Nbsp;interruptedexception{long sum=0;for (text val:values) {sum++;} Context.write (Text,new text (string.valueof (sum)));}} @SuppressWarnings ("deprecation") Public static void main (String[] args) throws EXCEPTION&NBSP;{//&NBSP;TODO&NBSP;AUTO-GENERATED&NBSP;METHOD&NBSP;STUBIF (args.length < 2) { system.out.println ("Args not right !"); return ; }configuration conf=new configuration (); Conf.set ("Mapred.job.tracker", "192.168.19.201:9001"); String inputdir = args[0]; path outputdir =new path (Args[1]); job job1 = new job (conf, "Ipuv1"); joB1.setjarbyclass (Ipuv.class); job1.setmapperclass (Map1.class); job1.setreducerclass (Reduce1.class); job1.setoutputkeyclass ( Text.class); job1.setoutputvalueclass (Text.class); Fileinputformat.setinputpaths (Job1,inputdir); fileoutputformat.setoutputpath (JOB1, outputdir); boolean flag = job1.waitforcompletion (True); &NBSP;&NBSP;&NBSP;IF (flag) { job job2 = new job (conf, "Ipuv2"); job2.setjarbyclass (Ipuv.class); job2.setmapperclass (Map2.class); Job2.setreducerclass (Reduce2.class); job2.setoutputkeyclass ( Text.class); &nbsP; job2.setoutputvalueclass (Text.class); Fileinputformat.setinputpaths (Job2,outputdir); Fileoutputformat.setoutputpath (Job2,new path (outputdir + "-2")); job2.waitforcompletion (True); } }}
Hadoop calculates PV, UV methods more than one, where bloggers use the most simple way to understand
2.3 Run
Packaged into a jar package and placed in any directory of the host Linux
Hadoopjar/home/hadoop/ipuv.jar Com.mapreduce.pvuv.ipuv/user/output
View Run effects
Hadoop dfs-cat/output-2/part-r-00000
UV 4
MapReduce Java applet Computing Web site Uvs under Hadoop Distributed System