yiwai 发表于 2016-12-5 06:50:28

第一个hadoop程序-WordCount

首先说明一下环境:我在前面的博客中搭建的hadoop平台,具体为运行在win7上的eclipse3.3连接到位于ubuntu14.04的hadoop集群,至于具体的搭建方法请参见以前的博客。下面开始在eclipse中调试WordCount程序:
打开eclipse,新建一个Map/Reduce Project


 
 


 
 
在Map/Reduce Project工程下,建立一个java文件,命名为WordCount,代码如下:
WordCount程序的源代码,可以在hadoop包的src文件夹中找到
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.fs.Path;
 
public class WordCount {
public static class MyMapper extends Mapper<Object,Text, Text,IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
 
public void map(Object key, Text value,Context context) throws IOException,InterruptedException{
StringTokenizer st=new StringTokenizer(value.toString());
while(st.hasMoreTokens()){
String str=st.nextToken();
word.set(str);
context.write(word, one);
System.out.println(str+"="+one.get());
}
}
}
 
public static class MyReducer 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();
}
System.out.println(key+"="+sum);
result.set(sum);
context.write(key,result);
}
 
}
 
 
 
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: wordcount <in> <out>");
      System.exit(2);
    }
   Job job=new Job(conf,"word count" );
   job.setJarByClass(WordCount.class);
   job.setMapperClass(MyMapper.class);
   job.setCombinerClass(MyReducer.class);
   job.setReducerClass(MyReducer.class);
   
   job.setOutputKeyClass(Text.class);
   job.setOutputValueClass(IntWritable.class);
   
    FileInputFormat.addInputPath(job, new Path(otherArgs));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs));
    System.out.println("运行啦");
    System.exit(job.waitForCompletion(true) ? 0 : 1);
 
}
}
这个程序需要输入文件和输出文件(输入文件会自动生成),因此我们在hdfs中建立输入文件,通过XShell进入NameNode节点,执行如下命令:
mkdir ~/input
cd ~/input
echo “hello world”  >> 1.txt
echo “hello hadoop” >>1.txt
echo “hello java”  >>2.txt
echo “hello c” >>2.txt
执行完这些命令,input中有两个txt文件,内容分别为:
 


 
之后,使用hadoop dfs -put ~/input inin命令将该文件复制到hdfs系统中。
然后就可以运行了,但是运行的时候需要参数,因此进行如下操作设置参数:


 
 


 
 
 
注意,上图中的Program arguments中的内容就是程序运行需要的参数,多个参数之间使用空格就可以,当然换行也可以。
这个程序需要输入输入文件和输出文件在hdfs中的位置,这里,小编的输入输出(输出会自动生成)如图。
到这里,点击对话框下面的Run就可以运行了。
注意:
可能会有的问题:
第一个问题:
org.apache.hadoop.security.AccessControlException:Permission denied:user=xxx,access=WRITE,incode=”hadoop”
这是权限问题,通过Xshell进入namenode节点,先关闭hadoop服务,在修改hadf-site.xml文件,添加如下内容(注意,对hdfs-site.xml的修改要同步到其他所有节点,不然集群无法启动)

 
然后,启动hadoop服务,重新运行WordCount程序,bug消失。
第二个问题:

 
Hadoop错误锦集http://blog.iyunv.com/xmlrpc.php?r=blog/article&uid=22312037&id=4095491
页: [1]
查看完整版本: 第一个hadoop程序-WordCount