1、分析、およびロジック思考を処理する
(1)入力形式出力形式
<アクセス日付、番号>
テキストIntWritable
達成するために、計算ロジック(2)マッパーを
カスタムクラスを継承MyFirstMapperマッパー<キーの形式の入力と出力を>
地図機能は
、最初の定義されました回数は、
ユーザのアクセスログファイル読み取るする
カンマ区切り<2016-1-1,1>分割する各行単位での
結果は、配列アレイ内に格納されている<2016-1-1,1>
アレイフォーマット<日付1>出力<2016-1-1,1>
<2016-1-2,1>
<2016-1-1,1>
<2016-1-2,1>
<2016から1 -2,1>
<2016-1-3,1>
(3)減速論理が実装されます
MyFirstReducerのカスタムクラスを継承リデューサー<2016-1-1,4> <キーの形式の入力と出力>
reudce機能<2016-1-2,3は>
マッパー出力値のペアを読ん<2016年1月3日、 1>
同じ鍵が蓄積
出力<アクセス日付、総数>
図2に示すように、コードの実装
(1)のMapReduceプロジェクトを作成
Javaクラスを作成します。2.
package test;
import java.io.IOException;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class dailyAccessCount {
// Driver
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length < 2){
System.err.println("Usage: can shu bu dui");
System.exit(2);
}
Job job = new Job(conf, "Daily Access Count");
job.setJarByClass(dailyAccessCount.class);
job.setMapperClass(MyFirstMapper.class);
job.setReducerClass(MyFirstReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 设置输入路径
for(int i = 0; i < otherArgs.length-1; i++){
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length-1]));
System.exit(job.waitForCompletion(true)?0:1);
}
// 自定义Mapper
public static class MyFirstMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line = value.toString();
String array[] = line.split(",");
String keyOutput = array[1];
context.write(new Text(keyOutput), one);
}
}
//Reducer
public static class MyFirstReducer
extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
int sum = 0;
for(IntWritable val :values){
sum +=val.get();
}
result.set(sum);
context.write(key, result);
}
}
}
3、コンパイルされたJarパッケージファイルとのHadoopクラスタの実行を提出
Hadoopのクラスタにアップロード4、
HDFS user_log.txtにアップロードしたログファイル