MapReduce做词频率统计
WordCount堪称大数据界的HelloWorld
移除点击此处添加图片说明文字
今天来学习搭建hadoop开发环境。并且制作一个本地测试版本的WordCount,稍后我们将会来开发实际项目,在此之前,我们需要了解mapreduce所能做的事情。
先介绍一下业务需求假如我们有这样一个文件:
hadoop hello world
hello hadoop
hbase zookeeper
想统计每个单词出现的次数。
好吧,开始搭建:
首先eclipse准备好,然后创建工程,java工程即可。
创建mapper
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
//该方法循环调用,从文件的split中读取每行调用一次,把该行所在的下标为key,该行的内容为value
protected void map(LongWritable key, Text value,
Context context)
throws IOException, InterruptedException {
String[] words = StringUtils.split(value.toString(), ' ');
for(String w :words){
context.write(new Text(w), new IntWritable(1));
}
}
}
创建reducer:
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
//每组调用一次,这一组数据特点:key相同,value可能有多个。
protected void reduce(Text arg0, Iterable<IntWritable> arg1,
Context arg2)
throws IOException, InterruptedException {
int sum =0;
for(IntWritable i: arg1){
sum=sum+i.get();
}
arg2.write(arg0, new IntWritable(sum));
}
}
最后,创建run方法:
public class RunJob {
public static void main(String[] args) {
Configuration config =new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.181.100:8020");
config.set("yarn.resourcemanager.hostname", "192.168.181.100");
//config.set("mapred.jar", "C:\\Users\\Administrator\\Desktop\\wc.jar");
try {
FileSystem fs =FileSystem.get(config);
Job job =Job.getInstance(config);
job.setJarByClass(RunJob.class);
job.setJobName("wc");
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//FileInputFormat.addInputPath(job, new Path("/data/input/"));
FileInputFormat.addInputPath(job, new Path("/data/wc.txt"));
//Path outpath =new Path("/usr/output/wc");
Path outpath =new Path("/data/output/wc");
if(fs.exists(outpath)){
fs.delete(outpath, true);
}
FileOutputFormat.setOutputPath(job, outpath);
boolean f= job.waitForCompletion(true);
if(f){
System.out.println("job finished!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
解释一下这两行
config.set("fs.defaultFS", "hdfs://192.168.181.100:8020");
config.set("yarn.resourcemanager.hostname", "192.168.181.100");
是为了告诉服务器我们的节点位置。
然后记住要配置本地的hadoop,没错就本机的hadoop,去网上下载源码。如果解压报错什么.so文件啥的,不用管,请记住了哈,什么不是管理员权限啥的,及时用了管理员权限也没用。而且,其实那个.so文件不影响使用hadoop
然后,把我们从网上找到的这个客户端winutils.exe放到bin目录下,如果不放,会报错,null/hadoop/bin:
接着,我们要配置环境变量:
F:\MapReduce\mr\hadoop-2.5.2\hadoop-2.5.2
path后面追加:
;%HADOOP_HOME%\bin
然后,我们要准备数据哦,把这个文本放到服务器上,再上传到hdfs里。
说实话,我还是建议一边做,一遍记文档,这个就是我回头来做的,有很多重要的问题,重要的截图丢了很尴尬啊。
总是报这个错,很烦,然后想办法:
更改所有权限:
./hdfs dfs -chmod 777 /data/wc.txt
但是任然报这个错,原来是hadoop的执行权限里面, /data/output 其他用户只有执行权限,我们任然要写,之前写的写入文件权限,接着改。
org.apache.hadoop.security.AccessControlException: Permission denied: user=lishouzhuang, access=WRITE, inode="/data/output":beifeng:supergroup:drwxr-xr-x
原来的output权限很小:
./hdfs dfs -chmod 777 /data/output2
现在被我放大:
到这里我们在执行run,这回应该不报错了吧,我靠成功了!爱死jiji啦,
去看一下结果:
在这个目录下面,我们要取出来。
然后我们从hdfs拿下来:./hdfs dfs -get /data/output2/wc /file/output/
再然后看看数据:
hadoop2
hbase1
hello2
world1
zookeeper1
好了,wordcount搞定了,简单吧。
今天来学习搭建hadoop开发环境。并且制作一个本地测试版本的WordCount,稍后我们将会来开发实际项目,在此之前,我们需要了解mapreduce所能做的事情。
先介绍一下业务需求假如我们有这样一个文件:
hadoop hello world
hello hadoop
hbase zookeeper
想统计每个单词出现的次数。
好吧,开始搭建:
首先eclipse准备好,然后创建工程,java工程即可。
创建mapper
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
//该方法循环调用,从文件的split中读取每行调用一次,把该行所在的下标为key,该行的内容为value
protected void map(LongWritable key, Text value,
Context context)
throws IOException, InterruptedException {
String[] words = StringUtils.split(value.toString(), ' ');
for(String w :words){
context.write(new Text(w), new IntWritable(1));
}
}
}
创建reducer:
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
//每组调用一次,这一组数据特点:key相同,value可能有多个。
protected void reduce(Text arg0, Iterable<IntWritable> arg1,
Context arg2)
throws IOException, InterruptedException {
int sum =0;
for(IntWritable i: arg1){
sum=sum+i.get();
}
arg2.write(arg0, new IntWritable(sum));
}
}
最后,创建run方法:
public class RunJob {
public static void main(String[] args) {
Configuration config =new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.181.100:8020");
config.set("yarn.resourcemanager.hostname", "192.168.181.100");
//config.set("mapred.jar", "C:\\Users\\Administrator\\Desktop\\wc.jar");
try {
FileSystem fs =FileSystem.get(config);
Job job =Job.getInstance(config);
job.setJarByClass(RunJob.class);
job.setJobName("wc");
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//FileInputFormat.addInputPath(job, new Path("/data/input/"));
FileInputFormat.addInputPath(job, new Path("/data/wc.txt"));
//Path outpath =new Path("/usr/output/wc");
Path outpath =new Path("/data/output/wc");
if(fs.exists(outpath)){
fs.delete(outpath, true);
}
FileOutputFormat.setOutputPath(job, outpath);
boolean f= job.waitForCompletion(true);
if(f){
System.out.println("job finished!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
解释一下这两行
config.set("fs.defaultFS", "hdfs://192.168.181.100:8020");
config.set("yarn.resourcemanager.hostname", "192.168.181.100");
是为了告诉服务器我们的节点位置。
然后记住要配置本地的hadoop,没错就本机的hadoop,去网上下载源码。如果解压报错什么.so文件啥的,不用管,请记住了哈,什么不是管理员权限啥的,及时用了管理员权限也没用。而且,其实那个.so文件不影响使用hadoop
然后,把我们从网上找到的这个客户端winutils.exe放到bin目录下,如果不放,会报错,null/hadoop/bin:
接着,我们要配置环境变量:
F:\MapReduce\mr\hadoop-2.5.2\hadoop-2.5.2
path后面追加:
;%HADOOP_HOME%\bin
然后,我们要准备数据哦,把这个文本放到服务器上,再上传到hdfs里。
说实话,我还是建议一边做,一遍记文档,这个就是我回头来做的,有很多重要的问题,重要的截图丢了很尴尬啊。
总是报这个错,很烦,然后想办法:
更改所有权限:
./hdfs dfs -chmod 777 /data/wc.txt
但是任然报这个错,原来是hadoop的执行权限里面, /data/output 其他用户只有执行权限,我们任然要写,之前写的写入文件权限,接着改。
org.apache.hadoop.security.AccessControlException: Permission denied: user=lishouzhuang, access=WRITE, inode="/data/output":beifeng:supergroup:drwxr-xr-x
原来的output权限很小:
./hdfs dfs -chmod 777 /data/output2
现在被我放大:
到这里我们在执行run,这回应该不报错了吧,我靠成功了!爱死jiji啦,
去看一下结果:
在这个目录下面,我们要取出来。
然后我们从hdfs拿下来:./hdfs dfs -get /data/output2/wc /file/output/
再然后看看数据:
hadoop2
hbase1
hello2
world1
zookeeper1
好了,wordcount搞定了,简单吧。