win7下安装hadoop完成后,接下来就是eclipse hadoop开发环境配置了。具体的操作如下:
一、在eclipse下安装开发hadoop程序的插件
安装这个插件很简单,haoop-0.20.2自带一个eclipse的插件,在hadoop目录下的 contrib\eclipse-plugin\hadoop-0.20.2-eclipse-plugin.jar,把这个文件copy到eclipse的eclipse\plugins
目录下,然后启动eclipse就算完成安装了。
这里说明一下,haoop-0.20.2自带的eclipse的插件只能安装在eclipse 3.3上才有反应,而在eclipse 3.7上运行hadoop程序是没有反应的,所以要针对eclipse 3.7重新编译插件。
另外简单的解决办法是下载第三方编译的eclipse插件,下载地址为:
http://code.google.com/p/hadoop-eclipse-plugin/downloads/list
由于我用的是Hadoop-0.20.2,所以下载hadoop-0.20.3-dev-eclipse-plugin.jar.
然后将hadoop-0.20.3-dev-eclipse-plugin.jar重命名为hadoop-0.20.2-eclipse-plugin.jar,把它copy到eclipse的eclipse\plugins目录下,然后启动eclipse完成安装。
1、在左边的 project explorer 上头会有一个 DFS locations的标志
2、在 windows -> preferences里面会多一个hadoop map/reduce的选项,选中这个选项,然后右边,把下载的hadoop根目录选中(windows上的hadoop只是为了调用里面的jar包)
如果能看到以上两点说明安装成功了。
二、在windows的“C:\Windows\System32\drivers\etc\hosts”路径下加入以下内容:
192.168.136.128 hadoop1
192.168.136.129 hadoop2
192.168.136.130 hadoop3
location name: 这个随便填写,我填写的是:hadoop
Map/Reduce Master 这个框里
Host:就是jobtracker 所在的集群机器,这里写hadoop1(这里可以写主机名,因为前面已经加了主机映射。)
Hort:就是jobtracker 的port,这里写的是9001
这两个参数就是mapred-site.xml里面mapred.job.tracker里面的ip和port
DFS Master 这个框里
Host:就是namenode所在的集群机器,这里写hadoop1
Port:就是namenode的port,这里写9000
这两个参数就是core-site.xml里面fs.default.name里面的ip和port
(Use M/R master host,这个复选框如果选上,就默认和Map/Reduce Master这个框里的host一样,如果不选择,就可以自己定义输入,这里jobtracker 和namenode在一个机器上,所以是一样的,就勾选上)
user name:这个是连接hadoop的用户名,因为我是用root用户安装的hadoop,而且没建立其他的用户,所以就用root。
下面的不用填写。然后点击finish按钮,此时,这个视图中就有多了一条记录。
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; 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; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; /* * 倒排索引 * 现有一批电话通信清单,记录了用户A拨打用户B的记录 * 需要做一个倒排索引,记录拨打给用户B的所有用户A * 13599999999 10086 * 13599999999 120 * 13999999999 13800138000 * 13722222222 13800138000 * 18800000000 120 * 13722222222 10086 * 18966666666 10086 * 18966666666|110 * 任务输出必须如下所示,主叫以‘|’分割 * hdfs://192.168.136.128:9000/user/root/test * hdfs://192.168.136.128:9000/user/root/out */ public class Test extends Configured implements Tool { /* 计数器 Counter 是一个计数器 可以记录这个程序一些数据用于统计 */ enum Counter { LINESKIP, // 出错的行 } public static class Map extends Mapper<LongWritable, Text, Text, Text> { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 读取源文件,line得到的就是输入文件的一行数据 String line = value.toString(); try { // 数据处理 String[] lineSplit = line.split(" "); // 对源数据进行分割重组 String anum = lineSplit[0]; // 主叫 String bnum = lineSplit[1]; // 被叫 context.write(new Text(bnum), new Text(anum)); // 输出 } catch (ArrayIndexOutOfBoundsException e) { context.getCounter(Counter.LINESKIP).increment(1); // 出错令计数器加1 } } } public static class Reduce extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String valueString; String out = ""; // 每个value代表Map函数发送的一个value // 在这里代表拨打了这个被叫号码的一个主叫 for (Text value : values) { valueString = value.toString(); out += valueString + "|"; } context.write(key, new Text(out)); } } public int run(String[] args) throws Exception { Configuration conf = getConf(); Job job = new Job(conf, "test_job"); // 任务名 job.setJarByClass(Test.class); // 执行class FileInputFormat.addInputPath(job, new Path(args[0])); // 输入路径 FileOutputFormat.setOutputPath(job, new Path(args[1])); // 输出路径 job.setMapperClass(Map.class); // 指定上面Map类作为MAP任务代码 job.setReducerClass(Reduce.class); // 指定上面Reduce类作为REDUCE任务代码 job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); // 指定输出KEY的格式 job.setOutputValueClass(Text.class); // 指定输出VALUE的格式等哈 job.waitForCompletion(true); return job.isSuccessful() ? 0 : 1; } public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new Test(), args ); System.exit(res); } }