1.写一个wordcount程序
package com.huni.mapreduce.WordCount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
*
*
* @ClassName: WordCountMapper
* @author: Huni
* LongWritable:是mr框架所读到的一行文本的起始偏移量,Long
* 在hadoop中有自己的更精简的序列化接口,所以不直接用Long,而是用LongWritable
* Text:是mr框架所读到的一行文本内容
*
* Text:通过mapper输出的key类型(每个单词)
* IntWritable:通过mapper输出的value类型(每个单词出现的数量)
* @date: 2018年10月7日 下午3:52:52
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//用来接收原始数据中每行的数据,并且转换成字符串类型
String line = value.toString();
//通过空格来切割每行数据,用来确定每个单词
String[] words = line.split(" ");
//将单词遍历输出到mapper的缓冲区中
for(String word:words){
//再次又将Java中的String类型转换成Hadoop中的Text
context.write(new Text(word), new IntWritable(1));
}
}
}
package com.huni.mapreduce.WordCount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
*
* @作用:
* @ClassName: WordCountReduce
* @author: Huni
* Text:mapper阶段传过来的,输入中的key类型(每个的单词)
* IntWritable:mapper阶段传过来的,输入中的value类型(每个单词出现的次数)
* Text:reduce输出阶段的key类型(每个单词)
* IntWritable:reduce输出阶段的value类型(统计之后每个单词出现的次数)
* @date: 2018年10月7日 下午4:04:32
*/
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
//Iterable<IntWritable> values是将相同的key的value存储在一个Iterable类型中,相当于(key-list)
int count = 0;
// 1 汇总各个key的个数values(key, value-list)
for (IntWritable value : values) {
count+=value.get();
}
// 2输出该key的总次数
context.write(key, new IntWritable(count));
}
}
package com.huni.mapreduce.WordCount;
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.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
*
* @作用:相当于一个yarn集群的客户端,需要在此封装我们的mr程序相关运行参数,
* 指定jar包,最后提交给yarn
* @ClassName: WordCountJob
* @author: Huni
* @date: 2018年10月7日 下午4:14:17
*/
public class WordCountJob {
public static void main(String[] args) throws Exception {
//1.获取配置信息,得到Job对象
Configuration configuration =new Configuration();
Job job = Job.getInstance(configuration);
//2.设置加载jar的位置
job.setJarByClass(WordCountJob.class);
//3.设置mapper和reduce的class类
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReduce.class);
//4.设置输出mapper的数据类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//5.设置最终输出的数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//自定义设置分区
job.setPartitionerClass(WordCountPartition.class);//自定义为自己重写的partition类
job.setNumReduceTasks(2);//设置开启reduceTask的数量,要与分区数量一致
//设置文件切割机制,当达到512k时才会去执行切割,最小512k,最大10m
job.setInputFormatClass(CombineTextInputFormat.class);
CombineTextInputFormat.setMaxInputSplitSize(job,10240);
CombineTextInputFormat.setMinInputSplitSize(job,512);
//6.设置输入数据和输出数据的的路径
FileInputFormat.setInputPaths(job, new Path("d://ap_event"));
FileOutputFormat.setOutputPath(job, new Path("d://out"));
//FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/input/install.log"));
// FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/outt"));
//7.提交
boolean result = job.waitForCompletion(true);
System.exit(result?0:1);
}
}
2.本地调试的时候需要下载对应的Hadoop安装包(我用的是Hadoop-2.9.2)
3.配置环境变量
4.配置Path环境变量
5.将winutils.exe hadoop.dll下载后放到安装包hadoop-2.9.2\bin下,安装包是没有自带的
6.就可以直接运行代码了
7.添加日志打印
需要有日志出现 则需要将下面的添加到src下的log4j.properties下(IDEA需要放在resource的目录下)
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
8.连接hadoop集群
如果要连接上linux上的hadoop集群 还需要将core-site.xml文件放到src目录下,不然他会默认加载hadoop中的默认文件的,
hadopp自带的文件中是file://这个协议的,所以如果需要用到hdfs://必须修改添加下面的文件
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://192.168.133.200:9000</value> //自己的master节点
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/usr/local/src/hadoop-2.8.4/data</value> //自己定义放临时目录的地方
<description>A base for other temporary directories.</description>
</property>
</configuration>
ps:
1.有的需要丢到C:\Windows\System32下
2.所需软件下载地址:
链接:https://pan.baidu.com/s/1jYHmCIZGo6H8vCdpXuE1cQ
提取码:6666